Producing figures in R

There are many ways to produce figures from R, and this document offers a guide through the maze for those who want to produce figures to incorporate into other documents such as dissertations.

If you are writing a document in LaTeX, you have the choice of producing a printable version by latex+dvips or by pdflatex. Which you use determines the most suitable format for the figures: latex+dvips requires 'encapsulated Postscript' figures, and pdflatex either PDF or PNG figures. See our LaTeX FAQ for more details.

For Word you can include 'encapsulated Postscript' if you are going to print to a Postscript printer (as we do in the department), or (extended) Windows metafiles (.wmf or .emf) or various bitmap formats such as PNG and JPEG.

You will almost always be resizing the figure when incorporating it into another document, and this means that it is best to avoid bitmap formats and use only vector formats (.eps, .pdf, .emf, .wmf). If you do want/need to use a bitmap format, avoid JPEG which is designed for photographs and will blur lines and text: PNG is usually the best bitmap format for the graphs that you will produce in R.

Windows users
The simplest thing to do is to save the graph from the version you produced on screen. From the File menu of the graphics window you have the option to Save as various formats. The ones that interest us here are Metafile, Postscript (which does give encapsulated Postscript), PDF and PNG. Choose an appropriate format for your needs and save to a file.

Saving from the R command line can be done by the function savePlot() which parallels the options from its menu: see ?savePlot for details.

For use in Word, you can also copy the plot to the clipboard and paste from the clipboard into Word. It is best to use the Metafile format, selected from File -> Copy to clipboard -> as a Metafile with shortcut CTRL+W.

For some purposes (including inserting into Word documents) it can be helpful to add a bitmap preview to an encapsulated Postscript file. You can do this with GSView: load the file into GSView (typically by double-clicking on it), and use Edit -> add EPS preview, then save.

Note for users of R 2.6.0: you may get an error if you try to save to Postscript or PDF. The best thing to do is to update your R to 2.6.1, but a workaround is given here.

Linux users
There is a function dev.copy2eps() to copy the screen device to an encapsulated Postscript file.

Function dev.print() can be used to copy the screen device to a number of other formats, including PNG.

There are a number of ways to produce bitmap plots, including the png() and jpeg() devices, bitmap() (which uses gs) and various Cairo-based devices in packages Cairo and cairoDevice. Depending on the plot, some of these give better results than others.

All users
The highest quality results are obtained by plotting directly in the format you need. R has postscript(), pdf() and png() (and more) devices. The general workflow is

- try out commands to produce the plot on-screen
- open a new device
- repeat the commands

For example, to produce an encapsulated Postscript plot you might use

postscript("fig2.eps", width=6, height=4, pointsize=12, paper="special",
      horizontal=FALSE, onefile=FALSE)

repeat commands

You can tweak the sizes to obtain the best results at the final reproduction size (it is generally best to plot at about 1.5x the final size).

If you are incorporating figures into a LaTeX document using Computer Modern fonts (the default), typographic good practice suggests that you should use the same fonts to label your figures. This can be done, and you will see hints on the help page for postscript() with more details in the Murrell-Ripley article R-News 2006-2.