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

From: baptiste auguie <ba208_at_exeter.ac.uk>
Date: Thu, 20 Nov 2008 11:24:09 +0000

Dear list,

My favorite output format is usually pdf. I can include the graphics in pdflatex documents and benefit from the scalable nature of vector graphic formats.
However, I recently had to generate high-res 2D levelplot graphics as in the example below,

N <- 100
# N <- 1000 # slow to diplay
xy <- expand.grid(x=seq(0, 10, length=N), y=seq(0, 10, length=N))

xy <- within(xy, z <- x^2*cos(x*y))


p <- levelplot(z~x*y, data=xy, panel=function(...) {panel.levelplot(...); ltext(5, 5, "text")})



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. On a side note, I noticed that Acrobat Professional can substantially reduce the file size (3.4MB), but it does not seem to help some pdf readers (Preview on Leopard, for one). I'm open to suggestions of auxiliary tools that might help.

Best regards,


R version 2.7.2 Patched (2008-08-25 r46438) i386-apple-darwin9.4.0


attached base packages:
[1] stats graphics grDevices utils methods base

other attached packages:
[1] latticeExtra_0.5-1 lattice_0.17-13 baptMisc_1.0

loaded via a namespace (and not attached):
[1] grid_2.7.2

Baptiste Auguié

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

Phone: +44 1392 264187


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. Received on Thu 20 Nov 2008 - 11:27:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 20 Nov 2008 - 12:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive