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

6ie5vjzr  于 2023-10-13  发布在  其他
关注(0)|答案(6)|浏览(160)

我提供了下面的示例代码。我的问题涉及到将“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
xam8gpfp

xam8gpfp1#

另一个拼凑的解决方案是在插图中突出显示感兴趣的区域。我觉得这比箭还好看:

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)

yxyvkwin

yxyvkwin2#

您可以使用令人惊叹的软件包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)

sigwle7e

sigwle7e3#

您可以使用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)

pkwftd7m

pkwftd7m4#

cowplot软件包可以通过多种方式组装ggplotggplot::annotate()可用于在图的顶部绘制箭头。
下面的示例首先绘制map1。在左上角,我们添加一个空矩形,即map2的“canvas”。最后,我们添加带有annotate()的箭头。您可能需要调整元素的坐标和大小。
将绘图尺寸固定为正方形可能有助于使元素的放置保持一致。您可以使用ggsave(),并为高度和宽度设置相同的值。

library(cowplot)

ggdraw(clip = "on") +
  draw_plot(map1 + theme_void()) +
  draw_grob(
    grid::rectGrob(),
    x = 0.08,
    y = .8,
    width = .2,
    height = .2
  ) +
  draw_plot(
    map2 + theme_void(),
    x = 0.08,
    y = .8,
    width = .2,
    height = .2
  ) +
  annotate(
    "segment",
    x = 0.28,
    xend = .55,
    y = 0.8,
    yend = 0.2,
    colour = "orange",
    linewidth = 2,
    arrow = arrow()
  )

dbf7pr2w

dbf7pr2w5#

下面是一个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()
)

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

r1zk6ea1

r1zk6ea16#

添加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")))

这有点烦人,因为你不能使用原始坐标,因为你可以与拼凑,所以它需要一些尝试和错误-但我不能想出一个方法与拼凑有箭头从插图到整体Map,并在两个层可见。
应该注意的是,我编辑了插图,没有图例和边界:

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

相关问题