Use layout function within levelplot

岁酱吖の 提交于 2019-12-05 03:41:20

问题


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.

Here is my code:

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

Using

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

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

Is there a function within levelplot or lattice to do this kind of layout?

Thanks in advance.


回答1:


As suggested by @Pascal, you can use index.cond to do this:

For example:

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

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

where the 2 indicates the number of rows you will have in your layout.


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


来源:https://stackoverflow.com/questions/28621120/use-layout-function-within-levelplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!