结合 tmap 和 ggplot

Jan*_*ana 3 r ggplot2 cowplot tmap

我正在尝试将test1(用 tmap 制作的地图)与test2(用 ggplot2 制作的图)结合起来,使用cowplot::plot_grid。

library(ggplot2)
library(tmap)
library(cowplot)
library(ggplotify)

test2 <-ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point()

data("World")

test1 <- tm_shape(World) + tm_polygons("HPI")

plot_grid(test1,test2)
Run Code Online (Sandbox Code Playgroud)

tmap 似乎与cowplot 不兼容:

“在 as_grob.default(plot) 中:无法将 tmap 类的对象转换为 grob”

使用时同样的问题

tmap::tmap_arrange(test1,test2)
Run Code Online (Sandbox Code Playgroud)

“tmap_arrange(test1, test2) 中的错误:并非所有参数都是 tmap 对象”

我还尝试使用 ggplotify 中的“as.grob”等函数来转换 tmap,

test3 <- as.grob(test1)
Run Code Online (Sandbox Code Playgroud)

但运气不佳“UseMethod(“as.grob”) 中的错误:没有适用于“as.grob”的方法应用于类“tmap”的对象”

有什么建议么?

谢谢!

Mar*_*kes 6

是的,我最近添加了tmap_grob这个功能。

library(ggplot2)
library(tmap)
library(cowplot)
library(ggplotify)

test2 <-ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point()

data("World")

tm <- tm_shape(World) + tm_polygons("HPI")
test1 <- tmap_grob(tm)

plot_grid(test1,test2)
Run Code Online (Sandbox Code Playgroud)