I came across this answer from @baptiste that uses gridGraphics
package in R to plot multiple heatmaps. https://stackoverflow.com/a/31768236/11696009
But while I was able to recreate the example (obviously), I need to apply it to my own unique condition. I have an excel workbook with 6 sheets in it. I want to plot each sheet as a separate heatmap so that all 6 heatmaps are plotted in a 3x2 grid (3 heatmaps arranged one below the other).
I am very new to R but I gather if I am to able to pass all these sheets to arr[[]] here, I might be able to use this code. Please help me on how to do that.
This is the code that I am trying to adapt.
library(gridGraphics)
library(grid)
grab_grob <- function(){
grid.echo()
grid.grab()
}
arr <- replicate(4, matrix(sample(1:100),nrow=10,ncol=10), simplify = FALSE)
library(gplots)
gl <- lapply(1:4, function(i){
heatmap.2(arr[[i]], dendrogram ='row',
Colv=FALSE, col=greenred(800),
key=FALSE, keysize=1.0, symkey=FALSE, density.info='none',
trace='none', colsep=1:10,
sepcolor='white', sepwidth=0.05,
scale="none",cexRow=0.2,cexCol=2,
labCol = colnames(arr[[i]]),
hclustfun=function(c){hclust(c, method='mcquitty')},
lmat=rbind( c(0, 3), c(2,1), c(0,4) ), lhei=c(0.25, 4, 0.25 ),
)
grab_grob()
})
grid.newpage()
library(gridExtra)
grid.arrange(grobs=gl, ncol=2, clip=TRUE)
Thanks.
edit: So, I added some lines and I got 6 plots (3 in each row) but it only plots two graphs - the first in the list and the last one. i.e the heatmap for 1st sheet goes in the first row (repeated thrice) and that of the 6th sheet goes in the second row (repeated thrice).
for (i in 6) {
arr = as.data.frame(SheetList[i])
g1 <- lapply(1:6, function(j){
heatmap.2(as.matrix(arr[2:7]), dendrogram ='none',
Colv=FALSE, Rowv = FALSE,
key=FALSE, keysize=1.0, symkey=FALSE, density.info='none',
trace='none',
scale="none",cexRow=0.2,cexCol=0.9,
breaks = col_breaks,col=my_palette,
labRow = arr[,1],
hclustfun=function(c){hclust(c, method='mcquitty')},
lmat=rbind(c(0, 3), c(2,1), c(0,4)), lhei=c(0.25, 4, 0.25),
)
grab_grob()
})
}
grid.newpage()
grid.arrange(grobs = g1, ncol=3, clip=TRUE)