[BioC] Generating heat map of pre-organised data in matrix without clustering using ggplot2 or heatmap.2

Jakub Stanislaw Nowak jakub.nowak at ed.ac.uk
Fri Aug 8 13:06:01 CEST 2014

Hello bioconductor friends,

I am trying to generate heatmaps for my data and run in following problems. I would be grateful for some suggestions about heat map code. 

I arranged matrix for the heatmap by myself and I would like to keep this not changed during heat map plotting. I tried both heatmap.2 (gplot) and geom_tile (ggplot2)

1. So when I used heatmap.2 from (gpot) it keeps my matrix sort but I have problems making the layout look nice. Below is the code I used.
Can you suggest how to:
a) make rows and columns separated and rows bigger so it can read easier.
b) is there a way for changing  x label for key so it includes min and max values for colour code.
c) can the density plot be generated above the key

Preparing table for the heatmap.2 from (qplot)
data.heatmap <- data.sorted.merged[,-c(2:5,8),drop=FALSE]
data.heatmap <- data.heatmap[,-1]
data.heatmap.matrix <- data.matrix(data.heatmap)
Generating heatmap with heatmap.2
if (!require("gplots")) {
 install.packages("gplots", dependencies = TRUE)
if (!require("RColorBrewer")) {
 install.packages("RColorBrewer", dependencies = TRUE)
mypalette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)
#subseting default gradient with custom one for better visualisation
colbreaks <- c(seq(-6,-3,length=110), # for red
seq(-3,3,length=90), # for yellow
seq(3.10,length=100)) # for green
  main = "Correlation", # heat map title
  notecol="black",      # change font color of cell labels to black
  density.info="density",  # turns density plot inside color legend
  trace="none",# turns off trace lines inside the heat map
  colsep, rowsep, sepcolor="white", sepwidth=c(1,1), #doesn't really show any separation
  margins =c(1,15),     # widens margins around plot doesn't do much 
  col=mypalette,       # use on color palette defined earlier 
  breaks=colbreaks,    # enable color transition at specified limits
  Rowv="NA",     # only draw a row dendrogram
  Colv="NA",    # turn off column clustering
  key.title=NA, # no title

2. I also tried to use geom_tile from (ggplot2) to generate this heat map. Here layout looks better but I am loosing my organisation of the matrix. Below is a code I used to make a plot
Can you suggest:
1. how to switch off clustering in ggplot2

Generating heatmap with ggplot2 

if (!require("reshape2")) {
 install.packages("reshape2", dependencies = TRUE)
if (!require("plyr")) {
 install.packages("plyr", dependencies = TRUE)
if (!require("scales")) {
 install.packages("scales", dependencies = TRUE)
if (!require("ggplot2")) {
 install.packages("ggplot2", dependencies = TRUE)
data.sorted.merged.test <- data.sorted.merged[,-c(2:5,8)]
data.heatmap2 <- melt(data.sorted.merged.test)
data.heatmap2 <- ddply(data.heatmap2, .(variable), transform,rescale = rescale(value))
ggplot(data.heatmap2, aes(variable, ID))+ geom_tile(aes(fill=rescale), colour="white") + 
  scale_fill_gradient(low="white", high="red", breaks=colbreaks)

Also I can attach png files of the heat map if that can help.

Thanks very much for your help,


The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

More information about the Bioconductor mailing list