2010-03-29 43 views
53

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

lattice中设置多个图有没有特别的功能?

+2

'lattice'图通常不使用'par'设置。他们有自己的一组来自Grid图形的设置。请参阅'?trellis.par.get'获取解释。 – James 2010-03-30 12:56:39

回答

38

包往往(但并不总是)忽略相提并论命令,所以我只是避免出现打印W/时使用它。

将多个格子情节单页上:

  • 创建(但不积)格/网格情节对象,然后

  • 致电打印每个地块一次

  • 每个打印呼叫,通过在(i)所述情节参数; (ⅱ) ,设置为TRUE,并且其仅通过在用于初始调用打印,和(iii)POS,这给对每个小区的位置页面分别指定为曲线的左下角和右上角 拐角处的xy坐标对 - 即,具有四个数字的矢量。

更容易表现出比告诉:

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)) 
+3

另请参见'print.trellis'的'split'参数和Murrell的“R图形”的第5.8节http://books.google.co.uk/books?id=78P4zntHHVQC – 2010-03-31 10:42:37

+3

如果有公共坐标轴,'c 'latticeExtra'包中的.trellis'快捷方式也很有用。 – qoheleth 2014-12-24 00:58:58

60

的“格子”包是建立在格包,当“格子”装载重视它的命名空间。但是,为了使用grid.layout函数,您需要明确地指定load() pkg :: grid。另一种选择,这可能是更容易,是在pkg的grid.arrange功能:: gridExtra:

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) 

enter image description here

+1

这比接受的答案更整齐。 – qed 2013-10-26 13:55:17

+0

[这里](http://www.statmethods.net/advgraphs/layout.html)你可以找到整洁的例子。 – Masoud 2017-04-04 15:06:32

+0

@Masoud:该页面仅使用基础图形。问题是关于格子图形。 – 2017-04-04 17:12:57

6

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

split:一个由4个整数组成的向量c(x,y,nx,ny),它表示将当前绘图定位在x,y位置,地块。(注:这有原点在左上角)

你可以看到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所做的),而参数将您的绘图定位到nxny帧。