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

baptiste auguie ba208 at exeter.ac.uk
Thu Nov 20 17:00:51 CET 2008

Thanks for your comment. I would typically follow this approach too,  
but I'm wondering whether one could find a more sophisticated  
solution. Ideally, I'd like to be able to select the text that is  
annotating the figure. There are very few cases where I can see a real  
need for raster text, therefore I like to avoid resorting to it.

Using the versatile grid system, I can envisage two ways to implement  

i) generate a bitmap of the graph without any annotation (removing a  
custom list of grob), and include the resulting image in a pdf file  
containing only the annotations on top. I think grid could manage  
perfectly accurate positioning between the  image and its annotations.

ii) generate a sub-sampled version of the image part of the graph  
before creating the pdf file (I'm thinking of the adimpro package, for  
instance). However, I don't think this approach would work for a graph  
that contains very many lines or other high-level grobs.

I'm probably missing some important points to consider as I have no  
familiarity with the internals of R graphics engine / devices.


On 20 Nov 2008, at 11:41, Stefan Evert wrote:

>> 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 ]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,

Phone: +44 1392 264187


More information about the R-help mailing list