Here is short post describing some genious functionalities of the plot function for raster stack/brick objects, the addfun, and the nc/nr parameters:
# let's say we've got a raster stack map of some environmental variables
library(biomod2)
## Loading required package: abind
## Loading required package: sp
## Loading required package: raster
## raster 2.0-41 (21-December-2012)
## biomod2 1.3.5 loaded.
stack <- stack(system.file("external/climat/current/bio3.grd", package = "biomod2"),
system.file("external/climat/current/bio4.grd", package = "biomod2"), system.file("external/climat/current/bio7.grd",
package = "biomod2"), system.file("external/climat/current/bio11.grd",
package = "biomod2"), system.file("external/climat/current/bio12.grd",
package = "biomod2"))
# and if we have some random spatial points
set.seed(131) #this is to ensure that no random points will be outside of [-90,90]&[-180,180]
points <- data.frame(long = rnorm(10, mean = 0, sd = 50), lat = rnorm(10, mean = 0,
sd = 50))
points <- SpatialPoints(coords = points, proj4string = CRS("+proj=longlat +datum=WGS84"))
# now we can plot the points on one layer of the stack
plot(stack[[1]])
plot(points, add = TRUE, col = "red", pch = 3)
# but if we want to plot the points on each layers of the stack we need to
# create a function and pass it to the addfun argument (see ?raster::plot)
fun <- function() {
plot(points, add = TRUE, col = "red", pch = 3)
}
plot(stack, addfun = fun)
Another usefull arguments in the raster::plot function are the nc,nr which allows you to control the layout, nc defines on how many columns should the layers be displayed and nr the number of displayed row
plot(stack, addfun = fun, nc = 2, nr = 3)
Enjoy your raster plotting!!