*Important Lattice functions

xyplot: this is the main function for creating scatterplots bwplot: box-and-whiskers plots (“boxplots”) histogram: histograms stripplot: like a boxplot but with actual points dotplot: plot dots on “violin strings” splom: scatterplot matrix; like pairs in base plotting system levelplot, contourplot: for plotting “image” data

Lattice functions generally take a formula for their first argument, usually of the form

xyplot(y ~ x | f * g, data) We use the formula notation here, hence the ~.

On the left of the ~ is the y-axis variable, on the right is the x-axis variable

f and g are conditioning variables — they are optional

the * indicates an interaction between two variables The second argument is the data frame or list from which the variables in the formula should be looked up

If no data frame or list is passed, then the parent frame is used. If no other arguments are passed, there are defaults that can be used.

library(lattice)
library(datasets)
#Simple scatter plot
xyplot(Ozone~Wind, data=airquality)

plot of chunk unnamed-chunk-1

#Multiple panel
str(airquality)
## 'data.frame':    153 obs. of  6 variables:
##  $ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...
##  $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
##  $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
##  $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
##  $ Month  : int  5 5 5 5 5 5 5 5 5 5 ...
##  $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...
#Month is not a factor variable. Transform it to a factor variable
airquality <- transform(airquality, Month = factor(Month))
str(airquality)
## 'data.frame':    153 obs. of  6 variables:
##  $ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...
##  $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
##  $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
##  $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
##  $ Month  : Factor w/ 5 levels "5","6","7","8",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...
#Now scatter plot based on Month 
xyplot(Ozone ~ Wind | Month, data=airquality)

plot of chunk unnamed-chunk-1

#Want to change default layout
xyplot(Ozone ~ Wind | Month, data=airquality, layout=c(5,1))

plot of chunk unnamed-chunk-1

*Lattice Panel functions

set.seed(10)
x <- rnorm(100)
f <- rep(0:1, each = 50)
y <- x + f - f * x + rnorm(100, sd = 0.5)
f <- factor(f, labels = c("Group 1", "Group 2"))
xyplot(y ~ x | f, layout = c(2, 1))  ## Plot with 2 panels

plot of chunk unnamed-chunk-2

## Custom panel function
xyplot(y ~ x | f, panel = function(x, y, ...) {
    panel.xyplot(x, y, ...)  ## First call the default panel function for 'xyplot'
    panel.abline(h = median(y), lty = 2)  ## Add a horizontal line at the median
})

plot of chunk unnamed-chunk-2

#Lattice Panel Functions: Regression line
## Custom panel function
xyplot(y ~ x | f, panel = function(x, y, ...) {
    panel.xyplot(x, y, ...)  ## First call default panel function
    panel.lmline(x, y, col = 2)  ## Overlay a simple linear regression line
})

plot of chunk unnamed-chunk-2