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

2

8

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.

Handtomouth answered 10/2, 2015 at 19:32 Comment(8)
Do you have the same problem using R from a terminal, without RStudio? What is your operating system, R version and RStudio version? A complete minimal working example to reproduce your problem would also help.Brandenburg
First, specs: R 3.1.0 GUI 1.64 Snow Leopard build (6734), RStudio Version 0.98.1091, Mac OSX 10.8.5. No .eps problem when using base R i.e plot(). The problem occurs when calling ggplot(). A common error message says: "semi-transparency is not supported on this device: reported only once per page"Handtomouth
The information in your comment is most revealing. Your geom_smooth() creates a ribbon with transparency (alpha). The eps device doesn't support transparency. To fix this, you need to set alpha=1 in the geom_smooth().Ineslta
Thanks @Andrie, I just discovered transparency is not supported by postscript from here. Guess this is a dead end.Handtomouth
You should post full code. This may be a case of R-FAQ 7.21.Afeard
This question might be useful to other people if it included the full code necessary to reproduce this problem. Without that, it's only useful to the one person who knows the details.Brandenburg
@Ineslta consider posting this as an answer and hopefully OP will accept it as the correct answer.Wentzel
Sorry for the late reply here. Posted sample code. Hope it helps someone.Handtomouth
P
15

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)
Polychaete answered 29/6, 2015 at 7:26 Comment(1)
Spelling is the lowest form of knowledge!Handtomouth
C
3

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

Chic answered 25/9, 2015 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.