If you imagine that this pen is Trellis, then Lattice is not this pen.

– Paul Murrell1

用户交互

Base R 提供两个函数

  • locator 定位图形中点的坐标
  • identify 识别图中位置/如点对应的名字/标签

其它交互工具还有 Tk/ggobi

将绘图区域拆分为长方形区域/这些区域可能重叠,我们称这个叫 Viewports 视角 rectangular regions (called viewports),详见帮助文档 ?Grid

library(grid)
apropos("^grid\\.")
 [1] "grid.abline"          "grid.add"             "grid.arrows"         
 [4] "grid.bezier"          "grid.cap"             "grid.circle"         
 [7] "grid.clip"            "grid.collection"      "grid.convert"        
[10] "grid.convertHeight"   "grid.convertWidth"    "grid.convertX"       
[13] "grid.convertY"        "grid.copy"            "grid.curve"          
[16] "grid.delay"           "grid.display.list"    "grid.DLapply"        
[19] "grid.draw"            "grid.edit"            "grid.force"          
[22] "grid.frame"           "grid.function"        "grid.gedit"          
[25] "grid.get"             "grid.gget"            "grid.grab"           
[28] "grid.grabExpr"        "grid.gremove"         "grid.grep"           
[31] "grid.grill"           "grid.grob"            "grid.layout"         
[34] "grid.legend"          "grid.line.to"         "grid.lines"          
[37] "grid.locator"         "grid.ls"              "grid.move.to"        
[40] "grid.multipanel"      "grid.newpage"         "grid.null"           
[43] "grid.pack"            "grid.panel"           "grid.path"           
[46] "grid.place"           "grid.plot.and.legend" "grid.points"         
[49] "grid.polygon"         "grid.polyline"        "grid.pretty"         
[52] "grid.raster"          "grid.record"          "grid.rect"           
[55] "grid.refresh"         "grid.remove"          "grid.reorder"        
[58] "grid.revert"          "grid.roundrect"       "grid.segments"       
[61] "grid.set"             "grid.show.layout"     "grid.show.viewport"  
[64] "grid.strip"           "grid.text"            "grid.xaxis"          
[67] "grid.xspline"         "grid.yaxis"          

绘制点、线(线段,直线)等的命令不一样了。如设置图形参数的命令,从 par 变成 gpar,

grid.rect(gp = gpar(fill = "grey"))

新建一个画布、设置背景、创建 viewport

grid.newpage()
grid.rect(gp = gpar(fill = "grey"))
pushViewport(...)
...
popViewport()

定义一个视角

viewport(
  w = 0.9, h = 0.9, # width and height
  xscale = c(xmin, xmax),
  yscale = c(ymin, ymax)
)

给视角留边空

viewport(
  w = 0.9, h = 0.9,
  xscale = c(xmin, xmax) + .05 * c(-1, 1),
  yscale = c(ymin, ymax) + .05 * c(-1, 1)
)

举例子,来自手册

library(grid)
grid.show.viewport(viewport(
  x = 0.6, y = 0.6,
  w = unit(1, "inches"), h = unit(1, "inches")
))

viewport

视角里面包含多张图形

viewport(layout = grid.layout(2, 2))

例子来自手册

grid.show.layout(grid.layout(4, 2,
  heights = unit(
    rep(1, 4),
    c("lines", "lines", "lines", "null")
  ),
  widths = unit(c(1, 1), "inches")
))

layout

新建绘图区域、定义一个新的视角,把它拆分为四份,在每个部分中画图

dessine <- function() {
  pushViewport(viewport(
    w = 0.9, h = 0.9,
    xscale = c(-.1, 1.1), yscale = c(-.1, 1.1)
  ))
  grid.rect(gp = gpar(fill = rgb(.5, .5, 0)))
  grid.points(runif(50), runif(50))
  popViewport()
}
grid.newpage()
grid.rect(gp = gpar(fill = rgb(.3, .3, .3)))
pushViewport(viewport(layout = grid.layout(2, 2)))
for (i in 1:2) {
  for (j in 1:2) {
    pushViewport(viewport(
      layout.pos.col = i,
      layout.pos.row = j
    ))
    dessine()
    popViewport()
  }
}
popViewport()

ex1

非常像 lattice

grid.multipanel(vp = viewport(0.5, 0.5, 0.8, 0.8))

ex2

其中包含两个关键函数,值得一看,分别是 grid.multipanelgrid.panel。另一个例子,用 box-and-whiskers plots 代替其中的散点图试试

do.it <- function(x = runif(100), y = runif(100),
                  a = .9, b = .1,
                  col1 = rgb(0, .3, 0), col2 = rgb(1, 1, 0)) {
  xscale <- range(x) + c(-1, 1) * .05
  yscale <- range(y) + c(-1, 1) * .05
  grid.newpage()
  grid.rect(gp = gpar(fill = col1, col = col1))
  w1 <- a - b / 2
  w2 <- 1 - a - b / 2
  c1 <- b / 3 + w1 / 2
  c2 <- a + b / 6 + w2 / 2
  vp1 <- viewport(
    x = c1, y = c1, width = w1, height = w1,
    xscale = xscale, yscale = yscale
  )
  pushViewport(vp1)
  grid.rect(gp = gpar(fill = col2, col = col2))
  grid.points(x, y)
  popViewport()
  vp2 <- viewport(
    x = c1, y = c2, width = w1, height = w2,
    xscale = xscale, yscale = c(0, 1)
  )
  pushViewport(vp2)
  grid.rect(gp = gpar(fill = col2, col = col2))
  grid.points(x, rep(.5, length(x)))
  popViewport()
  vp3 <- viewport(
    x = c2, y = c1, width = w2, height = w1,
    xscale = c(0, 1), yscale = yscale
  )
  pushViewport(vp3)
  grid.rect(gp = gpar(fill = col2, col = col2))
  grid.points(rep(.5, length(y)), y)
  popViewport()
}
do.it()

ex3


  1. on the difference of Lattice (which eventually was called grid) and Trellis. DSC 2001, Wien (March 2001)