Lattice Graphs

The lattice package, written by Deepayan Sarkar, aims to enhance base R graphics by providing improved defaults and simplified visualization of multivariate relationships. One of its key features is the support for creating trellis graphs, which allow for the display of variables or relationships between variables, conditioned on one or more other variables.

It supports various types of plots such as scatter plots, line plots, bar plots, and histograms, among others.

The typical format is

graph_type(formula, data=)

where graph_type is selected from the listed below. formula specifies the variable(s) to display and any conditioning variables . For example ~x|A means display numeric variable x for each level of factor A. y~x | A*B means display the relationship between numeric variables y and x separately for every combination of factor A and B levels. ~x means display numeric variable x alone.

graph_type description formula examples
barchart bar chart x~A or A~x
bwplot boxplot x~A or A~x
cloud 3D scatterplot z~x*y
contourplot 3D contour plot z~x*y
densityplot kernal density plot ~x
dotplot dotplot ~x
histogram histogram ~x
levelplot 3D level plot z~y*x
parallel parallel coordinates plot data frame
splom scatterplot matrix data frame
stripplot strip plots A~x or x~A
xyplot scatterplot y~x
wireframe 3D wireframe graph z~y*x

Here are some examples. They use the car data (mileage, weight, number of gears, number of cylinders, etc.) from the mtcars data frame.

# Lattice Examples

# create factors with value labels
cyl.f <-factor(cyl,levels=c(4,6,8),

# kernel density plot
  main="Density Plot",
  xlab="Miles per Gallon")

# kernel density plots by factor level
  main="Density Plot by Number of Cylinders",
  xlab="Miles per Gallon")

# kernel density plots by factor level (alternate layout)
   main="Density Plot by Numer of Cylinders",
   xlab="Miles per Gallon",

# boxplots for each combination of two factors
   ylab="Cylinders", xlab="Miles per Gallon",
   main="Mileage by Cylinders and Gears",

# scatterplots for each combination of two factors
   main="Scatterplots by Cylinders and Gears",
   ylab="Miles per Gallon", xlab="Car Weight")

# 3d scatterplot by factor level
   main="3D Scatterplot by Cylinders")

# dotplot for each combination of two factors
   main="Dotplot Plot by Number of Gears and Cylinders",
   xlab="Miles Per Gallon")

# scatterplot matrix
   main="MTCARS Data")

density1density 2density 3boxplot

scatterplot3D scatterplot dotplotscatterplot matrix

click to view

Note, as in graph 1, that you specifying a conditioning variable is optional. The difference between graphs 2 & 3 is the use of the layout option to contol the placement of panels.

Customizing Lattice Graphs

Unlike base R graphs, lattice graphs are not effected by many of the options set in the par( ) function. To view the options that can be changed, look at help(xyplot). It is frequently easiest to set these options within the high level plotting functions described above. Additionally, you can write functions that modify the rendering of panels. Here is an example.

# Customized Lattice Example
panel.smoother <- function(x, y) {
  panel.xyplot(x, y) # show points
  panel.loess(x, y)  # show smoothed line
hp <- cut(hp,3) # divide horse power into three bands
xyplot(mpg~wt|hp, scales=list(cex=.8, col="red"),
   xlab="Weight", ylab="Miles per Gallon",
   main="MGP vs Weight by Horse Power")

custom trellis graph click to view

Going Further

Lattice graphics are a comprehensive graphical system in their own right. Deepanyan Sarkar's book Lattice: Multivariate Data Visualization with R is the definitive reference. Additionally, see the Trellis User's Guide. Dr. Ihaka has created a wonderful set of slides on the subject. An excellent early consideration of trellis graphs can be found in W.S. Cleveland's classic book Visualizing Data.

To Practice

Try this course on Multivariate Probability Distributions in R.