今天给大家推荐一个非常好用的大小地图绘制工具-ggmapinset,让大家以后可以快速的绘制子地图。
ggmapinset是R语言中的一个包,它是基于ggplot2和ggmap包的扩展,用于在地图上添加缩略图。该包提供了一种简单的方法,可以在一个图形中同时显示主地图和缩略图,从而方便用户进行比较和分析。
可以使用以下命令在R中安装ggmapinset包:
install.packages("ggmapinset")
library(ggmapinset)
library(ggplot2)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
ggplot(nc) +
# this is equivalent to the following line:
# geom_sf_inset(fill = "white", map_inset = "none") +
geom_sf(fill = "white") +
geom_sf_inset(aes(fill = AREA), map_base = "none") +
geom_inset_frame() +
coord_sf_inset(inset = configure_inset(
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Bladen"]), scale = 1.5,
translation = c(-180, -50), radius = 50, units = "mi"))
Example01 of ggmapinset
ggplot(nc) +
geom_sf(aes(fill = AREA)) +
geom_inset_frame(target.aes = list(fill = "white")) +
geom_sf_inset(aes(fill = AREA), map_base = "none") +
coord_sf_inset(inset = configure_inset(
centre = st_centroid(st_geometry(nc)[nc$NAME == "Yancey"]), scale = 2,
translation = c(100, -120), radius = 50, units = "mi"))
Example02 of ggmapinset
inset1 <- configure_inset(
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Bladen"]), scale = 1.5,
translation = c(150, -50), radius = 50, units = "mi")
inset2 <- configure_inset(
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Orange"]), scale = 3,
translation = c(30, 120), radius = 30, units = "mi")
ggplot(nc) +
# base map
geom_sf_inset() +
# inset 1
geom_sf_inset(map_base = "none", inset = inset1) +
geom_inset_frame(inset = inset1, colour = "red") +
# inset 2
geom_sf_inset(map_base = "none", inset = inset2) +
geom_inset_frame(inset = inset2, colour = "blue")
Example03 of ggmapinset
更多案例和其他ggmapinset包中的其他函数,可参考:ggmapinset包官网[1]
ggmapinset 是一个非常有用的R包,它可以在地图上添加缩略图,提高图形的可读性。该包提供了一些简单的函数,可以方便地调整缩略图的大小和位置,并添加标签和注释。如果您需要在地图上显示多个区域的数据,那么ggmapinset是一个值得尝试的工具。