问题
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
回答1:
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)
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))
来源:https://stackoverflow.com/questions/22021889/multiple-rastervis-levelplots