baptiste auguie
2008-Nov-20 11:24 UTC
[R] pdf device: rasterize portions of the plot to reduce file size
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
Stefan Evert
2008-Nov-20 11:41 UTC
[R] pdf device: rasterize portions of the plot to reduce file size
> 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 ]