[R] pdf device: rasterize portions of the plot to reduce file size

Stefan Evert stefan.evert at uos.de
Thu Nov 20 12:41:12 CET 2008

> With N=1000, this approach produces a hefty pdf file of ~29MB, while  
> the png file with default resolution is only 72kB. It is clear that  
> I don't want to include the pdf figure in a manuscript, as most pdf  
> readers (let alone the printer) will painfully stall when scrolling  
> down the document. The png file has a good enough resolution (this  
> could be tuned anyway) as far as the levelplot is concerned, however  
> the text and labels are evidently converted to bitmap. I would like  
> to ask whether there are some alternative ways to combine the "best  
> of both worlds" in R, that is to create a pdf file with part of the  
> output being an embedded bitmap (the levelplot in this case). Xfig,  
> as I recall, has a way to produce two separate files for this kind  
> of purpose: one containing the graphical information stripped of  
> annotations, the other the labels and axes to be processed by TeX.

What I usually do in this situation is to produce very high-resolution  
bitmaps (2000 x 2000 pixels and possibly even more). The .png files  
for these will still be much smaller than your 30MB .pdf, and if you  
don't need best quality, you can probably also convert them to .jpg  
format.  Most viewers should be able to display large bitmap images  
fast and in good quality. This seems to be the only way to include  
plots reliably in Microsoft Word documents ...

I'm not sure about the anti-aliasing options of R's png() driver, but  
you could always generate an even higher-resolution bitmap and then  
scale down with standard image processing software (ImageMagick, GIMP,  
xv, ...).

My favourite solution is to generate a .pdf or .eps file, even if this  
is very large, and then convert to a hi-res bitmap image with  
"pstoimg" from the latex2html package.  Useful command-line options are:

	pstoimg -type png -depth 24 -antialias -scale 2 plot.eps

Use the -scale option to generate the desired bitmap size.

Best regards,
Stefan Evert

[ stefan.evert at uos.de | http://purl.org/stefan.evert ]

More information about the R-help mailing list