结合 tmap 和 ggplot

Combine tmap and ggplot

我正在尝试将使用 tmap 制作的地图 test1 与使用 ggplot2 制作的绘图 test2 结合使用 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)

tmap 似乎与 cowplot 不兼容:

”在as_grob.default(剧情)中: 无法将 class tmap 的对象转换为 grob

使用时同样的问题

tmap::tmap_arrange(test1,test2)

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

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

test3 <- as.grob(test1)

但运气不好 “UseMethod("as.grob") 错误: 没有适用于 'as.grob' 的方法应用于 class“tmap”“

的对象

有什么建议吗?

谢谢!

是的,我最近添加了 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)