Combining vector and bitmap graphics in pdf

When building images or heat maps in pdf files, as in the example below, they are saved as vector objects, where each pixel in the image or cell in the heat map is represented by a square. Even with modest resolutions, this leads to unnecessarily large files, which also show ugliness on some devices. Is there a way to make R save only the image area as png or jpg embedded in pdf, but save text, axes, annotations, etc. as vector graphics?

I ask because I often print R graphics, sometimes on large posters, and would like to combine the best of the two worlds. Of course, I could save the whole figure like high-resolution png, but that would not be so elegant, or combine it manually, for example. in Inkscape , but it's pretty tedious.

my.func <- function(x, y) x %*% t(y) pdf(file="myPlot.pdf") image(my.func(seq(-10,10,,500), seq(-5,15,,500)), col=heat.colors(100)) dev.off() 

The figure generated by the above code

Thank you for your time, ideas and, I hope, solutions!

+4
source share
1 answer

Use ?rasterImage or more conveniently in recent versions of image with the useRaster = TRUE option.

This will significantly reduce the file size.

 my.func <- function(x, y) x %*% t(y) pdf(file="image.pdf") image(my.func(seq(-10,10,,500), seq(-5,15,,500)), col=heat.colors(100)) dev.off() pdf(file="rasterImage.pdf") image(my.func(seq(-10,10,,500), seq(-5,15,,500)), col=heat.colors(100), useRaster = TRUE) dev.off() file.info("image.pdf")$size file.info("rasterImage.pdf")$size 

image.pdf: 813229 bytes

rasterImage.pdf 16511 bytes

Read more about new features here:

http://developer.r-project.org/Raster/raster-RFC.html

http://journal.r-project.org/archive/2011-1/RJournal_2011-1_Murrell.pdf

+10
source

All Articles