Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a pheatmap in arrangeGrob

Tags:

r

grob

pheatmap

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)
like image 991
Xizam Avatar asked Sep 20 '16 09:09

Xizam


1 Answers

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

like image 133
Mehrad Mahmoudian Avatar answered Nov 09 '22 12:11

Mehrad Mahmoudian