Skip to content

how to colloct legend from grobs? #160

@xjsun1221

Description

@xjsun1221

my orginal require is splicing a grob object and a ggplot2 object,but get some error
then i tried two ways to fix it:

first way: covert pheatmap to ggplot by ggplotify::as.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')
image

plot_layout(guides = 'collect') doesn't work
##second way :convert ggplot to 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')

no error but plot_layout(guides = 'collect') still not work
image

#ggplot2 object convert to grob then turn back to ggplot2 ,it doesn't work neither。
p2 + p2 + plot_layout(guides = 'collect')
as.ggplot(p2_2) +as.ggplot(p2_2) +plot_layout(guides = 'collect')

Activity

thomasp85

thomasp85 commented on Jun 17, 2020

@thomasp85
Owner

I don't think you can expect patchwork to work with ggplotify etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @thomasp85@xjsun1221

        Issue actions

          how to colloct legend from grobs? · Issue #160 · thomasp85/patchwork