Search code examples
rgraphicsplotggplot2postscript

R: use cairo fallback resolution greater than 72dpi in cairo_pdf and cairo_ps


In grDevices R functions cairo_pdf and cairo_ps it is mentioned that when transparency (alpha channels) are used in vector output, it will rasterize the PDF or postscript exported graph at a resolution of 72 dpi : https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/cairo.html

You can see the problem if you try

library(ggplot2)
cairo_ps(file = "test.eps",onefile = FALSE)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))
dev.off()

as in the output (here zoomed in) the plot symbols are heavily pixelated then, showing it is indeed only using 72 dpi : enter image description here

I was wondering how the fallback resolution could be increased to 600 dpi? In library(RGtk2) there is a command cairoSurfaceSetFallbackResolution, which I think is what is relevant here, but I would not know how to make grDevices use that parameter. Any thoughts?

Using postscript() btw also doesn't work, since that doesn't support transparency, and returns the error "semi-transparency is not supported on this device: reported only once per page".


Solution

  • The latest r-devel version has now added the extra argument fallback_resolution, to specify the resolution at which non-supported vector elements should be rasterized, and that seems to solve the problem. E.g. :

    library(ggplot2)
    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()