One of the first packages made to make plotting in R easier was the lattice package, which is now included with standard R installations. lattice aims to make creating graphics for multivariate data easier, and relies on R’s formula notation to do so.
We can create a basic scatterplot in lattice using xyplot(y ~ x, data = d)
, where y
is the y-variable, x
the x-variable, and d
the data frame containing the data.
library(lattice)
xyplot(Sepal.Width ~ Sepal.Length, data = iris)
What if we wish to create multiple plots, breaking up the plots by different categorical variables? We can do so with xyplot(y ~ x | c, data = d)
, where all is as before but c
is a categorical variable with which we break up the plots.
xyplot(Sepal.Width ~ Sepal.Length | Species, data = iris)
# Compare to the solution with base R
The aim of lattice is to create complex graphics using a single function call. Thus we can create other interesting graphics in lattice that we could make in base R. Some examples are shown below.
# Lattice comparative dotplot of iris petal length
dotplot(Species ~ Petal.Length, data = iris)
# Lattice comparative boxplot, which resembles the base R comparative
# boxplot in style
bwplot(Species ~ Petal.Length, data = iris)
# For the Cars93 data set, let's look at price depending on the type of car
# and the origin of the car
dotplot(Price ~ Type | Origin, data = Cars93)
bwplot(Price ~ Type | Origin, data = Cars93)
# We can also make histograms and density plots, though since these do not
# lend well to comparison, we must leave the left side of the formula blank.
histogram(~Price | Origin, data = Cars93)
densityplot(~Price | Origin, data = Cars93)