在levelplot中使用布局功能

时间:2015-02-20 03:19:58

标签: r levelplot

我正在R中进行映射,并在levelplot包中找到了非常有用的rasterVis函数。我想在一个窗口中显示多个图。但是,par(mfcol)不适合lattice。我发现layout函数在我的情况下非常有用,但它无法执行我想要做的事情。

这是我的代码:

s <- stack(Precip_DJF1, Precip_DJF2, Precip_DJF3, Precip_DJF4, 
           Precip_DJF5, Precip_DJF6)

levelplot(s, layout(matrix(c(1, 2, 0, 3, 4, 5), 2, 3)), 
          at=seq(floor(3.81393), ceiling(23.06363), length.out=20), 
          par.settings=themes, par.strip.text=list(cex=0), 
          scales=list(alternating=FALSE))

使用

 layout(matrix(c(1, 2, 0, 3, 4, 5), 2, 3))
layout(3, 2)工作时

失败,但是绘图是按行显示而不是按列显示。我希望绘图显示在第1列,然后是第2列等。例如:

mat <- matrix(c(1, 2, 3, 4, 5, 6), 2, 3)

> mat
#      [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]    2    4    6

levelplotlattice中是否有功能可以执行此类布局?

提前致谢。

1 个答案:

答案 0 :(得分:6)

根据@Pascal的建议,您可以使用index.cond执行此操作:

例如:

library(rasterVis)
s <- stack(replicate(6, raster(matrix(runif(100), 10))))
levelplot(s, layout=c(3, 2), index.cond=list(c(1, 3, 5, 2, 4, 6)))

enter image description here

如果您不想对传递给index.cond的列表进行硬编码,可以使用以下内容:

index.cond=list(c(matrix(1:nlayers(s), ncol=2, byrow=TRUE)))

其中2表示您在布局中的行数。


当然,您也可以传递stack,其中的图层按照所需的行方式绘制顺序排列,例如:

levelplot(s[[c(matrix(1:nlayers(s), ncol=2, byrow=TRUE))]], layout=c(3, 2))