将国家 shapefile 彼此相邻地绘制以保持比例
Plotting country shapefiles next to each other preserving scale
我想做什么
使用来自 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()
我想做什么
使用来自 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()