如何使用 R 中 patchwork 包中的 inset_element() 函数嵌入地图
How to embed a map using the inset_element() function from the patchwork package in R
我正在尝试使用 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")
inset_element
似乎是 patchwork 中的一个新功能,我想你已经发现了一个错误,它在 set_panel_dimensions
中也有 affected other users. The error is thrown in this line,只有当你尝试打印(而不是创建)这个拼凑物对象:
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 问题线程中引用了这个答案。
我正在尝试使用 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")
inset_element
似乎是 patchwork 中的一个新功能,我想你已经发现了一个错误,它在 set_panel_dimensions
中也有 affected other users. The error is thrown in this line,只有当你尝试打印(而不是创建)这个拼凑物对象:
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 问题线程中引用了这个答案。