首页 > 解决方案 > 有没有办法在 R 中使用 ggplot 在地图的两个区域之间添加空间?

问题描述

我正在努力解决一个问题。我想通过在它们之间添加一些空间来分隔两个国家。例如,这个想法是通过仍然显示每个国家但在每个国家之间有预定义的空间来爆炸欧洲。

我正在为我正在工作的项目使用 R 和 ggplot,直到现在我试图在网上寻找一些答案,但找不到任何东西。您可以通过更改大小来获得一些东西,但该解决方案也意味着地图细节的损失。

如果你能帮忙那就太好了!

标签: rggplot2mapsspatial

解决方案


有点 hacky,但您可以尝试将多边形缩放到 <100% 的原始值...

library(sf)
library(magrittr)

样本数据

#read shapefile with country polygons
# source: http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
map <- st_read( "./data/countries/TM_WORLD_BORDERS-0.3.shp" )
#filter out some relevant countries
benelux <- c("Belgium", "Luxembourg", "Netherlands")
map <- map %>% filter( NAME %in% benelux ) 

#what do we have?
ggplot() + geom_sf( data = map ) 

在此处输入图像描述

代码

#scale the polygons to 75% of original
#extract geometry
map.sfc = st_geometry(map)
#get centroids
map.centroid = st_centroid(map.sfc)
#recalculate geometry, scale to 75%
map.scale = ( map.sfc - map.centroid ) * 0.75 + map.centroid
#replace original geoemtry by recalculated geometry. set crs back to WGS84
map.scale_sf = st_set_geometry(map, map.scale) %>% st_set_crs( 4326 )

#ewhat do we have now?
ggplot() + geom_sf( data = map.scale_sf ) 

在此处输入图像描述


推荐阅读