https://github.com/thomasp85/patchwork/issues/160
热图和ggplot2 用patchwork拼到一起,图例是无法收集的。发到了大佬的github上,等待回复ing。。。(也有可能等不到)
ls("package:patchwork")
#思路一:pheatmap转为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转为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')
网友评论