我正在尝试使用 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")
最佳答案
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
我在上面提到的 patchwork github 问题线程中引用了这个答案。
关于r - 如何使用 R 中 patchwork 包中的 inset_element() 函数嵌入 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65208689/