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

Joerg van den Hoff j.van_den_hoff at fzd.de
Wed May 23 18:24:57 CEST 2007


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 place
> of a pdf() statement.  An X11 window opened and my figure flew by, but I
> didn't get the file output.  I also used dev2bitmap after opening a
> pdf() and just before the dev.off() statement, since the help says it
> works on the "current device", but again no written output.  What am I
> doing wrong?
> 
> I tried:
>   dev2bitmap(file = plotfile2, type="pdfwrite", width=8.5, height=11,
> pointsize=12)
>   print(myplot())
>   dev.off()
> 
> and
> 
>   pdf(file = plotfile, paper="letter", width=8.5, height=11,
> pointsize=12)
>   print(myplot())
>   dev2bitmap(file = plotfile2, type="pdfwrite", width=8.5, height=11,
> pointsize=12)
>   dev.off()
> 
> Thanks,
> Scott Waichler
> scott.waichler _at_ pnl.gov

`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).


joerg



More information about the R-help mailing list