Plotting

Several generic functions have been implemented for SpatRaster objects to create maps and other plot types. Use ‘plot’ to create a map of a SpatRaster object. When plot is used with a SpatRaster, it calls the function ‘rasterImage’ (but, by default, adds a legend; using code from fields::image.plot). It is also possible to directly call image. You can zoom in using ‘zoom’ and clicking on the map twice (to indicate where to zoom to). With click it is possible to interactively query a SpatRaster object by clicking once or several times on a map plot.

After plotting a SpatRaster you can add vector type spatial data (points, lines, polygons). You can do this with functions points, lines, polygons if you are using the basic R data structures or plot(object, add=TRUE) if you are using Spatial* objects as defined in the sp package. When plot is used with a multi-layer SpatRaster object, all layers are plotted (up to 16), unless the layers desired are indicated with an additional argument. You can also plot SpatRaster objects with ggplot (via the “tidyterra” package). The rasterVis package has several other lattice based plotting functions for SpatRaster objects.

Multi-layer SpatRasters can be plotted as a single plot if they channels are declared as RGB channels (red, green blue), see ?RGB

library(terra)
## terra 1.7.62
b <- rast(system.file("ex/logo.tif", package="terra"))
nlyr(b)
## [1] 3
RGB(b)
## [1] 1 2 3
plot(b)

image0

You can also use the a number of other plotting functions with a terra object as argument, including hist, persp, contour, and density. See the help files for more info.