r - 有没有办法在 R 中使用 ggplot 在地图的两个区域之间添加空间?
问题描述
我正在努力解决一个问题。我想通过在它们之间添加一些空间来分隔两个国家。例如,这个想法是通过仍然显示每个国家但在每个国家之间有预定义的空间来爆炸欧洲。
我正在为我正在工作的项目使用 R 和 ggplot,直到现在我试图在网上寻找一些答案,但找不到任何东西。您可以通过更改大小来获得一些东西,但该解决方案也意味着地图细节的损失。
如果你能帮忙那就太好了!
解决方案
有点 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 )
推荐阅读
- delphi - Indy 10.5.8.0 + XE2 + Windows 10“地址00000000的访问冲突。读取地址00000000”
- vue.js - 定义的数组变量工作正常,但子元素在 vuejs 中不起作用
- google-apps-script - 免费和 GSuite Enterprise Google 产品的配额差异
- python - 表示 1-1 关系
- javascript - 如何在后台永远运行nodemon +
- azure - 如何为 Azure 中的多个 asp.net 核心应用程序设置一个会话?
- assembly - 添加数组时遇到问题 8086
- javascript - JavaScript:只使用 TIME 的倒数计时器
- javascript - 从 PHP 创建一个 JS 数组
- git - 新功能分支中的 Git 未跟踪文件