I have 2 rasters stacked together:
library(rasterVis)
r1 <- raster(system.file("external/test.grd", package="raster"))
r2 <- r1 / 2
r.stack <- stack(r1, r2)
Since I would like to highlight some areas later for each specific layer in the stack, I create two point datasets based on the raster values:
pts1 <- rasterToPoints(r1, spatial=T)
idx <- which(as.data.frame(pts1)[, 1] >= 400)
pts1 <- pts1[idx, 1]
pts2 <- rasterToPoints(r2, spatial=T)
idx <- which(as.data.frame(pts2)[, 1] >= 400)
pts2 <- pts2[idx, 1]
Now, I would like to plot the raster stack with levelplot from rasterVis in R. I would also like to overlay r1 with pts1 and r2 with pts2.
However, as soon as I add one point dataset, it is used for both layers:
levelplot(r.stack) + layer(sp.points(pts1, pch=20, cex=0.1, col="black"))
How can I use specific point datasets with specific layers while still using raster stacks?
I would like to avoid making my own subplot by plotting each layer with its' specific point dataset alone followed by using print.trellis. I tried it, but the result is just inferior compared to levelplot with raster stacks.
Any idea on how to achieve that?
With the panel.number
function you can subset your data according to the panel you are located:
pts <- list(pts1, pts2)
levelplot(r.stack) +
layer(sp.points(pts[[panel.number()]],
pch=20, cex=0.1, col="black"))
来源:https://stackoverflow.com/questions/31651694/plot-specific-point-data-on-each-layer-in-stack-with-rastervis-in-r