3

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.

jbaums
  • 27,115
  • 5
  • 79
  • 119
code123
  • 2,082
  • 4
  • 30
  • 53
  • 2
    Through `index.cond` option, maybe. –  Feb 20 '15 at 03:27
  • 1
    It would be helpful if you actually included some sample input data to make the problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It also seems like you're confusing the `layout()` function which is used for base graphics with the `layout=` parameter which is used with Lattice plots. – MrFlick Feb 20 '15 at 03:36

1 Answers1

7

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

enter image description here

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))
jbaums
  • 27,115
  • 5
  • 79
  • 119