将图另存为封装后记(.eps)时R会丢失信息 [英] R loses information when saving plot as encapsulated postscript (.eps)

查看:663
本文介绍了将图另存为封装后记(.eps)时R会丢失信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Lyx和Latex与.eps图像完美结合.但是,当我从Rstudio导出具有平滑曲线的散点图时,这些点将丢失,并且仅随曲线一起提供该图.

Lyx and Latex work splendidly with .eps images. But when I export a scatter plot with a smoothing curve from Rstudio, the points are lost and the plot is delivered with only the curve.

我尝试过的两种保存方法是:

The two save methods I have tried are:

  1. 在Rstudio中,从图像字段的下拉菜单中选择导出",然后另存为.eps.有趣的是,该图在Rstudio预览中应有的显示.

  1. In Rstudio, choose "Export" from the drop down menu in the image field and save as .eps. Interestingly, the plot appears as it should in the Rstudio preview.

setEPS()开头的绘图代码,然后以postscript()表示所需的尺寸,依此类推,然后使用library(ggplot2)进行绘图调用,例如ggplot().

Preface the plot code with setEPS() followed by postscript(), with the desired dimensions and so on, followed by the plot call using library(ggplot2), e.g. ggplot().

起初,我认为问题可能出在其他地方.但是后来我在Mathematica中保存了一个.eps,没有任何问题.

At first I thought the problem may be elsewhere. But then I saved a .eps in Mathematica and there was no issue.

我窥探了互联网,发现在R中保存.eps的其他问题,但没有一个处理丢失的信息.

I snooped around the internet and found other issues with saving .eps in R, but none dealt with lost information.

到底是怎么回事?

我应该提到以Lyx渲染的.eps成像器比其他任何格式的加载效果都更好,因此我坚持使用.eps.

I should mention that .eps imager render in Lyx loads better than any other format, so I insist on using .eps.

在此先感谢您的输入,但我仍无法对其进行投票.

Many thanks in advance for your input, I cannot yet up-vote them.

编辑

据我所知,这个问题是死路一条,因为EPS无法保持透明色带. (请参阅评论.)我根据请求发布了突出该问题的代码.

So far as I can tell, this question was a dead end due to EPS being unable to keep transparency ribbons. (See comments.) By request I posted code that highlights the problem.

说您有数据data <- data.frame(replicate(2,rnorm(1000))).您想绘制它们,但是有很多点,因此您添加了透明度参数.此外,添加具有置信区间的拟合线.您的代码是:

Say you have the data data <- data.frame(replicate(2,rnorm(1000))). You want to plot them, but there are so many points, so you add a transparency parameter. In addition, you add a fitted line with a confidence interval. Your code is:

ggplot(data = data, aes(x=X1, y=X2)) +
    geom_point(alpha=0.4) +
    stat_smooth(se=T, method="lm")

看起来不错.但是,如果尝试将图另存为EPS,则稍后打开已保存的文件时,将看到的是一个空的图对象,只保存了蓝色拟合线.

Looks good. But if you try to save the plot as an EPS, all you will see when you later open the saved file is an empty plot object save for the blue fitted line.

教训是,如果您坚持使用EPS,则必须关闭透明色带.在这种情况下,请设置alpha=1(或者只是不包括它)和se=FALSE.

Lesson is, if you insist on EPS, you must turn off transparency ribbons. In this case, set alpha=1 (or just don't include it) and se=FALSE.

推荐答案

问题是EPS格式不支持透明度.

Problem is that the EPS format does not support transparency.

一个选项是导出为PDF,然后完全支持透明度:

One option is to export to PDF, transparency is fully supported then :

ggplot(data = data, aes(x=X1, y=X2)) +
  geom_point(alpha=0.4) +
  stat_smooth(se=T, method="lm")
dev.copy2pdf(file="plot.pdf",out.type="cairo", width=10, height=7.5)

然后可以使用pdftops,Inkscape或Adobe Illustrator将PDF转换为EPS.

the PDF you can then convert to EPS with pdftops, Inkscape or Adobe Illustrator.

保存高分辨率PNG也可以透明化,但是它当然不再是矢量格式...

Saving as high res PNG also works with transparency, but then it's no longer vector format of course...

或者您可以使用export软件包(建立在ReporteRs软件包的顶部)导出到Powerpoint,该软件包为您提供完全可编辑的矢量格式,同时还完全支持透明度:

Or you could export to Powerpoint using the export package (built on top of the ReporteRs package), that gives you fully editable vector format with full support for transparency as well :

library(export)
library(ggplot2)
data=data.frame(replicate(2,rnorm(1000)))
ggplot(data = data, aes(x=X1, y=X2)) +
  geom_point(alpha=0.4) +
  stat_smooth(se=T, method="lm")
graph2ppt(file="plot.pptx", width=8, height=6)

如果您绑定到EPS格式,而EPS格式并不能真正正确地支持半透明性,则可以使用cairo_ps(),它可以光栅化半透明区域,但将其余部分保留为矢量格式.在cairo_ps()的最新更新中,现在还有一个参数fallback_resolution来控制以dpi为单位的分辨率,在该分辨率下对半透明区域进行栅格化(其余部分保留为矢量格式).因此,您可以使用:

If you are tied to the EPS format, which does not really properly support semi-transparency, you can use cairo_ps(), that one rasterizes the semi-transparent areas but keeps the rest as vector format. In a recent update of cairo_ps() there is now also an argument fallback_resolution to control the resolution in dpi at which semi-transparent areas are rasterized (the rest stays as vector format). Hence, you can use:

cairo_ps(file = "test.eps", onefile = FALSE, fallback_resolution = 600)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))
dev.off()

,或者使用export软件包更短:

or even shorter using the export package:

graph2eps(file="plot.pptx", width=8, height=6, cairo=TRUE, fallback_resolution=600)

这篇关于将图另存为封装后记(.eps)时R会丢失信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆