multiple (rasterVis) levelplots
Asked Answered
R

1

10

i'm quite desperate trying to adjust two levelplots of one rasterstack each on one plot. It seems like rasterVis::levelplot does not take the par(mfrow = c(...)) option for splitting the pane. An example using two rasterstacks that shall be arranged side by side on a new plot window :

f <- system.file("external/test.grd", package="raster")
r1 <- stack(raster(f),log(raster(f)))
r2 <- stack(raster(f)-500,raster(f)+500)

par(mfrow=c(2,2))
  levelplot(r1)
  levelplot(r2)

Here, levelplot(r1) is being plotted on the full scale window, while levelplot(r2) unfortunately is painting over levelplot(r1).

I tried to play around, wrapping the call for levelplot with the print function, which takes split as well as newpage = false options. Unfortunately I do not get the twist on how to use split properly so I end up with nothing but frustration.

I'd really appreciate your help, thanks in advance

Andi

Retsina answered 25/2, 2014 at 17:37 Comment(5)
print(p1, split=c(1,1,1,2), more=TRUE); print(p2, split=c(1,2,1,2))Interior
@Interior -- Nifty! I hadn't ever noticed that split= argument in ?print.trellis.Headwater
In the FAQs: How to print several Rasters with different legends in the same pageGoodtempered
@OscarPerpiñán - just a heads up that at the FAQs, print(p, split=c(col(m)[i], row(m)[i], 2, 2) should probably read print(p, split=c(col(m)[i], row(m)[i], ncol(m), nrow(m)).Familiar
@Familiar You are right. Fixed. Thanks.Goodtempered
S
17

For grid-based graphics, including those produced by lattice (which underlies rasterVis' plotting functions) the gridExtra function grid.arrange() does +/- the same thing as par(mfcol=) does for base R graphics.

library(gridExtra)
p1 <- levelplot(r1)
p2 <- levelplot(r2)
grid.arrange(p1, p2, ncol=2)

enter image description here

Edit: An alternative lattice-specific solution uses the split= argument to print.trellis(), the plotting method for lattice plots (h.t. baptiste & Oscar Perpiñán). split= takes a vector of four numbers. The vector's 3rd and 4th elements give the number of columns and rows in the display, while its 1st and 2nd elements give the column- and row-positions of the object being printed.

library(gridExtra)
p1 <- levelplot(r1)
p2 <- levelplot(r2)
print(p1, split=c(1,1,2,1), more=TRUE)
print(p2, split=c(2,1,2,1))
Skite answered 25/2, 2014 at 17:43 Comment(1)
when using levelplot with the methods above (grid.arrange or print) to visualise a raster stack (2 rasters) next to a single raster, the single raster will be twice as big. How do i make them all the same size?Inroad

© 2022 - 2024 — McMap. All rights reserved.