Desktop Survival Guide
by Graham Williams

Graphics in R

As well as being a package of choice for many Statisticians, R is capable of producing excellent graphics in many formats, including PostScript, PDF, PNG, and JPG.

Example code presented in the following chapters will illustrate the generation of publication quality PDF (portable document format) graphics that can be viewed with many viewers, including Adobe's Acrobat. However, R supports many output formats, including PNG (portable network graphics, supported by many web browsers and importable into many word processors), JPG, and PostScript. Another format supported is XFIG. Such output is editable with the xfig graphics editor, allowing further annotations and modifications to be made to the automatically generated plot. The XFIG graphics can then be converted to an even larger collection of graphics formats, including PDF. For the graphics actually presented here in the book R has been used, in fact, to generate XFIG output which is then converted to PDF. Thus the code examples here, generating PDF directly, may give slightly different layouts to the figures that actually appear here.

A highly interoperable approach is to generate graphs in FIG format which can then be loaded into the xfig application, for example, for further editing. This allows, for example, minor changes to be made to fine tune the graphics, but at the cost of losing the ability to automatically regenerate the plot from the original R code. For LATEX processing the rubber package (under Debian GNU/Linux) will automatically convert them to the appropriate EPS or PDF format. Of course, xfig can also generate PNG and JPG and many other formats.

The basic concept of R's graphics model is that a plot is built up bit by bit. Each latter component of the plot overlays earlier components. A plot also has two components. The plotting area is identified by through the usr parameter, as 4 numbers $x_1$, $x_2$, $y_1$, and $y_2$. You can retrieve the current plotting region (which is defined by the first component of a plot) with:

> plot(rnorm(10))
> par("usr")
[1]  0.640000 10.360000 -1.390595  1.153828

The whole figure itself will encompass the plotting region and the region around the plot used to add axis information and labels. Outside of the figure region is the device region. Normally, adding components to a plot, outside of the plotting region, will have no effect--they will be cropped. To ensure they do not get cropped, set the graphic parameter xpd to TRUE:

> par(xpd=TRUE)

Copyright © 2004-2006 [email protected]
Support further development through the purchase of the PDF version of the book.
Brought to you by Togaware.