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 ...
Get The R Book now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.