r - 使用 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
,但我可能永远无法达到真正适合的高度,因为限制是以度而不是公制单位给出的(这需要适当的地理空间转换)。
解决方案
一种选择是切换到patchwork
包:
library(patchwork)
NorthAmerica + Europe
推荐阅读
- php - update_option 到 for 循环中的数组
- powershell - Start-Job - 参数的位置顺序
- php - 使用 PHP 和 PDO 搜索表单,然后在页面上显示结果
- java - 重新格式化字符串数组
- asp.net - VB.NET - 单击 DataGridView 超链接并获取单击的文本
- reporting-services - 堆叠条形图中的 SSRS 匹配颜色系列
- database - Codeigniter SUM 查询只带来一行
- jquery - 如果模态已打开,则不要进行位置重新加载
- sql - 通过键查找从一个表到另一个表的路径
- javascript - 使用表单数据处理发布请求,而不在节点中表达