听说你想把基础包画的图和ggplot2的图拼到一起
2021-07-27 本文已影响0人
小洁忘了怎么分身
前面讲过优秀的patchwork,可以实现ggplot2里面花样百出的拼图。但它仅仅使用与ggplot2,如果确实需要将ggplot2的图和别的图拼到一起,它不太行。
library(ggplot2)
library(patchwork)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()+theme_classic()
p

plot(iris[,1],iris[,4])

拼不了。
姜还是老的辣
搬出cowplot,recordPlot函数可以把画板上的图存下来,完美解决基础包画图不能赋值的问题。
library(cowplot)
p2 = recordPlot()
plot_grid(p,p2)

如果是热图和ggplot2拼图
p3 = pheatmap::pheatmap(iris[,1:4])
p3

p+ggplotify::as.ggplot(p3)

妥。