r - 如何使用 R 中 patchwork 包中的 inset_element() 函数嵌入 map

标签 r ggplot2 patchwork

我正在尝试使用 patchwork 包中的 inset_element() 函数将小 map 嵌入到大 map 中。

尝试插入 map 时出现此错误:

Error in seq.default(design$t[i], design$b[i]) : 
  'from' must be a finite number

这是一个例子:

library(patchwork)
library(sf)
library(ggplot2)
library(rnaturalearth)
world <- rnaturalearth::ne_countries(scale='medium',returnclass = 'sf')
p1 <- ggplot()+
  geom_sf(data= world)

p2 <- ggplot() + 
  geom_point(data = iris, aes(x= Petal.Width, y = Petal.Length))

# 2 maps = fail
p1 + inset_element(p1, left =0.75, right =0.95, bottom = 0.75, top =0.95)

# 1 map as "main" = success
p1 + inset_element(p2, left =0.75, right =0.95, bottom = 0.75, top =0.95)


# 1 map as "inset" = fail
p2 + inset_element(p1, left =0.75, right =0.95, bottom = 0.75, top =0.95)

packageVersion("patchwork")

enter image description here

最佳答案

inset_element 似乎是 patchwork 中的一个新功能,我想你已经发现了一个错误,它也有 affected other users .错误在 this line 中抛出在 set_panel_dimensions 中,它仅在您尝试打印(而不是创建)此拼凑对象时调用:

fail <- p2 + inset_element(p1, left =0.75, right =0.95, bottom = 0.75, top =0.95)

fail
#> Error in seq.default(design$t[i], design$b[i]) : 
#>   'from' must be a finite number

但是,我们现在可以通过将 patches 对象的布局高度和宽度设置为 1 来解决此错误:

fail$patches$layout$widths  <- 1
fail$patches$layout$heights <- 1

fail

enter image description here

我在上面提到的 patchwork github 问题线程中引用了这个答案。

关于r - 如何使用 R 中 patchwork 包中的 inset_element() 函数嵌入 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65208689/

相关文章:

R 文本挖掘 : Counting the number of times a specific word appears in a corpus?

r - 带有 Rmarkdown 的 gt 表中的数学单位

r - 如何使 ggplot 图例水平添加对象(相对于垂直)

r - 将多个图例与拼凑对齐

r - 我如何添加链接以从R Shiny应用程序在新窗口中打开pdf文件?

r - 如何提取多个下划线之间的数字

R : ggplot2 geom_segment pixelized

r - 不因式分解 x 轴标签的因素

r - plot_annotations 通过 R 中的拼凑消失