【问题标题】:How to create choropleth world map with insets where necessary in R如何在R中必要时创建带有插图的等值线世界地图
【发布时间】:2020-11-02 12:54:36
【问题描述】:

我希望创建 choropleth 世界地图,其插图显示世界地图中难以辨别的小区域。以下是我希望绘制的世界地图示例:

上图来自Lance的一篇文章,"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 代码可以为这些小位置创建插图?

【问题讨论】:

标签: r ggplot2 maps sf


【解决方案1】:

我认为最好的策略是单独制作情节,然后将它们拼接在一起。这是一个快速而肮脏的例子。

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 月 3 日创建

要使图很好地平铺,您必须确保所有插图都具有正确的纵横比。您可以通过相应地设置插图的 x 和 y 限制来做到这一点。我在这里设置了非投影坐标的限制,以便快速将它们组合在一起,但是您可以通过在投影坐标中设置限制来更好地控制确切的纵横比(无论如何,这是您在当前发布的 ggplot2 版本中唯一可以做的事情)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2021-04-05
    • 2022-10-15
    • 1970-01-01
    相关资源
    最近更新 更多