[R] pdf device: rasterize portions of the plot to reduce file size
baptiste auguie
ba208 at exeter.ac.uk
Thu Nov 20 12:24:09 CET 2008
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))
library(lattice)
p <- levelplot(z~x*y, data=xy, panel=function(...)
{panel.levelplot(...); ltext(5, 5, "text")})
pdf("plot.pdf")
print(p)
dev.off()
png("plot.png")
print(p)
dev.off()
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,
baptiste
sessionInfo()
R version 2.7.2 Patched (2008-08-25 r46438)
i386-apple-darwin9.4.0
locale:
en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8
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
RColorBrewer_1.0-2
loaded via a namespace (and not attached):
[1] grid_2.7.2
_____________________________
Baptiste Auguié
School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
Phone: +44 1392 264187
http://newton.ex.ac.uk/research/emag
More information about the R-help
mailing list