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

前端 未结 2 757
我寻月下人不归
我寻月下人不归 2021-01-12 15:00

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

相关标签:
2条回答
  • 2021-01-12 15:32

    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)
    
    0 讨论(0)
  • 2021-01-12 15:35

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

    0 讨论(0)
提交回复
热议问题