我正在尝试使用修补程序包中的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")
发布于 2020-12-09 00:52:35
inset_element
似乎是修补程序中的一个新特性,我认为您已经发现了一个带有受影响的其他用户的bug。错误将在这条线中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
我在上面提到的补丁github问题线程中引用了这个答案。
https://stackoverflow.com/questions/65208689
复制相似问题