1 Introduction to Lattice Package

1.1 What is the Lattice Package?

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 that allows producing multiple small graphs. 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 without 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 fuctions available in the Lattice package.


2 Using tha 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, you to load it in your current session, you shouls 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 Using xyplot() in the lattice package

2.3.1 Simple Scatterplot

2.3.1.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)

#### 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 to add the argument: group =“variable” to the xyplot() function.

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

2.3.1.2 Adding a Line

We can also use a smoothing line that will represent in a more general way our data. Moreover, the X and Y label can also be changed by adding argument to the xyplot() as follows:

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

#### 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.3.2 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.3.3 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.3.4 Dot Plots

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

2.3.5 Strip Plots

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

2.4 Density Plots in the Lattice Package

Density plots can be created in R using the densityplot() function that is available with the lattice package.

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

2.4.1 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: