Grid 是另一套图形系统,可完整替代之前的 Base R 中 graphics 包提供的绘图系统,不要与标准的图形系统 Graphics 混淆

斐济地震数据

data(quakes)
head(quakes)
     lat   long depth mag stations
1 -20.42 181.62   562 4.8       41
2 -20.62 181.03   650 4.2       15
3 -26.00 184.10    42 5.4       43
4 -17.97 181.66   626 4.1       19
5 -20.42 181.96   649 4.0       11
6 -19.68 184.31   195 4.0       12

按深度等分成8组,下图从左至右,从下往上的顺序看

library(lattice)
Depth <- equal.count(quakes$depth, number = 8, overlap = .1)
xyplot(lat ~ long | Depth, data = quakes)

quake-01

plot(lat ~ long, data = quakes)

quake-02

op <- par(mfrow = c(2, 2), mar = c(4,4,1,1))
plot(lat ~ long, data = quakes)
plot(lat ~ -depth, data = quakes)
plot(-depth ~ long, data = quakes)
par(op)

quake-03

# library(MVA)
biplot(princomp(quakes[1:3]))

quake-04

pairs(princomp(quakes[1:3])$scores)

quake-05

library(scatterplot3d)
scatterplot3d(quakes[, 1:3],
  highlight.3d = TRUE,
  pch = 20
)

quake-06

大麦产量数据

data(barley)
head(barley)
     yield   variety year            site
1 27.00000 Manchuria 1931 University Farm
2 48.86667 Manchuria 1931          Waseca
3 27.43334 Manchuria 1931          Morris
4 39.93333 Manchuria 1931       Crookston
5 32.96667 Manchuria 1931    Grand Rapids
6 28.96667 Manchuria 1931          Duluth

多重组合

barchart(yield ~ variety | year * site, data = barley)

barley-01

barchart(yield ~ variety | year * site,
  data = barley,
  ylab = "Barley Yield (bushels/acre)",
  scales = list(x = list(0,
    abbreviate = TRUE,
    minlength = 5
  ))
)

barley-02

data(barley)
png(filename = "barley-03.png", res = 200, width = 120*200/30, height = 240*200/30,
    type = "cairo")
dotplot(variety ~ yield | site,
  groups = year,
  data = barley,
  layout = c(1, 6), aspect = .5, pch = 16,
  col.line = c("grey", "transparent"),
  panel = "panel.superpose",
  panel.groups = "panel.dotplot"
)
dev.off()

barley-03

library(nlme)
data(bdf)
d <- data.frame(iq = bdf$IQ.perf, sex = bdf$sex, den = bdf$denomina)
bwplot(~d$iq | d$sex + d$den)
histogram(~d$iq | d$sex + d$den)
densityplot(~d$iq | d$sex + d$den)
stripplot(~d$iq | d$sex + d$den)

bdf-01

bdf-02

bdf-03

bdf-04

d <- data.frame(x = bdf$aritPOST, y = bdf$sex, z = equal.count(bdf$langPRET))
bwplot(~x | y + z, data = d)
histogram(~x | y + z, data = d)
densityplot(~x | y + z, data = d)

bdf-05 bdf-06 bdf-07

d <- data.frame(x = (bdf$IQ.perf > 11), y = bdf$sex, z = bdf$denomina)
d <- as.data.frame(table(d))
barchart(Freq ~ x | y * z, data = d)

bdf-08

x ~ y Plot x as a function of y (on a single plot) x ~ y | z Plots x as a function of y after cutting the data into slices for different values of z x ~ y | z1 * z2
Idem, we cut with according the values of (z1,z2) x ~ y | z, groups=t Idem, but we use a different symbol (or a different colour) according to the values of t

But it also works with univariate data

~ y
~ y | z
~ y | z1 * z2
~ y | z1 * z2, groups=t
n <- 200
x <- rnorm(n)
y <- x^3 + rnorm(n)
plot1 <- xyplot(y ~ x)
plot2 <- bwplot(x)
# Beware, the order is xmin, ymin, xmax, ymax
print(plot1, position = c(0, .2, 1, 1), more = T)
print(plot2, position = c(0, 0, 1, .2), more = F)

x3

n <- 200
x <- rnorm(n)
y <- x^4 + rnorm(n)
k <- .7
op <- par(mar = c(0, 0, 0, 0))
# Attention : l'order est xmin, xmax, ymin, ymax
par(fig = c(0, k, 0, k))
plot(y ~ x)

par(fig = c(0, k, k, 1), new = T)
boxplot(x, horizontal = T)

par(fig = c(k, 1, 0, k), new = T)
boxplot(y, horizontal = F)
par(op)

fig

例1

png(filename = "barley-04.png", res = 200, width = 120*200/30, height = 240*200/30,
    type = "cairo")
dotplot(variety ~ yield | site,
  data = barley,
  groups = year,
  panel = function(x, y, subscripts, ...) {
    dot.line <- trellis.par.get("dot.line")
    panel.abline(
      h = y,
      col = dot.line$col,
      lty = dot.line$lty
    )
    panel.superpose(x, y, subscripts, ...)
  },
  key = list(
    space = "right",
    transparent = TRUE,
    points = list(
      pch = trellis.par.get("superpose.symbol")$pch[1:2],
      col = trellis.par.get("superpose.symbol")$col[1:2]
    ),
    text = list(c("1932", "1931"))
  ),
  xlab = "Barley Yield (bushels/acre) ",
  aspect = 0.5,
  layout = c(1, 6),
  ylab = NULL
)
dev.off()

barley-04

图形参数设置

# 查询图形参数设置
str(trellis.par.get())
List of 35
 $ grid.pars        : list()
 $ fontsize         :List of 2
  ..$ text  : num 12
  ..$ points: num 8
 $ background       :List of 2
  ..$ alpha: num 1
  ..$ col  : chr "transparent"
 $ panel.background :List of 1
  ..$ col: chr "transparent"
 $ clip             :List of 2
  ..$ panel: chr "on"
  ..$ strip: chr "on"
 $ add.line         :List of 4
  ..$ alpha: num 1
  ..$ col  : chr "#000000"
  ..$ lty  : num 1
  ..$ lwd  : num 1
 $ add.text         :List of 5
  ..$ alpha     : num 1
  ..$ cex       : num 1
  ..$ col       : chr "#000000"
  ..$ font      : num 1
  ..$ lineheight: num 1.2
 $ plot.polygon     :List of 5
  ..$ alpha : num 1
  ..$ col   : chr "#00ffff"
  ..$ border: chr "black"
  ..$ lty   : num 1
  ..$ lwd   : num 1
 $ box.dot          :List of 5
  ..$ alpha: num 1
  ..$ col  : chr "#000000"
  ..$ cex  : num 1
  ..$ font : num 1
  ..$ pch  : num 16
 $ box.rectangle    :List of 5
  ..$ alpha: num 1
  ..$ col  : chr "#0080ff"
  ..$ fill : chr "transparent"
  ..$ lty  : num 1
  ..$ lwd  : num 1
 $ box.umbrella     :List of 4
  ..$ alpha: num 1
  ..$ col  : chr "#0080ff"
  ..$ lty  : num 2
  ..$ lwd  : num 1
 $ dot.line         :List of 4
  ..$ alpha: num 1
  ..$ col  : chr "#e6e6e6"
  ..$ lty  : num 1
  ..$ lwd  : num 1
 $ dot.symbol       :List of 5
  ..$ alpha: num 1
  ..$ cex  : num 0.8
  ..$ col  : chr "#0080ff"
  ..$ font : num 1
  ..$ pch  : num 16
 $ plot.line        :List of 4
  ..$ alpha: num 1
  ..$ col  : chr "#0080ff"
  ..$ lty  : num 1
  ..$ lwd  : num 1
 $ plot.symbol      :List of 6
  ..$ alpha: num 1
  ..$ cex  : num 0.8
  ..$ col  : chr "#0080ff"
  ..$ font : num 1
  ..$ pch  : num 1
  ..$ fill : chr "transparent"
 $ reference.line   :List of 4
  ..$ alpha: num 1
  ..$ col  : chr "#e6e6e6"
  ..$ lty  : num 1
  ..$ lwd  : num 1
 $ strip.background :List of 2
  ..$ alpha: num 1
  ..$ col  : chr [1:7] "#ffe5cc" "#ccffcc" "#ccffff" "#cce6ff" ...
 $ strip.shingle    :List of 2
  ..$ alpha: num 1
  ..$ col  : chr [1:7] "#ff7f00" "#00ff00" "#00ffff" "#0080ff" ...
 $ strip.border     :List of 4
  ..$ alpha: num 1
  ..$ col  : chr [1:7] "#000000" "#000000" "#000000" "#000000" ...
  ..$ lty  : num [1:7] 1 1 1 1 1 1 1
  ..$ lwd  : num [1:7] 1 1 1 1 1 1 1
 $ superpose.line   :List of 4
  ..$ alpha: num 1
  ..$ col  : chr [1:7] "#0080ff" "#ff00ff" "darkgreen" "#ff0000" ...
  ..$ lty  : num [1:7] 1 1 1 1 1 1 1
  ..$ lwd  : num [1:7] 1 1 1 1 1 1 1
 $ superpose.symbol :List of 6
  ..$ alpha: num [1:7] 1 1 1 1 1 1 1
  ..$ cex  : num [1:7] 0.8 0.8 0.8 0.8 0.8 0.8 0.8
  ..$ col  : chr [1:7] "#0080ff" "#ff00ff" "darkgreen" "#ff0000" ...
  ..$ fill : chr [1:7] "#CCFFFF" "#FFCCFF" "#CCFFCC" "#FFE5CC" ...
  ..$ font : num [1:7] 1 1 1 1 1 1 1
  ..$ pch  : num [1:7] 1 1 1 1 1 1 1
 $ superpose.polygon:List of 5
  ..$ alpha : num [1:7] 1 1 1 1 1 1 1
  ..$ col   : chr [1:7] "#CCFFFF" "#FFCCFF" "#CCFFCC" "#FFE5CC" ...
  ..$ border: chr [1:7] "black" "black" "black" "black" ...
  ..$ lty   : num [1:7] 1 1 1 1 1 1 1
  ..$ lwd   : num [1:7] 1 1 1 1 1 1 1
 $ regions          :List of 2
  ..$ alpha: num 1
  ..$ col  : chr [1:100] "#FF80FFFF" "#FF82FFFF" "#FF85FFFF" "#FF87FFFF" ...
 $ shade.colors     :List of 2
  ..$ alpha  : num 1
  ..$ palette:function (irr, ref, height, saturation = 0.9)  
 $ axis.line        :List of 4
  ..$ alpha: num 1
  ..$ col  : chr "#000000"
  ..$ lty  : num 1
  ..$ lwd  : num 1
 $ axis.text        :List of 5
  ..$ alpha     : num 1
  ..$ cex       : num 0.8
  ..$ col       : chr "#000000"
  ..$ font      : num 1
  ..$ lineheight: num 1
 $ axis.components  :List of 4
  ..$ left  :List of 3
  .. ..$ tck : num 1
  .. ..$ pad1: num 1
  .. ..$ pad2: num 1
  ..$ top   :List of 3
  .. ..$ tck : num 1
  .. ..$ pad1: num 1
  .. ..$ pad2: num 1
  ..$ right :List of 3
  .. ..$ tck : num 1
  .. ..$ pad1: num 1
  .. ..$ pad2: num 1
  ..$ bottom:List of 3
  .. ..$ tck : num 1
  .. ..$ pad1: num 1
  .. ..$ pad2: num 1
 $ layout.heights   :List of 19
  ..$ top.padding      : num 1
  ..$ main             : num 1
  ..$ main.key.padding : num 1
  ..$ key.top          : num 1
  ..$ xlab.top         : num 1
  ..$ key.axis.padding : num 1
  ..$ axis.top         : num 1
  ..$ strip            : num 1
  ..$ panel            : num 1
  ..$ axis.panel       : num 1
  ..$ between          : num 1
  ..$ axis.bottom      : num 1
  ..$ axis.xlab.padding: num 1
  ..$ xlab             : num 1
  ..$ xlab.key.padding : num 0
  ..$ key.bottom       : num 1
  ..$ key.sub.padding  : num 1
  ..$ sub              : num 1
  ..$ bottom.padding   : num 1
 $ layout.widths    :List of 15
  ..$ left.padding     : num 1
  ..$ key.left         : num 1
  ..$ key.ylab.padding : num 0
  ..$ ylab             : num 1
  ..$ ylab.axis.padding: num 1
  ..$ axis.left        : num 1
  ..$ axis.panel       : num 1
  ..$ strip.left       : num 1
  ..$ panel            : num 1
  ..$ between          : num 1
  ..$ axis.right       : num 1
  ..$ axis.key.padding : num 1
  ..$ ylab.right       : num 1
  ..$ key.right        : num 1
  ..$ right.padding    : num 1
 $ box.3d           :List of 4
  ..$ alpha: num 1
  ..$ col  : chr "#000000"
  ..$ lty  : num 1
  ..$ lwd  : num 1
 $ par.xlab.text    :List of 5
  ..$ alpha     : num 1
  ..$ cex       : num 1
  ..$ col       : chr "#000000"
  ..$ font      : num 1
  ..$ lineheight: num 1
 $ par.ylab.text    :List of 5
  ..$ alpha     : num 1
  ..$ cex       : num 1
  ..$ col       : chr "#000000"
  ..$ font      : num 1
  ..$ lineheight: num 1
 $ par.zlab.text    :List of 5
  ..$ alpha     : num 1
  ..$ cex       : num 1
  ..$ col       : chr "#000000"
  ..$ font      : num 1
  ..$ lineheight: num 1
 $ par.main.text    :List of 5
  ..$ alpha     : num 1
  ..$ cex       : num 1.2
  ..$ col       : chr "#000000"
  ..$ font      : num 2
  ..$ lineheight: num 1
 $ par.sub.text     :List of 5
  ..$ alpha     : num 1
  ..$ cex       : num 1
  ..$ col       : chr "#000000"
  ..$ font      : num 2
  ..$ lineheight: num 1

用紧凑的例子显示参数的作用

show.settings()

par-settings

例2

y <- cars$dist
x <- cars$speed
vitesse <- shingle(x, co.intervals(x, number = 6))
histogram(~x | vitesse,
  type = "density",
  panel = function(x, ...) {
    ps <- trellis.par.get("plot.symbol")
    nps <- ps
    nps$cex <- 1
    trellis.par.set("plot.symbol", nps)
    panel.histogram(x, ...)
    panel.densityplot(x, col = "brown", lwd = 3)
    panel.xyplot(x = jitter(x), y = rep(0, length(x)), col = "brown")
    panel.mathdensity(
      dmath = dnorm,
      args = list(mean = mean(x), sd = sd(x)),
      lwd = 3, lty = 2, col = "white"
    )
    trellis.par.set("plot.symbol", ps)
  }
)

cars

data(sunspot.year)
sunspot <- sunspot.year[20 + 1:37]
xyplot(sunspot ~ 1:37,
  type = "l",
  scales = list(y = list(log = TRUE)),
  sub = "log scales"
)

sunspot-01

xyplot(sunspot ~ 1:37,
  type = "l", aspect = "xy",
  scales = list(y = list(log = TRUE)),
  sub = "log scales"
)

sunspot-02

其它面板函数

apropos("^panel\\.*")
 [1] "panel.3dscatter"        "panel.3dwire"          
 [3] "panel.abline"           "panel.arrows"          
 [5] "panel.average"          "panel.axis"            
 [7] "panel.barchart"         "panel.brush.splom"     
 [9] "panel.bwplot"           "panel.cloud"           
[11] "panel.contourplot"      "panel.curve"           
[13] "panel.densityplot"      "panel.dotplot"         
[15] "panel.error"            "panel.fill"            
[17] "panel.grid"             "panel.histogram"       
[19] "panel.identify"         "panel.identify.cloud"  
[21] "panel.identify.qqmath"  "panel.levelplot"       
[23] "panel.levelplot.raster" "panel.linejoin"        
[25] "panel.lines"            "panel.link.splom"      
[27] "panel.lmline"           "panel.loess"           
[29] "panel.mathdensity"      "panel.number"          
[31] "panel.pairs"            "panel.parallel"        
[33] "panel.points"           "panel.polygon"         
[35] "panel.qq"               "panel.qqmath"          
[37] "panel.qqmathline"       "panel.rect"            
[39] "panel.refline"          "panel.rug"             
[41] "panel.segments"         "panel.smooth"          
[43] "panel.smoothScatter"    "panel.spline"          
[45] "panel.splom"            "panel.stripplot"       
[47] "panel.superpose"        "panel.superpose.2"     
[49] "panel.superpose.plain"  "panel.text"            
[51] "panel.tmd.default"      "panel.tmd.qqmath"      
[53] "panel.violin"           "panel.wireframe"       
[55] "panel.xyplot"          
apropos("^prepanel\\.*")
 [1] "prepanel.default.bwplot"      "prepanel.default.cloud"      
 [3] "prepanel.default.densityplot" "prepanel.default.histogram"  
 [5] "prepanel.default.levelplot"   "prepanel.default.parallel"   
 [7] "prepanel.default.qq"          "prepanel.default.qqmath"     
 [9] "prepanel.default.splom"       "prepanel.default.xyplot"     
[11] "prepanel.lmline"              "prepanel.loess"              
[13] "prepanel.qqmathline"          "prepanel.spline"             
[15] "prepanel.tmd.default"         "prepanel.tmd.qqmath"