Trellis/Lattice plots

"Trellis" plots are the R version of Lattice plots that were originally implemented in the S language at Bell Labs.  The aim of these plots is to extend the usual kind of univariate and bivariate plots, like histograms or scatter plots, to situations where some external variables, possibly categorical or "factor" variables, may influence the distribution of the data or form of a relationship.  They do this by generating a trellis or lattice of plots that consist of an array of simple plots, arranged according to the values of some "conditioning" variables.

A Trellis-type plot

A multipanel plot, in which the individual panels are "conditioned" by the value of a third variable can be illustrated for the Oregon climate station data using the following script:

library(lattice)
trellis.device(color=TRUE, theme = "col.whitebg")   # change default colors

attach(orstationc)
# make a factor variable indicating which latitude band a station falls in
Lon2 <- equal.count(lon,8,.5)
# plot the lattice plot
plot1 <- xyplot(pann ~ elev | Lon2,
    layout = c(4, 2),
    panel = function(x, y) {
        panel.grid(v=2)
        panel.xyplot(x, y)
        panel.loess(x, y, span = 1.0, degree = 1, family="symmetric")
        panel.abline(lm(y~x))
    },
    xlab = "Elevation (m)",
    ylab = "Annual Precipitation (mm)")
print(plot1, position=c(0,.375,1,1), more=T)

# add the shingles
print(plot(Lon2), position=c(.1,0.0,.9,.4))
detach(orstationc)

Some more plots

The following plots explore the seasonality of precipitation in the Yellowstone region.

trellis.device(color=TRUE, theme = "col.whitebg")
attach(yellpratio)

# simple map
library(gpclib)
library(maptools)
ynpstate.shp <- readShapeLines(file.choose(),
     proj4string=CRS("+proj=longlat"))
ynplk.shp <- readShapeLines(file.choose(),
     proj4string=CRS("+proj=longlat"))
ynprivers.shp <- readShapeLines(file.choose(),
     proj4string=CRS("+proj=longlat"))
plot(ynpstate.shp)
plot(ynplk.shp, add=T)
plot(ynprivers.shp, add=T)
points(Lon, Lat)

# stars plot for precipitation ratios
col.red <- rep("red",length(orstationc[,1]))
stars(yellpratio[,4:15], locations=as.matrix(cbind(Lon, Lat)),
col.stars=col.red, len=0.2, key.loc=c(-111.5,42.5), labels=NULL, add=T)

# create some conditioning variables
Elevation <- equal.count(Elev,4,.25)
Latitude <- equal.count(Lat,2,.25)
Longitude <- equal.count(Lon,2,.25)

# plot shingles
plot(Elevation)
plot(Latitude)
plot(Longitude)

# January vs July Precipitation Ratios by Elevation
plot2 <- xyplot(APJan ~ APJul | Elevation,
    layout = c(2, 2),
    panel = function(x, y) {
        panel.grid(v=2)
        panel.xyplot(x, y)
        panel.loess(x, y, span = 1.0, degree = 1, family="symmetric")
        panel.abline(lm(y~x))
    },
    xlab = "APJul",
    ylab = "APJan")
print(plot2, position=c(0,.375,1,1), more=T)
print(plot(Elevation), position=c(.1,0.0,.9,.4))

# January vs July Precipitation Ratios by Latitude and Longitude
plot3 <- xyplot(APJan ~ APJul | Latitude*Longitude,
    layout = c(2, 2),
    panel = function(x, y) {
        panel.grid(v=2)
        panel.xyplot(x, y)
        panel.loess(x, y, span = .8, degree = 1, family="gaussian")
        panel.abline(lm(y~x))
    },
    xlab = "APJul",
    ylab = "APJan")
print(plot3)

# Parallel plot of precipitation ratios
plot4 <- parallel(~yellpratio[,4:15] | Elevation,
    layout = c(4, 1),
    ylab = "Precipitation Ratios")
print(plot4)

# Lattice plot of scatter plot matrices
plot5 <- splom(~cbind(APJan,APJul,Elev) | Latitude*Longitude)
print(plot5)