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:
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.
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
.
Problem is that EPS in R 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 package ReporteRs
, that gives you fully editable vector format with full support for transparency as well :
library( ReporteRs )
require( ggplot2 )
data=data.frame(replicate(2,rnorm(1000)))
mydoc = pptx( )
mydoc = addSlide( mydoc, slide.layout = "Title and Content" )
myplot = ggplot(data = data, aes(x=X1, y=X2)) +
geom_point(alpha=0.4) +
stat_smooth(se=T, method="lm")
mydoc = addPlot( mydoc, function( ) print( myplot ), vector.graphic=T, offx=1,offy=0,width=10, height=7.5, fontname="Arial", pointsize=20)
writeDoc( mydoc, file = "plot.pptx" )
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:
ggsave("filename.eps", device=cairo_ps, fallback_resolution = 600)
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...