# Maps¶

Like for other plots, there are different approaches in R to make maps. You can use “base plot” in many cases. Alternatively use levelplot, either via the spplot function (implemented in sp and raster) or via the rasterVis package.

Here are some brief examples about making maps. You can also look elsewhere on the Internet, like here, or this for spplot and rasterVis.

## Vector data¶

### Base plots¶

library(terra)
p <- vect(system.file("ex/lux.shp", package="terra"))
plot(p)


n <- size(p)
plot(p, col=rainbow(n))


One colour per region (NAME_1)

u <- unique(p$NAME_1) u ## [1] "Diekirch" "Grevenmacher" "Luxembourg" m <- match(p$NAME_1, u)
plot(p, col=rainbow(n)[m])


#text(p, 'NAME_2', cex=.75, halo=TRUE)


### spplot¶

library(raster)
spplot(p, "AREA")
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.
## Warning in SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = L2, : If the coords argument is a SpatialPoints object, set its CRS first;
##   the proj4string argument to this function is ignored.


## Raster¶

Example data

library(terra)
b <- rast(system.file("ex/logo.tif", package="terra"))
r <- rast(p, res=0.01 )
values(r) <- 1:ncell(r)
r <- mask(r, p)


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. 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 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 object, all layers are plotted (up to 16), unless the layers desired are indicated with an additional argument.

plot(r)
plot(p, add=TRUE)


image does not provide a legend and that can be advantageous in some cases.

image(r)
plot(p, add=TRUE)


Multi-layer Raster objects can be plotted as individual layers

plot(b)


They can also be combined into a single image, by assigning individual layers to one of the three color channels (red, green and blue):

plotRGB(b, r=1, g=2, b=3)


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