0

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)

This is the plot I get

Ray
  • 56
  • 1
  • 7

1 Answers1

0

For those who might need it.

# load and install necessary packages
install.packages("pacman")
library(pacman)
pacman::p_load(gridGraphics,grid,gridExtra,gplots,lubridate, install = TRUE)

file = "something.xlsx"

## load all sheets
sheets <- openxlsx::getSheetNames(file)
SheetList <- lapply(sheets,openxlsx::read.xlsx,xlsxFile=file)
names(SheetList) <- sheets


## create color ramp and colour breaks
col_breaks <- c(-2,-1.5,-1,0,1,1.5,2) #provide col breaks
my_palette<-colorRampPalette(c("red4","red1","darkorange2","gold2",
                               "yellow4","yellow",
                               "chartreuse3","green4")) #color ramp

#Define a new function to create Row labels
#I have six heatmaps; three with same date row 
#First three heatmap start from 1982-2017 and last three from 2021-2100
rname <- function(i){
  if (i <= 3)
    rname = seq(as.Date("1982-01-01"), as.Date("2017-12-01"), by = "months")
  else
    rname = seq(as.Date("2021-01-01"), as.Date("2100-12-01"), by = "months")
}

grab_grob <- function(){
  grid.echo()
  grid.grab()
}

g1 = lapply(1:6, function(i) {
  arr = as.data.frame(SheetList[i])
  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.7,cexCol=0.9,
            breaks = col_breaks,col=my_palette,
            labRow = format(ymd(rname(i)),"%b-%Y"),
            labCol = c("spei-3", "spei-6", "spei-9", "spei-12", "spei-15", "spei-24"),
            colsep = 0:3, sepwidth = c(0.01),
            sepcolor = c("grey")
  )

  grab_grob()
}
)


grid.newpage()
title1=textGrob("MAIN TITLE", gp=gpar(fontface="bold"), vjust = 0.7)
grid.arrange(grobs = g1, ncol = 3, clip = TRUE, top = title1)
Ray
  • 56
  • 1
  • 7