Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I preserve transparency in ggplot2?

Tags:

r

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?

like image 689
Maiasaura Avatar asked Oct 22 '10 22:10

Maiasaura


People also ask

How do I save Ggplot as EPS?

The plot can be saved in the eps format using the ggplot. save() method, which takes as argument the string name of the plot.

How do I make a background transparent in R?

There is no option in Format pane to make the R visual background to be transparent. Only small part (around the R visual) can be transparent as below. But we should be able to set bg="transparent" to make it to be transparent like below. Could you please share your R scripts written in the R script editor?

How do I save in ggplot2?

You can either print directly a ggplot into PNG/PDF files or use the convenient function ggsave() for saving a ggplot. The default of ggsave() is to export the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.


2 Answers

This works:

ggsave("filename.eps", device=cairo_ps) 
like image 191
Rasmus Ø. Pedersen Avatar answered Sep 28 '22 04:09

Rasmus Ø. Pedersen


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() 
like image 23
SeeLittle Avatar answered Sep 28 '22 03:09

SeeLittle