R Lattice Package With Lattice Graphs

1. Objective – Lattice Package in R

In this tutorial of R Lattice Package, we will learn graphs, graphics and R lattice graphs. Along with this, we will also discuss different types of Lattice functions which we use in lattice graphs.

So, let’s start the R Lattice package tutorial.

R Lattice Package With Lattice Graphs

R Lattice Package With Lattice Graphs

2. Introduction to R Lattice Package

What is Lattice?
It is a powerful and elegant high-level data visualization system. That is being inspired by Trellis graphics. Although, it is being designed with an emphasis on multivariate data. That allows easy conditioning to produce “small multiple” plots.

i. Lattice Graphs

The lattice package was written by Deepayan Sarkar. He provides better defaults. It also provides the ability to display multivariate relationships. And trying to improve on-base R graphics. This package supports the creation of trellis graphs –

  • graphs that display a variable or
  • the relationship between variables, conditioned on one or
  • more other variables.

The typical format is:
graph_type(formula, data=)
We will select graph_type from the listed below. Formula specifies the variable(s) to display and any conditioning variables.
For example:
~x|A means display numeric variable x for each level of factor A;
y~x | A*B relationship between numeric variables y and x for every combination of factor A and B levels;
~x means display numeric variable x alone.

Graph_typedescription
formula examples
barchartbar chartx~A or A~x
bwplotboxplotx~A or A~x
cloud3D scatter plotz~x*y|A
contourplot3D contour plotz~x*y|A
Densityplotkernel density plot~x|A*B
dotplotdotplot~x|A
histogramhistogram~x
levelplot3D level plotz~y*x
Parallelparallel coordinates plotdata frame
Splomscatterplot matrixdata frame
stripplotstrip plotsA~x or x~A
xyplotscatterplot matrixy~x|A
wireframe3D wireframe graphz~y*x

ii. Customizing R Lattice Graphs

For Example:

# Customized Lattice Example
library(lattice)
panel.smoother <- function(x, y) {
panel.xyplot(x, y) # show points
panel.loess(x, y) # show smoothed line
}
attach(mtcars)
hp <- cut(hp,3) # divide horsepower into three bands
xyplot(mpg~wt|hp, scales=list(cex=.8, col="red"),
panel=panel.smoother,
xlab="Weight", ylab="Miles per Gallon",
main="MGP vs Weight by Horse Power")

iii. R Graphics

a. R has two independent graphics subsystems
Traditional graphics

  • Available in R from the beginning
  • Rich collection of tools
  • Not very flexible

Grid graphics

  • recent (2000)
  • Low-level tool, flexible

b. Grid forms the basis of two high-level graphics systems:

  • Lattice: based on Trellis graphics (Cleveland)
  • ggplot2: inspired by “Grammar of Graphics”(Wilkinson)
R Quiz

3. R Lattice Package

  • Trellis graphics for R (developed in S)
  • Powerful high-level data visualization system
  • Provides common statistical graphics with conditioning
  • Emphasis on multivariate data
  • Enough for typical graphics needs
  • Flexible enough to handle most nonstandard requirements

Traditional user interface:

  • Collection of high-level functions: xyplot, dotplot , etc.
  • Interface based on formula and data source

i. High-Level Functions in Lattice

Function    Default Display
histogram()                  Histogram
densityplot()                 Kernel Density Plot
qqmath()                      Theoretical Quantile Plot
qq()                             Two-sample Quantile Plot
stripplot()                     Stripchart (Comparative 1-D Scatter Plots)
bwplot()                       Comparative Box-and-Whisker Plots
barchart()                     Bar Plot
dotplot()                       Cleveland Dot Plot
xyplot()                        Scatter Plot
splom()                        Scatter-Plot Matrix
contourplot()                 Contour Plot of Surfaces
levelplot()                     False Color Level Plot of Surfaces
wireframe()                   Three-dimensional Perspective Plot of Surface
cloud()                          Three-dimensional Scatter Plot
parallel()                       Parallel Coordinates Plot
So, this was all in R lattice Package. Hope you like our explanation.

4. Conclusion

Hence, in this R lattice Package tutorial, we have studied in deep about different graphics and their functions. Moreover, learned their properties which help in creating graphs and functions. Still, if you have any query regarding R Lattice Package, ask in the comment tab.

Refer Best Books of R for learning R Programming Language. 

Reference for R

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.