[英]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)
Example:例子:
You can use tm_scale_bar
from the tmap
package to add the scale bar to a map.您可以使用tm_scale_bar
中的tmap
将比例尺添加到 map。 Additionally, you might need sf
to convert mun
from SpatVector
to sf
(a format usable by tmap).此外,您可能需要sf
将mun
从SpatVector
转换为sf
(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"))
Another option is using the libraries ggspatial
and tidyterra
.另一种选择是使用库ggspatial
和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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.