我认为最好的策略是单独制作情节,然后将它们拼接在一起。这是一个快速而肮脏的例子。
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 版本中唯一可以做的事情)。