r - 将国家形状文件彼此相邻绘制以保持比例
问题描述
我想做的事
使用来自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(),
ggplot(data = subset(countries, 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()
B) 使用方面
countries %>%
subset(SOVEREIGNT %in% c("Cameroon", "Ethiopia", "Democratic Republic of the Congo")) %>%
ggplot() +
geom_sf() +
facet_grid(~SOVEREIGNT)
C)只需将它们绘制在正确的位置而不移动或分离它们:
countries %>%
subset(SOVEREIGNT %in% c("Cameroon", "Ethiopia", "Democratic Republic of the Congo")) %>%
ggplot() +
geom_sf()
推荐阅读
- kotlin - Reactor - 如果未通过过滤器,如何使用 filterWhen 并仍将值传递给记录目的?
- django - 如何在 Debug = False 时记录有关 404 错误的信息
- sql - FULL JOIN 可以返回多少条最小和最大记录
- ubuntu - linux 标头占用了 EC2 机器上的大量磁盘空间。使用 rm 命令手动删除标题是否安全?
- amazon-web-services - 通过 ssh 连接到 aws rds 实例所需的参数
- sql - 如何将数据批量加载到由外键引用但数据是 Excel 格式的纯 SQL Server 表中?
- delphi - Delphi - ActionToolBar 如何获取动作执行事件的子菜单属性
- matrix - DolphinDB中的成对欧几里得距离
- java - 应用程序上下文和 url 模式如何交互?
- android - 如何将对象从函数传递到android中的oncreate方法