我正在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
levelplot
或lattice
中是否有功能可以执行此类布局?
提前致谢。
答案 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)))
如果您不想对传递给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))