Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 16. Advanced graphics > The lattice package - Pg. 375

The lattice package 375 Because our attention is primarily focused on practical data analyses, we won't elabo- rate on the grid package in this chapter. (If you're interested, refer to Dr. Murrell's Grid website [] for details on this pack- age.) Instead, we'll explore the lattice and ggplot2 packages in some detail. Each allows you to create unique and useful graphs that aren't easily created in other ways. 16.2 The lattice package The lattice package provides a comprehensive graphical system for visualizing uni- variate and multivariate data. In particular, many users turn to the lattice package because of its ability to easily generate trellis graphs. A trellis graph displays the distribution of a variable or the relationship between variables, separately for each level of one or more other variables. Consider the following question: How do the heights of singers in the New York Choral Society vary by their vocal parts? Data on the heights and voice parts of choral members is provided in the singer dataset contained in the lattice package. In the following code library(lattice) histogram(~height | voice.part, data = singer, main="Distribution of Heights by Voice Pitch", xlab="Height (inches)") height is the dependent variable, voice.part is called the conditioning variable, and a histogram is created for each of the eight voice parts. The graph is shown in figure 16.1. It appears that tenors and basses tend to be taller than altos and sopranos. In trellis graphs, a separate panel is created for each level of the conditioning variable. If more than one conditioning variable is specified, a panel is created for each combination of factor levels. The panels are arranged into an array to facilitate comparisons. A label is provided for each panel in an area called the strip. As you'll see, the user has control over the graph displayed in each panel, the format and placement of the strip, the arrangement of the panels, the placement and content of legends, and many other graphic features. The lattice package provides a wide variety of functions for producing univariate (dot plots, kernel density plots, histograms, bar charts, box plots), bivariate (scatter plots, strip plots, parallel box plots), and multivariate (3D plots, scatter plot matrices) graphs. Each high-level graphing function follows the format graph_function(formula, data=, options) where: graph_function is one of the functions listed in the second column of table 16.2. formula specifies the variable(s) to display and any conditioning variables. data specifies a data frame. options are comma-separated parameters used to modify the content, arrange- ment, and annotation of the graph. See table 16.3 for a description of common options.