## Inventory & Monitoring (I&M)

# Graphics

**Page Contents**

Please direct questions and comments about these pages, and the R-project in general, to Dr. Tom Philippi.

## Introduction

One of the strengths of R is graphical presentation of data. One consequence is that most pdf introductions to R and introductory books on R include chapters on the basics of R graphics, or include graphical examination of the data integrated with the statistical analyses. For instance, Kuhnert and Venables' "An Introduction to R: Software for Statistical Modelling & Computing" includes an early chapter on base graphics, and a later chapter on advanced graphics with lattice, but also includes relevant graphics in all of the chapters on statistical topics. If you download the book from the CRAN Contributed Documentation site, it comes in a zip file with all of the example code. Similarly, John Maindonald's "Using R for Data Analysis and Graphics" starts with chapters on base graphics and then lattice graphics, then uses graphics in the subsequent chapters on analyses, and all of the R code is available on his website.

## Base Graphics & Extensions

*plot(X)* is the core function for producing a graph of an R object X. If X is a data frame with a factor and a continuous variable, *plot(X)* generates boxplots; if X has a 2 numeric variables *plot(X)* produces a scatterplot; if X has more than 2 numeric variables, *plot(X)* produces a matrix of pairwise scatterplots. If X is a single numeric variable, *plot(X)* plots the values of X against their index position as a sequence; but the syntax of *plot()* allows *plot(X,Y)* to specify separate objects for the X and Y axes. Because R is fully object oriented, many object classes have their own methods defined for *plot()*. Thus *plot(ts)* where ts is a timeseries object produces a timeseries plot, if m1 is the result of lme *plot(m1)* produces the appropriate plot (something like the points and fitted curve). Almost all packages that define new classes of data objects include appropriate methods for *plot()*, so the results of various forms of clustering or factor analysis or ordination or isotonic or polished or smoothed regression each generate the appropriate graphs with plot. For example, before I load more than my default packages, *methods(plot)* lists the following methods on my machine:

*> methods(plot)
[1] plot.acf* plot.ACF* plot.augPred*
[4] plot.compareFits* plot.correspondence* plot.data.frame*
[7] plot.Date* plot.decomposed.ts* plot.default
[10] plot.dendrogram* plot.density plot.ecdf
[13] plot.factor* plot.formula* plot.gls*
[16] plot.hclust* plot.histogram* plot.HoltWinters*
[19] plot.intervals.lmList* plot.isoreg* plot.lda*
[22] plot.lm plot.lme plot.lmList*
[25] plot.margin* plot.mca* plot.medpolish*
[28] plot.mlm plot.nffGroupedData* plot.nfnGroupedData*
[31] plot.nls* plot.nmGroupedData* plot.pdMat*
[34] plot.POSIXct* plot.POSIXlt* plot.ppr*
[37] plot.prcomp* plot.princomp* plot.profile*
[40] plot.profile.nls* plot.randomForest* plot.ranef.lme*
[43] plot.ranef.lmList* plot.ridgelm* plot.rpart*
[46] plot.shingle* plot.simulate.lme* plot.spec
[49] plot.spec.coherency plot.spec.phase plot.stepfun
[52] plot.stl* plot.table* plot.trellis*
[55] plot.ts plot.tskernel* plot.TukeyHSD
[58] plot.Variogram*
*

The syntax for plot is *plot(x,...)*, so all additional parameters specifying axes and colors and symbols and titles are passed on to the underlying graphics system, with different parameters meaninglful for different forms of graphs.

- sciplot
- Advanced graphics: lattice and ggplot
- Graphics in other packages

## lattice

Lattice graphics provide the ability to generate quite complex graphics to present informative views of complex datasets. The general specification of what to graph uses a formula, just as in *glm(), lme()*, and other analytical functions. The general formula is *y~X|block*, where the response Y (which can be more than one rvariable for some forms of linked graphs) as a function of 1 or more X variables, with a separate panel for each block. In addition, most lattice functions accept groups=g to define groups to be plotted with different symbols or colors within each panel. While axes and legends and keys can be turned on (with reasonable defaults) and tweaked within the lattice graphics functions, the colors and symbols used are specified with a slightly convoluted *trellis.par.get() trellis.par.set()* block of code. Precise colors and symbol sets (fonts) are device dependent; *trellis.par.set()* only affects the current device, which is a bit complicated for viewing graphs on the screen and then writing them to a device, but it gives complete control for things like having a graph use color on the screen but gray scales on another device.

The lattice package is a porting and updating of the trellis package in S+. The primary documentation is Deepayan Sarkar's "Lattice multivariate data visualization with R" book in the Springer Use R! series. All of the figures from the book and the R code that generated them are available at http://lmdvr.r-forge.r-project.org/figures/figures.html.

The latticeextra package extends the lattice framework and has additional examples at its website http://latticeextra.r-forge.r-project.org/

## ggplot2

- There is a Graphics task view at CRAN.
- vcd: package from Michael Friendly's book Visualizing Categorical Data
- UCLA's Statistical Computing group has a set of introductory pages on using R, including a page of Graphics by Examples
- Advanced Graphics

## Field and Analysis-specific Graphics in Other Packages

Many field-specific packages include functions for making the standard graphics in that field. For example, the climatol package for climatology generates Walter & Leith climate diagrams (monthly temperature & precipitation on the same graph, scaled so that when the precip line is higher, precipitation exceeds potential evaporation, & when the temperature line is higher, potential evaporation exceeds precipitation) as well as wind rose diagrams. Ade, plotrix, and vcd all include functions to generate terniary plots (triangles of 3-way composition, such as soil diagrams of %silt, sand, & clay). The RGraphExampleLibrary Package List lists many

## Graphics Galleries (with example code)

Some galleries of R graphics, with links to the underlying code (and sometimes text descriptions & explanations):

R Example Graph Library: example graphs from many specialized packages, plus the examples from the documentation for some of the more general graphing packages. The downside is that the coverage is several years out of date.

R Graphics Gallery: wonderful gallery of informative graphs for specific types of data, built using various packages. Note that this is hosted on a free hosting service in France, so some agency filters block access (NPS's contractor blocks it; I send a request; it gets tested and unblocked; then in a month or 2 it gets blocked again, rinse-lather-repeat). Click on a thumbnail to see the full graph, with links to the example R code.

Deepayan Sarkar's Lattice Multivariate Data Visualization with R book has a website with figures and R code.

Paul Murrell's website for his book R Graphics has images of the graphics and the R code that created them.

R Graphic Manuals ?? This has been down for most of 2009.