如何在 R 中合并两个地图?

pom*_*mus 10 maps plot r ggplot2

我在下面提供了示例代码。我的问题涉及将“map2”放置在“map1”左上角的正方形中,并添加从“map2”到“map1”上特定位置的箭头。我搜索了该网站,但最常讨论的主题与合并两个数据层有关。

library (tidyverse)
  library (rnaturalearth)
  world <- rnaturalearth::ne_countries(scale = "medium", returnclass = "sf")

  map1<- ggplot(data = world) +
    geom_sf() +
    #annotation_scale(location = "bl", width_hint = 0.2) +
    #annotation_north_arrow(location = "tr", which_north = "true",
    #                     pad_x = unit(0.83, "in"), pad_y = unit(0.02, "in"),
    #                   style = north_arrow_fancy_orienteering) +
    coord_sf(xlim = c(35, 48), ylim=c(12, 22))+
    xlab("Longtitude")+
    ylab("Latitude")


  map2<- ggplot(data = world) +
    geom_sf() +
    #annotation_scale(location = "bl", width_hint = 0.2) +
    #annotation_north_arrow(location = "tr", which_north = "true",
    #                     pad_x = unit(0.83, "in"), pad_y = unit(0.02, "in"),
    #                   style = north_arrow_fancy_orienteering) +
    coord_sf(xlim = c(5, 45), ylim=c(5, 45))+
    xlab("Longtitude")+
    ylab("Latitude")

  map2
Run Code Online (Sandbox Code Playgroud)

All*_*ron 13

另一种拼凑解决方案是在插图中突出显示感兴趣的区域。对我来说这看起来比箭头更好:

library(patchwork)

map1 + 
  theme(panel.background = element_rect(fill = "white")) +
  inset_element(map2 + 
                  annotate("rect", ymin = 12, ymax = 22,
                           xmin = 35, xmax = 48, color = "red", fill = NA) +
                  theme_void() + 
                  theme(plot.background = element_rect(fill = "white"),
                        panel.border = element_rect(fill = NA, linewidth = 2)), 
                0, 0.6, 0.4, 0.98)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Maë*_*aël 8

您可以使用ggmagnifygeom_magnify

remotes::install_github("hughjonesd/ggmagnify")
library(ggmagnify)

from <- c(xmin = 35, xmax = 48, ymin = 12, ymax = 22)
to <- c(xmin = 51, xmax = 88, ymin = -20, ymax = 10)

ggplot(data = world) +
  geom_sf() +
  coord_sf(xlim = c(35, 89), ylim=c(-20, 25))+
  xlab("Longitude")+
  ylab("Latitude") + 
  theme_bw() +
  ggmagnify::geom_magnify(from = from, to = to, expand = 0)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


bre*_*auv 7

patchwork您可以使用具有功能的令人惊叹的包来做到这一点inset_element()。请注意,我稍微更改了主题以map2删除所有轴刻度和标签,但您不必:

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

world <- rnaturalearth::ne_countries(scale = "medium", returnclass = "sf")

map1 <- ggplot(data = world) +
  geom_sf() +
  coord_sf(xlim = c(35, 48), ylim=c(12, 22))+
  xlab("Longtitude")+
  ylab("Latitude")

map2 <- ggplot(data = world) +
  geom_sf() +
  coord_sf(xlim = c(5, 45), ylim=c(5, 45))+
  xlab("Longtitude")+
  ylab("Latitude") +
  theme_void() +
  theme(
    panel.border = element_rect(color = "black", fill = "transparent")
  )

map1 + inset_element(map2, left = 0.05, bottom = 0.6, right = 0.3, top = 1)
Run Code Online (Sandbox Code Playgroud)


Til*_*ill 6

该包可以通过多种方式cowplot组装s。\n可用于在绘图顶部绘制箭头。ggplotggplot::annotate()

\n

下面的例子map1首先绘制。在此之上,在左上角,我们添加一个空矩形,即 的 \xe2\x80\x9ccanvas\xe2\x80\x9d map2。最后,\n我们添加带有 的箭头annotate()。您\xe2\x80\x99 可能想要调整\n元素的坐标和大小。

\n

将绘图\n尺寸固定为正方形可能有助于使元素的位置保持一致。\n您可以通过使用ggsave()相同的高度和\n宽度值来实现这一点。

\n
library(cowplot)\n\nggdraw(clip = "on") +\n  draw_plot(map1 + theme_void()) +\n  draw_grob(\n    grid::rectGrob(),\n    x = 0.08,\n    y = .8,\n    width = .2,\n    height = .2\n  ) +\n  draw_plot(\n    map2 + theme_void(),\n    x = 0.08,\n    y = .8,\n    width = .2,\n    height = .2\n  ) +\n  annotate(\n    "segment",\n    x = 0.28,\n    xend = .55,\n    y = 0.8,\n    yend = 0.2,\n    colour = "orange",\n    linewidth = 2,\n    arrow = arrow()\n  )\n
Run Code Online (Sandbox Code Playgroud)\n

\n


asd*_*-tm 5

这是一个封装grid选项:

library(grid)

zoomed = viewport(
  x = .2,
  y = .8,
  width = .4,
  height = .4
)
regular = viewport(
  x = .5,
  y = .5,
  width = 1.0,
  height =1.0
)

grid.newpage()

print(map1, vp = regular)
print(map2, vp = zoomed)

grid.lines(x=c(0.6, 0.35),
              y=c(0.4,0.78),
              gp=gpar(col=1:5, lwd=3),
           arrow = grid::arrow()
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想您需要在缩放视口中抑制 x 和轴标签(调整map2),但这不是问题,不是吗?


小智 2

只需添加一个cowplot选项:

final <-
  ggdraw(map1) +
  draw_plot(map2, x = 0.7, y = .63, width = .3, height = .3)+ 
  geom_segment(aes(x = 0.92, y = 0.75, xend = 0.5, yend = 0.4),    
               arrow = arrow(length = unit(0.5, "cm"))) 



Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这有点烦人,因为你不能像拼凑一样使用原始坐标,所以需要一些尝试和错误 - 但我无法找到一种拼凑的方法来让箭头从插图到整个地图并且在两个图层上都可见。


应该注意的是,我编辑了插图,使其没有图例并且有边框:

map2<- ggplot(data = world) +
  geom_sf() +
  #annotation_scale(location = "bl", width_hint = 0.2) +
  #annotation_north_arrow(location = "tr", which_north = "true", 
  #                     pad_x = unit(0.83, "in"), pad_y = unit(0.02, "in"),
  #                   style = north_arrow_fancy_orienteering) +
  coord_sf(xlim = c(5, 45), ylim=c(5, 45))+
theme_void() + 
  theme(panel.border = element_rect(color = "black", linewidth=1, fill = NA))
map2 
Run Code Online (Sandbox Code Playgroud)