In addition to the base plotting system, lattice
is certainly one of the best known and most used packages for data visualization. The lattice
package was written by Deepayan Sarkar and is not pre-installed in R. The implementation of the lattice plotting system is done by the package, lattice
, which provides the code to build the Trellis graphics and grid, which supports an independent graphing system.
The lattice add-on package is an implementation of Trellis graphics for R. It is a powerful and elegant high-level data visualization system with an emphasis on multivariate data. It is designed to meet most typical graphics needs with minimal tuning, but can also be easily extended to handle most nonstandard requirements. Trellis Graphics, originally developed for S and S-PLUS at the Bell Labs, is a framework for data visualization developed by R. A. Becker, W. S. Cleveland, et al, extending ideas presented in Cleveland's 1993 book Visualizing Data. The Lattice API is based on the original design in S, but extends it in many ways. | ||
--Deepayan Sarkar (http://lattice.r-forge.r-project.org/) |
In contrast to the base plotting system, a whole lattice plot, including any annotations, is produced with only a single function call. The basal format to provide a complete lattice plot looks as follows:
plotType(formula, data = )
The corresponding formula syntax occurs usually in the following manner: variable – tilde – variable, thus y ~ x
. But, depending on the plot type, a variable can also be used alone in the formula notation, ~ x
. If a condition is used, one or more conditional variables follow the formula variables separated by a vertical bar. If there are two conditional variables, they are indicated by an asterisk. The attached argument to call the dataset is either a data frame, a list, or it can be left empty. In the last case, the parent data frame is used. So, the lattice format with example variables for the formula and conditions looks like this:
plotType(y ~ x | a * b, dataset)
It is important to know that only some of the plot parameters, as we know them from the base plotting system, also work for lattice plots. The following code creates a simple scatterplot by using lattice:
library(lattice) library(datasets) ## Simple lattice scatterplot xyplot(speed ~ dist, data = cars, main = "Lattice Scatterplot")
Lattice also offers graph types of all stripes, for any purpose:
Plot Type |
Function Call |
Formula Syntax Example |
---|---|---|
Histogram |
|
|
Bar Chart |
|
|
Dotplot |
|
|
Scatterplot |
|
|
Boxplot (Box-and-Whisker) |
|
|
Kernal Density Plot |
|
|
Strip Plot |
|
|
Theoretical Quantile Plot |
|
|
Two-sample Quantile Plot |
|
|
Scatterplot Matrix |
|
|
Parallel Coordinates Plot |
|
|
3D Scatterplot |
|
|
3D Contour Plot |
|
|
3D Level Plot |
|
|
3D Wireframe Plot |
|
|
Following we have plotted some lattice plot types as an example:
bwplot(count ~ spray, InsectSprays, main = "Boxplot")
qqmath( ~ count | spray, InsectSprays, main = "Theoretical Quantile Plot")
cloud(depth ~ lat * long, data = quakes, zlim = rev(range(quakes$depth)), screen = list(z = 105, x = -70), panel.aspect = 1, xlab = "Longitude", ylab = "Latitude", zlab = "Depth", main = "3D Scatterplot")
The special features of the lattice system are certainly the panel functions. These panel functions control the look of the called plot function and are comparable with the base plot functions for plot annotating. Each lattice plot comes with a default panel function that is customizable.
In the following code script, we customize the previously plotted Theoretical Quantile Plot to show only two panels, above each other, with the layout argument:
qqmath( ~ count | spray, InsectSprays, layout = c(2,1), main = "Two Panels Look")
A more sophisticated example would look like this:
histogram( ~ count | spray, data = InsectSprays, ylab = "", main = "Using The Lattice Panel Functions", panel = function(x, ...) { panel.histogram(x, ...) panel.qqmathline(x, ..., col = "red", lty = 2, lwd = 3) panel.abline(h = median(x), col = "blue", lwd = 2) })
As we have learned, there are some similarities between lattice and the base plotting system, but there are also some significant differences. Most obviously, while the base system directly plots to a graphic device, lattice returns a trellis class as an object. Because of that, the plotting is done by print methods or an object gets auto-printed. That also means, a whole lattice plot can be stored in a variable. Another important point is surely the fact that plotting and annotation are a single function call. The panel functions are certainly a unique feature.
All in all, it can be stated that lattice plots are most useful for conditioning plots, where you want to show the same variables under different conditions.