绘制具有固定尺寸和布局的多个国家地图
我试图将AA系列地图(NZ,澳大利亚和阿根廷)绘制在一个网格中,其中某些部分是空的。我正在使用ggplot创建单个地图,然后拼布将网格播种。
我希望结果是一个3x3网格,在顶行上有3x Nz地图,第二行上的2倍AUS +一个空图,而第三行上的1x Argentina + 2x空图。
我的问题是这些地块没有保持3x3网格,并且国家的大小非常不同。一个示例:
library(tidyverse)
library(sf)
library(rnaturalearth)
library(patchwork)
# basic country maps
nz <- ne_countries(country = 'new zealand', type = 'countries', returnclass = "sf")
aus <- ne_countries(country = 'australia', type = 'countries', returnclass = "sf")
arge <- ne_countries(country = 'argentina', type ='countries', returnclass = "sf")
# plot nz
p1 <- ggplot() +
geom_sf(data = nz) +
theme_void()
# plot aus
p2 <- ggplot() +
geom_sf(data = aus) +
theme_void()
# plot arg
p3 <- ggplot() +
geom_sf(data = arge) +
theme_void()
# combine plots
p1 + p1 + plot_spacer() /
p2 + plot_spacer() + p2 /
plot_spacer() + plot_spacer() + p3
,而所需的输出将是这样的(与所有相似大小的地图):
任何指针都将不胜感激!
I am trying to plot a a series of maps (NZ, Australia and Argentina) in a grid, with some parts of the grid being empty. I am using ggplot to create the individual maps and then patchwork to sow together the grid.
I expect the result to be a 3x3 grid with 3x NZ maps on the top row, 2x aus + an empty plot on the second row and 1x Argentina + 2x empty plots on the 3rd row.
My problem is the plots do not maintain a 3x3 grid and the countries are very different sizes. An example:
library(tidyverse)
library(sf)
library(rnaturalearth)
library(patchwork)
# basic country maps
nz <- ne_countries(country = 'new zealand', type = 'countries', returnclass = "sf")
aus <- ne_countries(country = 'australia', type = 'countries', returnclass = "sf")
arge <- ne_countries(country = 'argentina', type ='countries', returnclass = "sf")
# plot nz
p1 <- ggplot() +
geom_sf(data = nz) +
theme_void()
# plot aus
p2 <- ggplot() +
geom_sf(data = aus) +
theme_void()
# plot arg
p3 <- ggplot() +
geom_sf(data = arge) +
theme_void()
# combine plots
p1 + p1 + plot_spacer() /
p2 + plot_spacer() + p2 /
plot_spacer() + plot_spacer() + p3
Whereas the desired output would be like this (with the maps all a similar size):
Any pointers would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我必须导出地图时,这是我经常使用的黑客攻击。基本想法是不要直接绘制或导出地图,而是首先通过
patchwork :: inset_element
将其添加到空的背景图中。对于第一个图,我从背景图中删除了
them_void
,这表明所有子图实际上都具有相同大小的独立于地图的大小:
src =“ https://i.sstatic.net/wwiif.png” alt =“”>
This is a bit of a hack which I use quite often when I have to export maps. The basic idea is to not plot or export the map directly but instead add it to an empty background plot first via
patchwork::inset_element
.For the first plot I dropped
theme_void
from the background plot which shows that all subplots are actually of the same size independent what the size of the map is:And here is the final plot using
theme_void
for the background plots too: