绘制具有固定尺寸和布局的多个国家地图

发布于 2025-02-12 18:40:44 字数 1305 浏览 0 评论 0原文

我试图将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

enter image description here

Whereas the desired output would be like this (with the maps all a similar size):

enter image description here

Any pointers would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌吟 2025-02-19 18:40:44

当我必须导出地图时,这是我经常使用的黑客攻击。基本想法是不要直接绘制或导出地图,而是首先通过patchwork :: inset_element将其添加到空的背景图中。

对于第一个图,我从背景图中删除了them_void,这表明所有子图实际上都具有相同大小的独立于地图的大小:

library(tidyverse)
library(sf)
library(rnaturalearth)
library(patchwork)

plist <- dplyr::lst(p1, p2, p3)

plist <- lapply(plist, function(x) {
  ggplot() +
    geom_blank() +
    #theme_void() +
    inset_element(x, left = 0, right = 1, top = 1, bottom = 0)
})

# combine plots
(plist$p1 + plist$p1 + plist$p1) /
  (plist$p2 + plot_spacer() + plist$p2) /
  (plot_spacer() + plot_spacer() + plist$p3)

plist <- dplyr::lst(p1, p2, p3)

plist <- lapply(plist, function(x) {
  ggplot() +
    geom_blank() +
    theme_void() +
    inset_element(x, left = 0, right = 1, top = 1, bottom = 0)
})

# combine plots
(plist$p1 + plist$p1 + plist$p1) /
  (plist$p2 + plot_spacer() + plist$p2) /
  (plot_spacer() + plot_spacer() + plist$p3)

​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:

library(tidyverse)
library(sf)
library(rnaturalearth)
library(patchwork)

plist <- dplyr::lst(p1, p2, p3)

plist <- lapply(plist, function(x) {
  ggplot() +
    geom_blank() +
    #theme_void() +
    inset_element(x, left = 0, right = 1, top = 1, bottom = 0)
})

# combine plots
(plist$p1 + plist$p1 + plist$p1) /
  (plist$p2 + plot_spacer() + plist$p2) /
  (plot_spacer() + plot_spacer() + plist$p3)

And here is the final plot using theme_void for the background plots too:

plist <- dplyr::lst(p1, p2, p3)

plist <- lapply(plist, function(x) {
  ggplot() +
    geom_blank() +
    theme_void() +
    inset_element(x, left = 0, right = 1, top = 1, bottom = 0)
})

# combine plots
(plist$p1 + plist$p1 + plist$p1) /
  (plist$p2 + plot_spacer() + plist$p2) /
  (plot_spacer() + plot_spacer() + plist$p3)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文