[R] Reducing the size of pdf graphics files produced with R

Waichler, Scott R Scott.Waichler at pnl.gov
Wed May 23 19:13:22 CEST 2007



Scott

Scott Waichler, Senior Research Scientist
Pacific Northwest National Laboratory
MSIN K9-36
P.O. Box 999
Richland, WA   99352    USA
509-372-4423 (voice)
509-372-6089 (fax)
scott.waichler at pnl.gov
http://hydrology.pnl.gov
------------------------------------------------------------------------
---------------
 

> -----Original Message-----
> From: Joerg van den Hoff [mailto:j.van_den_hoff at fzd.de] 
> Sent: Wednesday, May 23, 2007 9:25 AM
> To: Waichler, Scott R
> Cc: r-help at stat.math.ethz.ch; j.van_den_hoff at fzd.de
> Subject: Re: [R] Reducing the size of pdf graphics files 
> produced with R
> 
> On Wed, May 23, 2007 at 07:24:04AM -0700, Waichler, Scott R wrote:
> > > as you are using MacOS X, you'll have ghostscript 
> installed anyway. 
> > > so try in R `dev2bitmap' with `type =pdfwrite'. I believe `gs' 
> > > _does_ include compression. a quick test showed at least 
> a reduction 
> > > by about a factor of 2 relative to `pdf()'. probably one 
> can fiddle 
> > > with the ghostscript settings (cf. e.g. `Ps2pdf.htm' in the 
> > > ghostscipt
> > > docs: you
> > > can adjust the resolution for images in the pdf file) to improve 
> > > this, so as a last resort you could indeed export the graphics as 
> > > postscript and do the conversion to `pdf' by adjusting 
> the `ps2pdf'
> > > switches. but even with the default settings the pdf produced via 
> > > dev2bitmap/ghostscript is the better solution. apart from 
> file size 
> > > I by and then ran into problems when converting `pdf()' output to 
> > > postscript later on, for instance.
> > 
> > Can you give an example of dev2bitmap usage?  I tried using it in 
> `dev2bitmap(file = "rf.pdf", type = "pdfwrite")' copies the 
> current device to the pdf-file `rf.pdf', i.e. you should have 
> plotted something on the screen prior to using this (the 
> manpage tells you so much...). no `dev.off' is necessary in this case.
> 
> in order to "plot directly" into the pdffile, you can use 
> `bitmap' instead of `dev2bitmap', i.e.
> 
> use either:
> 
> plot(1:10)
> dev2bitmap(file = "rf1.pdf", type = "pdfwrite")
> 
> or:
> 
> bitmap(file = "rf2.pdf", type = "pdfwrite")
> plot(1:10)
> dev.off()
> 
> both should produce the desired output file (at least after 
> including the correct `width' and `height' settings).

I tried the second method, using width=8.5, height=11, but the margins
in the output were bad, as if it assumed a4 paper or something.  I tried
inserting paper="special" and paper="letter" in the bitmap call, but
both caused errors.  Also, the plot was in grayscale instead of color.  

Scott Waichler



More information about the R-help mailing list