https://github.com/thomasp85/patchwork/issues/160
熱圖和ggplot2 用patchwork拼到一起戏仓,圖例是無法收集的苇本。發(fā)到了大佬的github上壤躲,等待回復(fù)ing绪商。坛掠。咪奖。(也有可能等不到)
ls("package:patchwork")
#思路一:pheatmap轉(zhuǎn)為ggplot
p1 <- pheatmap::pheatmap(t(iris[1:50,1:4]))
p1 = ggplotify::as.ggplot(p1$gtable)
p2 <- ggplot(mtcars) + geom_boxplot(aes(factor(cyl), disp,fill = factor(cyl)));p2
library(ggplot2)
library(patchwork)
p1 +p2 + plot_layout(guides = 'collect')
#失敗
#思路2:ggplot轉(zhuǎn)為grob
p1 <- pheatmap::pheatmap(t(iris[1:50,1:4]))
p1_1 <- p1$gtable
p2 <- ggplot(mtcars) + geom_boxplot(aes(factor(cyl), disp,fill = factor(cyl)));p2
p2_1 <- ggplot_gtable(ggplot_build(p2))
p2_2 <- ggplotGrob(p2)
p1_1 + p2_1 #error
wrap_ggplot_grob(p1_1)+wrap_ggplot_grob(p2_2) #error
wrap_elements(p1_1) + wrap_elements(p2_2)+ plot_layout(guides = 'collect')
#失敗
#ggplot2 變成grob再變回來盗忱,拼到一起仍然是無法收集圖例的。
p2 + p2 + plot_layout(guides = 'collect')
as.ggplot(p2_2) +as.ggplot(p2_2) +plot_layout(guides = 'collect')