微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

绘制彼此相邻的国家 shapefile 保留比例

如何解决绘制彼此相邻的国家 shapefile 保留比例

我想做什么

使用来自 Natural Earth shapefile 数据集的国家级数据,我想绘制相邻的国家以展示它们的大小差异。例如,我想在刚果民主共和国旁边绘制喀麦隆和埃塞俄比亚的图,以便让我的学生了解非洲国家的大小差异。

我的尝试

使用 R,这是我尝试过的:

使用 sf::st_read 我已将 shapefile 导入为 countries

library(sf) # Easily work with spatial objects
library(tidyverse) # Brings in GGPlot
library(gridExtra) # Allows you to easily plot multiple GGPlots

gridExtra::grid.arrange(
ggplot(data = subset(countries,SOVEREIGNT %in% "Cameroon")) + geom_sf(),ggplot(data = subset(countries,SOVEREIGNT %in% "Ethiopia")) + geom_sf(),SOVEREIGNT %in% "Democratic Republic of the Congo")) + geom_sf(),ncol = 3)

结果

结果是三个比例不匹配的地图。尽管是最大的国家,但刚果民主共和国看起来比最小的国家喀麦隆要小。

有没有什么优雅的方法来进行这种比较?

解决方法

有几个选项。

A) 提取国家,然后使用仿射运算符将它们彼此相邻

Cameroon = subset(countries,SOVEREIGNT %in% "Cameroon")
Ethiopia = subset(countries,SOVEREIGNT %in% "Ethiopia")
DRC = subset(countries,SOVEREIGNT %in% "Democratic Republic of the Congo")
buffer = 1
Cameroon = st_geometry(Cameroon) - st_centroid(Cameroon)$geometry
DRC = st_geometry(DRC) - st_centroid(DRC)$geometry 
DRC = DRC - c(st_bbox(st_geometry(DRC))['xmin'],0)
DRC = DRC + c(st_bbox(st_geometry(Cameroon))['xmax'] + buffer,0)
Ethiopia = st_geometry(Ethiopia) - st_centroid(Ethiopia)$geometry
Ethiopia = Ethiopia - c(st_bbox(Ethiopia)['xmin'],0)
Ethiopia = Ethiopia + c(st_bbox(st_geometry(DRC))['xmax'] + buffer,0)

ggplot(c(Cameroon,Ethiopia,DRC)) + geom_sf() + theme_minimal()

enter image description here

B) 使用分面

countries %>%
  subset(SOVEREIGNT %in% c("Cameroon","Ethiopia","Democratic Republic of the Congo")) %>%
  ggplot() + 
  geom_sf() +
  facet_grid(~SOVEREIGNT)

enter image description here

C) 只需将它们绘制在正确的位置,无需移动或分离它们:

countries %>%
  subset(SOVEREIGNT %in% c("Cameroon","Democratic Republic of the Congo")) %>%
  ggplot() + 
  geom_sf()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。