Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining vector and bitmap graphics in a pdf

Tags:

r

When plotting images or heatmaps to pdfs as in the example below they are saved as vector objects where every pixel in the image or cell in the heatmap is represented by a square. Even at modest resolutions this results in unnecessarily large files that also renders uglily on some devices. Is there a way to make R save only the image area as a png or jpg embedded in the pdf but keep text, axes, anotations etc as vector graphics?

I'm asking since I am often printing R graphics, sometimes on large posters, and would like to combine the best of the two worlds. Of course I could save the entire figure as a high resolution png but that would not be as elegant, or combine it manually e.g. in Inkscape but it is quite 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

Thanks for your time, ideas and hopefully solutions!

like image 625
Backlin Avatar asked Sep 29 '11 12:09

Backlin


People also ask

Can PDFs contain both vector and raster formats?

Is a PDF a raster or vector? Most PDFs are vector files. However, it depends on the program used to create the document because PDFs can also be saved as raster files. For example, any PDF created using Adobe Photoshop will be saved as a raster file.

Can vector images be PDFs?

Since vector images can be embedded in PDFs, it is possible to extract these graphics if they are required for use elsewhere. As vector images do not distort when resized, they can be useful when something has to be printed in a large format.

Can you mix raster and vector?

The goal is to fix and size the raster element before you bring it into a vector program. Most great artwork has a combination of a raster image along with vector images (mainly for type), and if you don't combine them correctly, you make it harder to color separate.

What is a PDF vector file?

A vector-based PDF uses line segments to define all of the geometry on the page. Most PDFs created from CAD (Computer-Aided Design) are vector-based. Vector PDFs are usually preferred to raster PDFs because they contain more data that make it easier to work with.


1 Answers

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

That will dramatically reduce the size of the file.

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

See more details about the 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

like image 69
mdsumner Avatar answered Sep 25 '22 15:09

mdsumner