在levelplot中使用布局功能 [英] Use layout function within levelplot
问题描述
我正在R中进行映射,并且在rasterVis
包中发现了非常有用的levelplot
函数.我想在一个窗口中显示多个图.但是,par(mfcol)
不适合lattice
.我发现layout
函数在我的情况下非常有用,但是无法执行我想做的事情.
I am doing mapping in R and found the very useful levelplot
function in rasterVis
package. I will like to display multiple plots in a window. However, par(mfcol)
does not fit within lattice
. I found layout
function very useful in my case but it fails to perform what I want to do.
这是我的代码:
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列,等等.
fails while layout(3, 2)
works but the plots are displayed row-wise instead of column-wise. I want the plots to be displayed in column 1, then column 2 etc. Something like:
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
内是否存在用于执行这种布局的功能?
Is there a function within levelplot
or lattice
to do this kind of layout?
谢谢.
推荐答案
如@Pascal所建议,您可以使用index.cond
来做到这一点:
As suggested by @Pascal, you can use index.cond
to do this:
例如:
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
的列表进行硬编码,则可以使用以下方式:
If you don't want to hard-code the list passed to index.cond
, you can use something like:
index.cond=list(c(matrix(1:nlayers(s), ncol=2, byrow=TRUE)))
其中2
表示布局中将具有的行数.
where the 2
indicates the number of rows you will have in your layout.
当然,您也可以传递stack
,其中的图层按所需的按行绘制顺序排列,例如:
Of course you could also pass a stack
with layers arranged in the desired row-wise plotting order, e.g.:
levelplot(s[[c(matrix(1:nlayers(s), ncol=2, byrow=TRUE))]], layout=c(3, 2))
这篇关于在levelplot中使用布局功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!