R – Plotting a Heatmap with Dendrogram and Distance Matrix

clusteringdata visualizationr

I have dendrogram and a distance matrix. I wish to compute a heatmap — without re-doing the distance matrix and clustering.

Is there a function in R that permits this?

Best Answer

I don't know a specific function for that. The ones I used generally take raw data or a distance matrix. However, it would not be very difficult to hack already existing code, without knowing more than basic R. Look at the source code for the cim() function in the mixOmics package for example (I choose this one because source code is very easy to read; you will find other functions on the Bioconductor project). The interesting parts of the code are l. 92-113, where they assign the result of HC to ddc, and around l. 193-246 where they devised the plotting regions (you should input the values of your distance matrix in place of mat when they call image()). HTH

Edit

A recent Google search on a related subject lead me to dendrogramGrob() from the latticeExtra package. Assuming you already have your sorted dendrogram object, you can skip the first lines of the example code from the on-line help and get something like this (here, with the mtcars dataset):

enter image description here