O'Reilly logo

The R Book by Michael J. Crawley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Special Plots

Trellis graphics

The main purpose of trellis graphics is to produce multiple plots per page and multi-page plots. The plots are produced in adjacent panels, typically with one plot for each level of a categorical variable (called the conditioning variable). For instance, you might plot weight against age for each of two genders (males and females). The response variable is weight, the continuous explanatory variable is age (also called the primary covariate in documentation on trellis graphics) and the categorical explanatory variable is gender (a factor with two levels). In a case like this, the default would produce two panels side by side in one row, with the panel for females on the left (simply because ‘f’ comes before ‘m’ in the alphabet). In the jargon of trellis graphics, gender is a grouping factor that divides the observations into distinct groups. Here are the data:

data<-read.table("c:\\temp\\panels.txt",header=T)
attach(data)
names(data)

[1]  "age"  "weight"  "gender"

The package for producing trellis graphics in R is called lattice (not trellis as you might have guessed, because that name was pre-empted by a commercial package):

library(lattice)

The panel plots are created by the xyplot function, using a formula to indicate the grouping structure: weight ~ age | gender. This is read as ‘weight is plotted as a function of age, given gender’ (the vertical bar | is the ‘given’ symbol).

xyplot(weight ~ age | gender)

Trellis graphics is a framework for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required