The lattice package, written by Deepayan Sarkar, improves base R graphics and provides the ability to display multivariate relationships. Hence, the lattice graphics is most useful for conditioning types of plots.

To use the lattice graphics system, we first need to load the package using the library command.

library(lattice)

If you get an error message saying that the package is not found, type install.packages('lattice') to install the package.

The typical format of a lattice plot 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

graph_type description formula
barchart() bar chart x~A or A~x
bwplot() boxplot x~A or A~x
cloud() 3D scatterplot z~x*y|A
contourplot() 3D contour plot z~x*y
densityplot() kernal density plot ~x|A*B
dotplot() dotplot ~x|A
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|A
wireframe() 3D wireframe graph z~y*x

Let us make some plots. Once again we make use of the students data set. You may download the students.csv file here. For a less packed visualization we restrict the data set to the first 100 entries.

students <- read.csv("https://userpage.fu-berlin.de/soga/200/2010_data_sets/students.csv")
str(students)
## 'data.frame':    8239 obs. of  16 variables:
##  $ stud.id        : int  833917 898539 379678 807564 383291 256074 754591 146494 723584 314281 ...
##  $ name           : Factor w/ 8174 levels "Aarvold, Cindi",..: 2480 4196 7858 5109 5770 5592 1258 162 7221 5240 ...
##  $ gender         : Factor w/ 2 levels "Female","Male": 1 1 1 2 1 2 1 1 2 1 ...
##  $ age            : int  19 19 22 19 21 19 21 21 18 18 ...
##  $ height         : int  160 172 168 183 175 189 156 167 195 165 ...
##  $ weight         : num  64.8 73 70.6 79.7 71.4 85.8 65.9 65.7 94.4 66 ...
##  $ religion       : Factor w/ 5 levels "Catholic","Muslim",..: 2 4 5 4 1 1 5 4 4 3 ...
##  $ nc.score       : num  1.91 1.56 1.24 1.37 1.46 1.34 1.11 2.03 1.29 1.19 ...
##  $ semester       : Factor w/ 7 levels ">6th","1st","2nd",..: 2 3 4 3 2 3 3 4 4 3 ...
##  $ major          : Factor w/ 6 levels "Biology","Economics and Finance",..: 5 6 6 3 3 5 5 5 2 3 ...
##  $ minor          : Factor w/ 6 levels "Biology","Economics and Finance",..: 6 4 4 4 4 4 6 2 3 4 ...
##  $ score1         : int  NA NA 45 NA NA NA NA 58 57 NA ...
##  $ score2         : int  NA NA 46 NA NA NA NA 62 67 NA ...
##  $ online.tutorial: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ graduated      : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ salary         : num  NA NA NA NA NA NA NA NA NA NA ...
students100 <- students[1:100,]

Let us construct a scatter plot of weight and height conditioned on the gender variable.

xyplot(height ~ weight | gender, data = students100, 
       ylab = "Height in cm", 
       xlab = "Weight in kg")

The lattice graphics is particularly convenient if we want to make separate plots for more than two groups. For example, we can plot the variables height ~ weight for each religious group.

xyplot(height ~ weight  | religion, data = students100, 
       ylab = "Height in cm", 
       xlab = "Weight in kg")

In order to add a split based on religion and gender, we use the expression religion*gender:

xyplot(height ~ weight | religion*gender, data = students100, 
       ylab = "Height in cm", 
       xlab = "Weight in kg")