Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R loses information when saving plot as encapsulated postscript (.eps)

Tags:

r

rstudio

lyx

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. 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.

  2. 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().

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

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

What exactly is going on?

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.

EDIT

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.

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")

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.

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.

like image 902
invictus Avatar asked Feb 10 '15 19:02

invictus


2 Answers

Problem is that the EPS format does not support transparency.

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)

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

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

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)

enter image description here

EDIT: 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()

or even shorter using the export package:

graph2eps(file="plot.pptx", width=8, height=6, cairo=TRUE, fallback_resolution=600)
like image 188
Tom Wenseleers Avatar answered Oct 16 '22 16:10

Tom Wenseleers


Not a solution, but the shortest work around I have found is to set alpha to 1 and change the transparency in another program e.g. in illustrator use select, then all same, and then alter the transparency/ opacity for all. It would be really nice if R would add a feature that allows for transparency to eps...

like image 34
not_a_fan Avatar answered Oct 16 '22 16:10

not_a_fan