lattice

graph_function(formula, data = dataset, options)

Base charts

  • scatter and scatter matrix chart
    • xyplot()
    • splom()
  • bar chart
    • barchart()
  • histogram and density chart
    • histogram()
    • densityplot()
  • dot chart
    • dotplot()
  • daizhuang chart
    • stripplot()
  • Q-Q chart
    • qqmath()
    • qq()
  • box chart
    • bwplot()
  • three dim chart
    • levelplot()
    • contourplot()
    • cloud()
    • wireframe()

package install and load

if(!require(lattice)) install.packages('lattice')
names(trellis.par.get())
##  [1] "grid.pars"         "fontsize"          "background"       
##  [4] "panel.background"  "clip"              "add.line"         
##  [7] "add.text"          "plot.polygon"      "box.dot"          
## [10] "box.rectangle"     "box.umbrella"      "dot.line"         
## [13] "dot.symbol"        "plot.line"         "plot.symbol"      
## [16] "reference.line"    "strip.background"  "strip.shingle"    
## [19] "strip.border"      "superpose.line"    "superpose.symbol" 
## [22] "superpose.polygon" "regions"           "shade.colors"     
## [25] "axis.line"         "axis.text"         "axis.components"  
## [28] "layout.heights"    "layout.widths"     "box.3d"           
## [31] "par.xlab.text"     "par.ylab.text"     "par.zlab.text"    
## [34] "par.main.text"     "par.sub.text"

1. scatter plot

data = data.frame(x = seq(0, 14),
                  y = seq(1:15),
                  z = rep(c('a', 'b', 'c'), times = 5))
xyplot(y ~ x, data = data)

show.settings()

1.1 trellis parameter setting

op = trellis.par.get()
trellis.par.get('axis.text')
## $alpha
## [1] 1
## 
## $cex
## [1] 0.8
## 
## $col
## [1] "#000000"
## 
## $font
## [1] 1
## 
## $lineheight
## [1] 1
trellis.par.set(list(axis.text = list(cex = 1.5, col = 'blue')))
xyplot(y ~ x, data = data)

trellis.par.set(op)
show.settings()

1.2 setting

op = trellis.par.get()
xyplot(y ~ x, groups = z, data = data)

mysetting = trellis.par.get()
mysetting$superpose.symbol$col
## [1] "#0080ff"   "#ff00ff"   "darkgreen" "#ff0000"   "orange"    "#00ff00"  
## [7] "brown"
mysetting$superpose.symbol$pch
## [1] 1 1 1 1 1 1 1
mysetting$superpose.symbol$pch = 1:10
mysetting$superpose.symbol$col = 'black'
trellis.par.set(mysetting)
xyplot(y ~ x, groups = z, data = data)

trellis.par.set(op)

1.3 condition variable

xyplot(y ~ x|z, data = data, layout = c(3, 1))

# panel.abline()
mypanel = function(...) {
  panel.abline(a = 1, b = 1)
  panel.xyplot(...)
}

xyplot(y ~ x|z, data = data, layout = c(3, 1), panel = mypanel)

1.4 group variable

densityplot(~ mpg, data = mtcars, 
            lty = 1:2, col = 1:2, lwd = 2,
            groups = factor(am), 
            main = list("MPG Distribution by Tranamission Type", cex = 1.5),
            xlab = "Miles per Gallon",
            key = list(column = 2, 
                       space = "bottom",
                       title = "Transmission (0 = automatic, 1 = manual)",
                       text = list(levels(factor(mtcars$am))),
                       lines = list(lty = 1:2, col = 1:2, lwd = 2)))

1.5 position setting

graph1 = xyplot(mpg ~ wt, data = mtcars, 
                xlab = "Weight", 
                ylab = "Miles Per Gallon")
displacement = equal.count(mtcars$disp, number = 3, overlap = 0)
graph2 = xyplot(mpg ~ wt|displacement, data = mtcars, layout = c(3, 1),
                xlab = "Weight", 
                ylab = "Miles Per Gallon")

method 1:

plot(graph1, split = c(1, 1, 2, 1))
plot(graph2, split = c(2, 1, 2, 1), newpage = FALSE)

method 2:

plot(graph1, position = c(0, 0, 0.5, 1))
plot(graph2, position = c(0.5, 0, 1, 1), newpage = FALSE)

2. bar chart

barchart(table, ...)
barchart(formula, data = data.frame, ...)
# panel function
panel.barchart()
str(Titanic)
##  table [1:4, 1:2, 1:2, 1:2] 0 0 35 0 0 0 17 0 118 154 ...
##  - attr(*, "dimnames")=List of 4
##   ..$ Class   : chr [1:4] "1st" "2nd" "3rd" "Crew"
##   ..$ Sex     : chr [1:2] "Male" "Female"
##   ..$ Age     : chr [1:2] "Child" "Adult"
##   ..$ Survived: chr [1:2] "No" "Yes"
barchart(Titanic, 
         layout = c(4, 1),         # 4 columns and 1 row
         auto.key = TRUE,          # legend output
         scales = list(x = 'free') # x axis type
         )

barchart(Class ~ Freq|Sex + Age, data = as.data.frame(Titanic), 
         groups = Survived, 
         stack = TRUE, 
         layout = c(4, 1),
         auto.key = list(title = 'Survived', columns = 2), 
         scales = list(x = 'free'))

mygraph = barchart(Class ~ Freq|Sex + Age, 
                   data = as.data.frame(Titanic), 
                   groups = Survived, 
                   layout = c(4, 1),
                   auto.key = list(title = 'Survived', columns = 2),
                   scales = list(x = 'free'))
plot(mygraph)

print(mygraph)

mygraph

update(mygraph, 
       panel = function(...) {
         panel.grid(h = 0, v = 1)
         panel.barchart(..., border = 'transparent')
       })

dot chart

dotplot(formula, data = data.frame, groups = TRUE, horizontal = TRUE)
# data
VADeaths
##       Rural Male Rural Female Urban Male Urban Female
## 50-54       11.7          8.7       15.4          8.4
## 55-59       18.1         11.7       24.3         13.6
## 60-64       26.9         20.3       37.0         19.3
## 65-69       41.0         30.9       54.6         35.1
## 70-74       66.0         54.3       71.1         50.0
dotplot(x = VADeaths, 
        pch = 1:4, col = 1:4, 
        main = list("Death Rates in virginia - 1940", cex = 1.5),
        xlab = "Rate (per 1000)",
        key = list(column = 4,
                   text = list(colnames(VADeaths)),
                   point = list(pch = 1:4, col = 1:4)))