Home > Enterprise >  How to insert a scale on a map in R
How to insert a scale on a map in R

Time:08-22

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

CodePudding user response:

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

CodePudding user response:

Another option is using the libraries ggspatial and tidyterra.

library(ggspatial)
library(tidyterra)

ggplot()  
  geom_spatvector(data = mun)   
  annotation_scale()   # add scale
  annotation_north_arrow(location = "br", which_north = "true")   # add arrow
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.text.y=element_blank(),  #remove y axis labels
    axis.ticks.y=element_blank(),  #remove y axis ticks
    axis.text.x=element_blank(),  #remove x axis labels
    axis.ticks.x=element_blank()  #remove x axis ticks
  )

enter image description here

  • Related