3

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

尝试插入地图时出现此错误:

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

在此处输入图像描述

4

1 回答 1

2

inset_element似乎是拼凑而成的新功能,我认为您发现了一个也影响其他用户的错误。错误在 中的这一行中抛出,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

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

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

fail

在此处输入图像描述

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

于 2020-12-09T00:52:35.987 回答