Search code examples
rterra

How to insert a scale on a map in R


I would like to know if it is possible to generate an equal scale of the attached image, highlighted in red

library(terra)
library(geodata)
brz <- geodata::gadm("Brazil", level=2, path=".")
mun <- brz[brz$NAME_2 == "Castro", ]
plot(mun, main="", axes=FALSE)

enter image description here

Example:

enter image description here


Solution

  • You can use tm_scale_bar from the tmap package to add the scale bar to a map. Additionally, you might need sf to convert mun from SpatVector to sf (a format usable by tmap).

    library(tmap)
    library(sf)
    
    # Transform mun to sf
    tm_shape(sf::st_as_sf(mun)) +
      # Draw as polygon with only borders
      tm_borders() +
      # Add scale bar
      tm_scale_bar(position = c("LEFT", "BOTTOM"))
    

    map