如何在 R 中的必要位置创建带插图的等值线世界地图
How to create choropleth world map with insets where necessary in R
我希望创建等值线世界地图,其中的插图显示在世界地图上难以辨别的小区域。下面是我希望绘制的那种世界地图的示例:
上图来自兰斯的一篇文章,"Global, regional, and national age-sex-specific mortality for 282 causes of death in 195 countries and territories, 1980–2017: a systematic analysis for the Global Burden of Disease Study 2017".这张图在世界地图的下方,有很多小区域被放大了插图
我可以在 R 中创建世界地图,但只能是没有插图的大地图。但我想问一下是否有现成的 R 代码可以为这些小位置创建插图?
我认为最好的策略是单独制作情节,然后将它们拼接在一起。这是一个简单粗暴的例子。
library(ggplot2) # use development version for coord limits in unprojected coordinates
library(sf) # for manipulation of simple features objects
#> Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(rnaturalearth) # for map data
library(dplyr) # for mutate()
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(cowplot) # for plot_grid()
world_sf <- ne_countries(returnclass = "sf") %>%
mutate(log_pop = log(pop_est))
# Robinson projection
crs_robin <- "+proj=robin +lat_0=0 +lon_0=0 +x0=0 +y0=0"
# base plot
base <- ggplot() +
geom_sf(data = world_sf, aes(fill = log_pop), size = 0.2) +
scale_fill_viridis_c()
# world
p1 <- base + theme_minimal() +
coord_sf(crs = crs_robin)
# theme for inset plots
theme_inset <- theme_void() +
theme(
panel.border = element_rect(colour = "black", fill = NA),
plot.margin = margin(2, 2, 2, 2)
)
# North America
p2 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(-150, -50), ylim = c(20, 70)) +
guides(fill = "none")
# Australia
p3 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(110, 155), ylim = c(-10, -45)) +
guides(fill = "none")
# UK
p4 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(-11, 2), ylim = c(49, 59)) +
guides(fill = "none")
# Island
p5 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(-25, -12), ylim = c(62, 68)) +
guides(fill = "none")
# Svalbard
p6 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(10.5, 26.5), ylim = c(75, 84)) +
guides(fill = "none")
inset_row <- plot_grid(
p2,
plot_grid(NULL, p4, p5, p6, NULL, ncol = 1),
p3,
nrow = 1, rel_widths = c(1, .4, 1)
)
plot_grid(p1, inset_row, ncol = 1)
由 reprex package (v0.3.0)
于 2020-11-03 创建
为了让绘图很好地平铺,您必须确保所有插图都具有正确的纵横比。您可以通过相应地设置插图的 x 和 y 限制来做到这一点。我在这里设置了未投影坐标的限制,以便快速将它们组合在一起,但是您可以通过设置投影坐标的限制来更好地控制确切的纵横比(无论如何,这是您在当前发布的 ggplot2 版本中唯一可以做的事情)。
我希望创建等值线世界地图,其中的插图显示在世界地图上难以辨别的小区域。下面是我希望绘制的那种世界地图的示例:
上图来自兰斯的一篇文章,"Global, regional, and national age-sex-specific mortality for 282 causes of death in 195 countries and territories, 1980–2017: a systematic analysis for the Global Burden of Disease Study 2017".这张图在世界地图的下方,有很多小区域被放大了插图
我可以在 R 中创建世界地图,但只能是没有插图的大地图。但我想问一下是否有现成的 R 代码可以为这些小位置创建插图?
我认为最好的策略是单独制作情节,然后将它们拼接在一起。这是一个简单粗暴的例子。
library(ggplot2) # use development version for coord limits in unprojected coordinates
library(sf) # for manipulation of simple features objects
#> Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(rnaturalearth) # for map data
library(dplyr) # for mutate()
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(cowplot) # for plot_grid()
world_sf <- ne_countries(returnclass = "sf") %>%
mutate(log_pop = log(pop_est))
# Robinson projection
crs_robin <- "+proj=robin +lat_0=0 +lon_0=0 +x0=0 +y0=0"
# base plot
base <- ggplot() +
geom_sf(data = world_sf, aes(fill = log_pop), size = 0.2) +
scale_fill_viridis_c()
# world
p1 <- base + theme_minimal() +
coord_sf(crs = crs_robin)
# theme for inset plots
theme_inset <- theme_void() +
theme(
panel.border = element_rect(colour = "black", fill = NA),
plot.margin = margin(2, 2, 2, 2)
)
# North America
p2 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(-150, -50), ylim = c(20, 70)) +
guides(fill = "none")
# Australia
p3 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(110, 155), ylim = c(-10, -45)) +
guides(fill = "none")
# UK
p4 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(-11, 2), ylim = c(49, 59)) +
guides(fill = "none")
# Island
p5 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(-25, -12), ylim = c(62, 68)) +
guides(fill = "none")
# Svalbard
p6 <- base + theme_inset +
coord_sf(crs = crs_robin, xlim = c(10.5, 26.5), ylim = c(75, 84)) +
guides(fill = "none")
inset_row <- plot_grid(
p2,
plot_grid(NULL, p4, p5, p6, NULL, ncol = 1),
p3,
nrow = 1, rel_widths = c(1, .4, 1)
)
plot_grid(p1, inset_row, ncol = 1)
由 reprex package (v0.3.0)
于 2020-11-03 创建为了让绘图很好地平铺,您必须确保所有插图都具有正确的纵横比。您可以通过相应地设置插图的 x 和 y 限制来做到这一点。我在这里设置了未投影坐标的限制,以便快速将它们组合在一起,但是您可以通过设置投影坐标的限制来更好地控制确切的纵横比(无论如何,这是您在当前发布的 ggplot2 版本中唯一可以做的事情)。