【问题标题】:Arrange two spatial maps side by side using ggplot (geom_sf)使用 ggplot (geom_sf) 并排排列两个空间地图
【发布时间】:2021-12-02 17:44:12
【问题描述】:

我想安排两张地图,一张带有给定坐标限制的北美地图和一张带有预定义坐标限制的欧洲地图。地图是使用 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 中的限制,但我可能永远无法达到真正适合的高度,因为限制以度数而不是公制单位给出(这需要适当的地理空间转换)。

【问题讨论】:

    标签: r ggplot2 layout coordinates geospatial


    【解决方案1】:

    一种选择是切换到patchwork 包:

    library(patchwork)
    
    NorthAmerica + Europe
    

    【讨论】:

      猜你喜欢
      • 2017-08-06
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2011-04-09
      • 2014-09-24
      相关资源
      最近更新 更多