格子:一个窗口中有多个图块?

我试图通过设置par(mfrow=c(2,1))使用levelplot在一个窗口中放置多个格子图,但似乎忽略了这一点。

有没有一个特殊的function来设置lattice多个图?

'lattice'包是在grid包上构build的,并且在'lattice'加载时附加了它的名称空间。 但是,为了使用grid.layout函数,需要显式地load() pkg :: grid。 另一种方法可能更简单,就是pkg :: gridExtra中的grid.arrange函数:

  install.packages("gridExtra") require(gridExtra) # also loads grid require(lattice) x <- seq(pi/4, 5 * pi, length.out = 100) y <- seq(pi/4, 5 * pi, length.out = 100) r <- as.vector(sqrt(outer(x^2, y^2, "+"))) grid <- expand.grid(x=x, y=y) grid$z <- cos(r^2) * exp(-r/(pi^3)) plot1 <- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="", ylab="", main="Weird Function", sub="with log scales", colorkey = FALSE, region = TRUE) plot2 <- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="", ylab="", main="Weird Function", sub="with log scales", colorkey = FALSE, region = TRUE) grid.arrange(plot1,plot2, ncol=2) 

在这里输入图像描述

Lattice Package经常(但并不总是)忽略par命令,所以在绘制w / Lattice时我只是避免使用它。

要在一个页面上放置多个格子图:

  • 然后创build (但不绘制)格子/格子图的对象

  • 为每个情节打电话一次

  • 对于每个打印呼叫,传递(i)该图的参数 ; (ii) more ,设置为TRUE ,并且只传入用于打印的初始呼叫,以及(iii) pos ,其将页面上的每个图的位置指定为图的左下的xy坐标对分别是右上angular和右上angular,即四个数字的向量。

显示比告诉更容易:

 data(AirPassengers) # a dataset supplied with base R AP = AirPassengers # re-bind to save some typing # split the AP data set into two pieces # so that we have unique data for each of the two plots w1 = window(AP, start=c(1949, 1), end=c(1952, 1)) w2 = window(AP, start=c(1952, 1), end=c(1960, 12)) px1 = xyplot(w1) px2 = xyplot(w2) # arrange the two plots vertically print(px1, position=c(0, .6, 1, 1), more=TRUE) print(px2, position=c(0, 0, 1, .4)) 

一旦你阅读?print.trellis这很简单。 特别感兴趣的是split参数。 看起来似乎很复杂,但一旦你理解了它的含义,就很简单。 从文档:

split:一个由4个整数组成的vectorc(x,y,nx,ny),它表示将当前graphics定位在x,y位置,并以nx和ny的规则排列。 (注意:这是左上angular的原点)

你可以看到几个example(print.trellis) ,但是我喜欢这个:

 library(lattice) # Data w <- as.matrix(dist(Loblolly)) x <- as.matrix(dist(HairEyeColor)) y <- as.matrix(dist(rock)) z <- as.matrix(dist(women)) # Plot assignments pw <- levelplot(w, scales = list(draw = FALSE)) # "scales..." removes axes px <- levelplot(x, scales = list(draw = FALSE)) py <- levelplot(y, scales = list(draw = FALSE)) pz <- levelplot(z, scales = list(draw = FALSE)) # Plot prints print(pw, split = c(1, 1, 2, 2), more = TRUE) print(px, split = c(2, 1, 2, 2), more = TRUE) print(py, split = c(1, 2, 2, 2), more = TRUE) print(pz, split = c(2, 2, 2, 2), more = FALSE) # more = FALSE is redundant 

上面的代码给你这个数字: levelplots

如您所见, split需要四个参数。 最后两个参考你的框架的大小(类似于mfrow ),而前两个参数将你的绘图定位到ny框架。

Interesting Posts