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.
In order 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 list 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 A
and B
.
~x
means display numeric variable x
alone.
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
|
You can find examples of different graph types presented within our
SOGA-Project by typing the graph type function
(e.g. densityplot()
into the search field in the upper
right corner of this page.
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/data/raw-data/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 : chr "Gonzales, Christina" "Lozano, T'Hani" "Williams, Hanh" "Nem, Denzel" ...
## $ gender : chr "Female" "Female" "Female" "Male" ...
## $ 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 : chr "Muslim" "Other" "Protestant" "Other" ...
## $ nc.score : num 1.91 1.56 1.24 1.37 1.46 1.34 1.11 2.03 1.29 1.19 ...
## $ semester : chr "1st" "2nd" "3rd" "2nd" ...
## $ major : chr "Political Science" "Social Sciences" "Social Sciences" "Environmental Sciences" ...
## $ minor : chr "Social Sciences" "Mathematics and Statistics" "Mathematics and Statistics" "Mathematics and Statistics" ...
## $ 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, ]
We can 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 split the displayed data 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"
)
The lattice graphics are a comprehensive graphical system and there is much more to learn. For further information refer to the book Lattice: Multivariate Data Visualization with R by Deepayan Sarkar (2008). Additionally, see the Trellis User’s Guide (pdf) provided by the Department of Statistics, Purdue University.
Citation
The E-Learning project SOGA-R was developed at the Department of Earth Sciences by Kai Hartmann, Joachim Krois and Annette Rudolph. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Hartmann, K., Krois, J., Rudolph, A. (2023): Statistics and Geodata Analysis using R (SOGA-R). Department of Earth Sciences, Freie Universitaet Berlin.