ggheat : a ggplot2 style heatmap function

[This article was first published on rforcancer, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I hope the code here is fairly self-explanatory with the inset annotations. I feel this is just a bit ‘prettier’ than heatmap.2 and has for me the right balance of options and extensibility. I have also found it difficult to produce high quality plots with heatmap.2- whereas ggplots especially with RStudio assistance in resizing PNG turn out better IMHO.

## m=matrix(data=sample(rnorm(100,mean=0,sd=2)), ncol=10)
## this function makes a graphically appealing heatmap (no dendrogram) using ggplot
## whilst it contains fewer options than gplots::heatmap.2 I prefer its style and flexibility
ggheat=function(m, rescaling='none', clustering='none', labCol=T, labRow=T, border=FALSE, 
heatscale= c(low='blue',high='red'))
  ## the function can be be viewed as a two step process
  ## 1. using the rehape package and other funcs the data is clustered, scaled, and reshaped
  ## using simple options or by a user supplied function
  ## 2. with the now resahped data the plot, the chosen labels and plot style are built
  ## you can either scale by row or column not both! 
  ## if you wish to scale by both or use a differen scale method then simply supply a scale
  ## function instead NB scale is a base funct
      m=scale(m, center=T)
  ## I have supplied the default cluster and euclidean distance- and chose to cluster after scaling
  ## if you want a different distance/cluster method-- or to cluster and then scale
  ## then you can supply a custom function 
    m=m[hclust(dist(m))$order, ]
    m=m[hclust(dist(m))$order ,hclust(dist(t(m)))$order]
	## this is just reshaping into a ggplot format matrix and making a ggplot layer
  melt.m=cbind(rowInd=rep(1:rows, times=cols), colInd=rep(1:cols, each=rows) ,melt(m))
  ## add the heat tiles with or without a white border for clarity
    g2=g+geom_rect(aes(xmin=colInd-1,xmax=colInd,ymin=rowInd-1,ymax=rowInd, fill=value),colour='white')
    g2=g+geom_rect(aes(xmin=colInd-1,xmax=colInd,ymin=rowInd-1,ymax=rowInd, fill=value))
  ## add axis labels either supplied or from the colnames rownames of the matrix
    g2=g2+scale_x_continuous(breaks=(1:cols)-0.5, labels=colnames(m))
    g2=g2+scale_x_continuous(breaks=(1:cols)-0.5, labels=rep('',cols))
    g2=g2+scale_y_continuous(breaks=(1:rows)-0.5, labels=rownames(m))	
    g2=g2+scale_y_continuous(breaks=(1:rows)-0.5, labels=rep('',rows))	
  ## get rid of grey panel background and gridlines
  g2=g2+opts(panel.grid.minor=theme_line(colour=NA), panel.grid.major=theme_line(colour=NA),
  panel.background=theme_rect(fill=NA, colour=NA))
  ## finally add the fill colour ramp of your choice (default is blue to red)-- and return
  return(g2+scale_fill_continuous("", heatscale[1], heatscale[2]))
  ## NB because ggheat returns an ordinary ggplot you can add ggplot tweaks post-production e.g. 
  ## data(mtcars)
  ## x= as.matrix(mtcars)
  ## ggheat(x, clustCol=T)+ opts(panel.background=theme_rect(fill='pink'))

Here is quick example with the same colourscheme as gplots::heatmap.2

ggheat(x, clustering='column', rescaling='row', heatscale=c(low='red', high='yellow'))


To leave a comment for the author, please follow the link and comment on their blog: rforcancer. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)