1 Introduction to the Lattice Package

1.1 What is the Lattice Package?

The Lattice package represents an elegant and powerful high-level data visualization system that aims to improve on-base R graphs. It has been first inspired by Trellis graphics and was designed with an emphasis on multivariate data to allow the creation of multiple small graphs. The Lattice can be sufficient alone for typical graphics needs. It is also known for its flexibility and ability to handle several nonstandard requirements. This package provides better defaults and helps create and develop many visualization features and plots such as barchart, counterplot, densityplot, histogram, etc. The typical format used to plot lattice graphs is: graph_type(formula, data=)

Such that,

  • graph_type: represents the type of graph to represent.

  • formula: specifies the variables or conditioned variables.

1.2 Content Overview

This R tutorial highlights the basic functions that can be deployed to visualize data using the Lattice package in R. The purpose of this Code-through is to help anyone, even those who do not have any prior knowledge, discover closely the different graphical capabilities of this package.

In this brief tutorial, we will look at how we can create graphs such as scatter plots, 3D scatter plots, boxplots, dotplots, density plots, and histograms by using the different predefined functions available in the Lattice package.


2 Using the Lattice Package in R

2.1 Installing and Loading the Lattice Package

In order to use the lattice graphs in R, it is important that you start first by installing the lattice package.

Then, to load this package in your current session, you will have to invoke the function library(“lattice”).

The two lines of code that follow summarize how the Lattice package can be installed and loaded in R.

# Install
install.packages("lattice")
# Load
library("lattice")

2.2 Available Functions in the Lattice Package

There are several functions available in the Lattice package that can be deployed for several purposes. Some of the most common functions used are the following:

  • xyplot()
  • splom()
  • cloud()
  • stripplot()
  • bwplot()
  • dotplot()
  • barchart()
  • histogram()
  • densityplot
  • qqmath()
  • qq()

2.3 Simple Scatterplot

2.3.1 Plotting Simple Scatterplot

One of the most commonly used functions in the Lattice package is xyplot(). The xyplot() function is usually used to create a scatter plot using the lattice package in R.

In this tutorial, we will be using the iris dataset as an example to demonstrate how each function can be deployed using the Lattice package.

library(lattice)
xyplot(Sepal.Length ~ Petal.Length,
       data = iris)

2.3.2 Adding Categories

To make the graph more meaningful, we can use different colors to group data by categories. To do so, you will need to add the argument: group =“variable” to the xyplot() function.

xyplot(Sepal.Length ~ Petal.Length,
       data = iris,
       group = Species,
       auto.key = TRUE)

2.3.3 Adding a Line

We can also use a smoothing line that will depict in a more concrete way the trend that our data follows. Moreover, the X and Y labels can also be changed by adding arguments to the xyplot() function as follows:

xyplot(Sepal.Length ~ Petal.Length,
       data = iris,
       type = c("p", "g", "smooth"),
       xlab = "Miles/(US) gallons",
       ylab = "Weight (1000lbs)")

2.3.4 Generating Multiple Graphs

As mentioned before, the Lattice plot is known for its ability to create plots in multiple panels. We can generate multiple graphs such that each graph represents a single group.

xyplot(Sepal.Length ~ Petal.Length | Species,
       group = Species,
       data = iris,
       type = c("p", "smooth"),
       scales = "free")

2.4 3D Scatter plot

To create 3D scatter plots using the Lattice package, we can use the predefined function cloud(). For instance, to generate a 3D plot for the iris data set we can use the line of code below.

cloud(Sepal.Length ~ Sepal.Length*Petal.Width,
      data = iris)

We can also categorize our data in the 3D plot the same way we did that using the xyplot() function.

cloud(Sepal.Length ~ Sepal.Length*Petal.Width,
      data = iris,
      group = Species,
      auto.key = TRUE)

2.5 Boxplots

In order to plot Boxplots using the Lattice Package, we should use bwplot() function.

Because the iris data will not be pertinent for the Boxplots, we will be using the ToothGrowth dataset in the next code chunks.

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
bwplot(len ~ dose, data = ToothGrowth,
       xlab = "Dose",
       ylab = "Length")

Again, we can create multiple sub-plots depending on the the different groups that our dataset consists of.

bwplot(len ~ supp|dose,
       data = ToothGrowth,
       layout = c(3,1),
       xlab = "Dose",
       ylab = "Length")

The Lattice package provides the possibility to plot violin graphs just by adding the argument panel = panel.violin to the bwplot() function.

bwplot(len ~ dose, data = ToothGrowth,
           xlab = "Dose",
           ylab = "Length",
           panel = panel.violin)

2.6 Dot Plots

To generate a dotplot for the ToothGrowth dataset, you will have to use the dotplot() function.

dotplot(len ~ dose,
        data = ToothGrowth,
        xlab = "Dose",
        ylab = "Length")

2.7 Strip Plots

Strip plots can be easily created in R using the stripplot() function available in the Lattice package.

stripplot(len ~ dose,
          data = ToothGrowth,
          jitter.data = TRUE,
          pch = 19,
          xlab = "Dose",
          ylab = "Length")

2.8 Density Plots

Density plots can be created in R using the densityplot() function which is also accessible through the Lattice package.

densityplot(~ len, data = ToothGrowth,
                  plot.points = FALSE)

2.9 Histograms

Lattice offers other options to visualize the data such as the histograms. We use the pre-defined function histogram() to create histograms in R just as follows:

histogram(~ len, data = ToothGrowth, 
          breaks = 20)



4 Works Cited


This code through references and cites the following sources: