结合tmap和ggplot

3

我正在尝试使用cowplot::plot_grid将由tmap制作的地图test1与ggplot2制作的绘图test2组合起来。

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(plot)中: 无法将tmap类对象转换为grob"

使用时出现相同问题。

tmap::tmap_arrange(test1,test2)

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

我还尝试使用"as.grob"等函数将tmap转换为ggplotify对象,

test3 <- as.grob(test1)

但是没有成功。

"UseMethod("as.grob")出错: 对于类为“tmap”的对象,没有适用的方法"as.grob"

有什么建议吗?

谢谢!

1个回答

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)

1
谢谢Martijn,我无法期望得到更好的答案 :) - Jana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接