rgrobpheatmap

Using a pheatmap in arrangeGrob


I'm attempting to plate two plots in the same .jpg using arrangeGrob(). I've only just started learning about grids and grobs and I think I know what the problem is: pheatmap is a grid object and containing grob objects, not allowing me to put it in arrangeGrob. Is this true?

Would I somehow need to put the qplot in a grid and the pheatmap in a grid and then put those grids in a new grid?

library(grid)
library(gridExtra)
library(pheatmap)
library(ggplot2)
hmdat=rbind(c(1,2,3),
            c(3,4,5),
            c(5,6,7))
hm=pheatmap(hmdat)
qp=qplot(1,1)
lm=rbind(c(1,2,2),
         c(1,2,2))
jpeg("plots.jpg")
arrangeGrob(qp,hm, layout_matrix=lm)
dev.off()

The above code snippet runs just fine when using

arrangeGrob(qp,qp, layout_matrix=lm)

Solution

  • I'm not sure if you wanted to have 6 figures or you wanted to have two figures one with twice as wide as the other one (I tried to do minimum code change):

    library("grid")
    library("gridExtra")
    library("pheatmap")
    library("ggplot2")
    
    hmdat=rbind(c(1,2,3),
                c(3,4,5),
                c(5,6,7))
    
    hm <- pheatmap::pheatmap(hmdat)
    qp <- qplot(1,1)
    
    lm <- rbind(c(1,2,2),
             c(1,2,2))
    grid.arrange(grobs = list(qp,hm[[4]]), layout_matrix = lm)
    

    which will give you: combination of pheatmap and ggplot in a grid

    The same way you can have multiple pheatmaps side-by-side:

    library("grid")
    library("gridExtra")
    library("pheatmap")
    
    hmdat <- rbind(c(1,2,3),
                c(3,4,5),
                c(5,6,7))
    
    hm <- pheatmap::pheatmap(hmdat)
    
    lm <- rbind(c(1,2),
             c(3,3))
    grid.arrange(grobs = list(hm[[4]],
                              hm[[4]],
                              hm[[4]]),
                 layout_matrix = lm)
    

    grid of pheatmap plots

    As @hrbrmstr mentioned in the comment, you should use the 4th item in the pheatmap object. Also remember to provide grobs as list to the grid.arrange