[R] highlighting clusters in a heatmap

Levi Waldron lwaldron.research at gmail.com
Fri Jul 1 02:02:10 CEST 2011


I would like to draw horizontal or vertical lines on a heatmap to
highlight the clusters at some specified cut depth of the dendrogram.
As a hacked example, the following code would work if I could set the
coordinates of the top and bottom of the false color image correctly
(ymin and ymax), but the correct values seem to depend on the output
device and its size.  I realize that heatmaps use a 2x2 layout which
makes the coordinate system non-obvious, but the result seems very
difficult to customize.  I would appreciate any suggestions for manual
or pre-made solutions.

Example:

set.seed(2)
x <- matrix(rnorm(1000),ncol=10)  #obviously no real clusters here...

row.hclust <- hclust(dist(x))
row.dendro <- as.dendrogram(row.hclust)

heatmap(x,
        Rowv=row.dendro)
row.cut <- cutree(row.hclust,3)[row.hclust$order]
cutpoints <- which(row.cut[-1]!=row.cut[-length(row.cut)])
ymin <- par("usr")[3]    #in general incorrect
ymax <- par("usr")[4]   #in general incorrect
for (i in cutpoints){
  thisy <- ymin + (ymax-ymin)*(i-1)/nrow(x)
  abline(h=thisy,lw=3)
}



More information about the R-help mailing list