I want to plot both a raster layer and a polygon layer using rasterVis::levelplot()
, however, I want to have the polygon layer appear under the raster layer.
library(sp)
library(rgeos)
library(raster)
library(rasterVis)
r <- raster(xmn = 0, xmx = 1, ymn = 0, ymx = 1, nrows = 100, ncols = 100,
crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
r[] <- rnorm(100 * 100)
r[sample(1:ncell(r), ncell(r) / 2)] <- NA
pt <- data.frame(x = 0.5, y = 0.5)
coordinates(pt) <- ~ x + y
proj4string(pt) <- projection(r)
pt_buff <- gBuffer(pt, width = 0.25)
levelplot(r, margin = FALSE) +
layer(sp.polygons(pt_buff, fill = "grey40", col = NA))