Maiasaura
Maiasaura

Reputation: 32996

How do I preserve transparency in ggplot2?

I love the plots that ggplot generates. However, it is still somewhat cumbersome to get publication quality plots directly. I usually have to do some post processing in Illustrator (i.e. changing fonts, numbering figures etc). While I could save as tiff or png, eps is best for manipulating figures in Illustrator (I can ungroup objects, move the legend/text etc).

When I save a ggplot object with some transparency (either in points or a smoother) I get this error:

Warning message:
In grid.Call.graphics("L_points", x$x, x$y, x$pch, x$size) :
  semi-transparency is not supported on this device: reported only once per page

Is there a workaround?

Upvotes: 31

Views: 22208

Answers (6)

Tom Wenseleers
Tom Wenseleers

Reputation: 8019

The EPS format in principle does not support semi-transparency - if you want semi-transparency and be able to edit in Illustrator or Inkscape you would better export to SVG or PDF. If you are really tied to the EPS format you could use the cairo device though, and specify at what resolution semi-transparent objects need to be rasterized (only the non-semitransparent elements stay as vector format then though, semi-transparant areas are rasterized to bitmap). The syntax to do this is:

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

ggsave("filename.eps", device=cairo_ps, fallback_resolution = 600)

An alternative option is to export to MS Office Powerpoint in vector format, and do any layout editing there. This can be done easily using my new export package, see https://cran.r-project.org/web/packages/export/index.html and for demo https://github.com/tomwenseleers/export

Typical syntax is very easy, e.g.:

install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))     
graph2ppt(file="ggplot2_plot.pptx", width=6, height=5) 

This results in a fully editable, high quality Powerpoint graph in native Office vector-based DrawingML format, with full support for transparency. In there you can easily fix minor formatting issues, and export to a high quality PDF by printing to PDF.

You can also use it to export to Word, Excel, Latex or HTML and you can also use it to export statistical output of various R stats objects.

It also has a graph2eps(file="plot.eps", width=6, height=5, fallback_resolution=600) function to export to eps with rasterizing of semi-transparent areas...

Upvotes: 6

Boyang Zhang
Boyang Zhang

Reputation: 1

I found an easy way to do so.

Use "zoom" in R plot and click right mouse to copy the picture. You can get a plot with shadow.

Upvotes: -4

SeeLittle
SeeLittle

Reputation: 626

I had the same issues with using the postscript function. I found that cairo_ps from the grDevices package does support transparency (at least in Ubuntu 10.04 with R version 2.10.1). Usage would be:

cairo_ps(filename='filename.eps', width=7, height=7)
plot(x,y)
dev.off()

Upvotes: 25

This works:

ggsave("filename.eps", device=cairo_ps)

Upvotes: 49

Spacedman
Spacedman

Reputation: 94317

You could also try exporting as SVG via the svg device. Not sure if Illustrator can read SVG, but Inkscape can, and it's open source :) I've done some good things with R output to SVG in Inkscape, and the groupings are preserved as with EPS. Can't be 100% sure it handles opacity but I'm sure its in the SVG standard.

Upvotes: 6

zwol
zwol

Reputation: 140876

R's eps "device" doesn't support partial transparency, but, if I remember correctly, its PDF device does. Illustrator ought to be able to read PDFs with equal facility to EPSes, or if not, try converting them after generation with pdftops (not pdf2ps, they are totally different programs and pdf2ps's output is ... inferior).

Note that R doesn't try to optimize its PDF output at all, so even if you do get a plot that needs no postproduction, you will want to run it through a compression utility like qpdf at the very least.

Upvotes: 20

Related Questions