EPS output with transparency in Sweave output using the cairo_ps driver

Despite all the advantages of pdflatex, I still do a lot of LaTeX writing in which I take the latex -> dvips -> ps2pdf route. This is primarily because of pstricks, which I use for drawing syntactic trees, dependency graphs, and various pictures that I’ve already written out. (I’ve never had complete success with the “pdf” option for pstricks or with xelatex, perhaps because I use other packages within pstricks.) When I write Sweave files, however, I’ve had the trouble that R’s default postscript driver doesn’t support transparency, which is problematic for graphs produced with ggplot2.

However, I found a work-around (): one can direct Sweave to use R’s cairo_ps driver, which *does* support transparency. Putting the following code at the beginning of your .Rnw file accomplishes ths:


<<custom=hide>>=
cairo <- function(name, width, height, ...) grDevices::cairo_ps(file = paste(name, "eps", sep =
"."),
@

Then, in the header of the R code snippets that generate your graphs, use the options fig=T,grdevice=cairo,pdf=F, and you’re good to go.

That being said, I have a long term goal to abandon pstricks and move completely over to tikz, because tikz is clearly the future (from Google Trends):

Post a Comment

You must be logged in to post a comment.