首页 > 解决方案 > 使用 ggplot (geom_sf) 并排排列两个空间地图

问题描述

我想安排两张地图,一张带有给定坐标限制的北美地图和一张带有预定义坐标限制的欧洲地图。地图是用 ggplot 的geom_sf().

地图应并排排列成一排。排列的地图应具有相同的高度(每个地图的宽度可能会相应调整)......我非常接近我想要的,但我不确定如何正确调整高度以使地图匹配。

这里是代码示例:

### Plot Europe an North America side by side
library(gridExtra)
library(ggplot2)
library(rnaturalearth)
library(ggspatial)

### Load country maps
europe <- ne_countries(scale = "medium", returnclass = "sf",
                       continent = "europe")
world <- ne_countries(scale = "medium", returnclass = "sf")


### Plot maps for continents
Europe <- ggplot(data = world) +
  geom_sf(fill="grey70",color="grey90",size=0.3) +
  coord_sf(xlim = c(-12, 33), ylim = c(40, 65), expand = F,datum = NA)+
  theme_minimal()+
  theme(axis.title=element_blank(),
        plot.background = element_rect(colour = "grey50", fill=NA),
        plot.margin = unit(c(0, 0, 0, 0), "null"))
  
NorthAmerica <- ggplot(data = world) +
  geom_sf(fill="grey70",color="grey90",size=0.3) +
  coord_sf(xlim = c(-127, -52), ylim = c(15, 65), expand = F, datum = NA)+
  theme_minimal()+
  theme(axis.title=element_blank(),
        plot.background = element_rect(colour = "grey50", fill=NA),
        plot.margin = unit(c(0, 0, 0, 0), "null"))

## Arrange maps side by side (e.g. via grid.arrange)
grid.arrange(NorthAmerica, Europe, nrow = 1)

以及高度稍微不匹配的结果: 不匹配的地图

我可以做的是手动调整 中的限制coord_sf,但我可能永远无法达到真正适合的高度,因为限制是以度而不是公制单位给出的(这需要适当的地理空间转换)。

标签: rggplot2layoutcoordinatesgeospatial

解决方案


一种选择是切换到patchwork包:

library(patchwork)

NorthAmerica + Europe


推荐阅读