Question

I am using R's heatmap function to do hierarchical clustering (http://www.inside-r.org/r-doc/stats/heatmap). Is there a function that takes the object returned by heatmap and gives back the clustering you get by applying a threshold alpha to the dendrogram? A function like:

h <- heatmap(...)
clusters <- get_clusters(h, alpha=0.5)

where clusters is an array of same length as number of columns of the input dataframe, and returns a number between 0 and n-1 for each entry, where n is number of columns (assignment of columns to clusters). Does this exist in R?

Was it helpful?

Solution

You're looking for the keep.dendro argument of the heatmap function. The dendrograms are then stored in the Rowv and Colv elements of the output.

x = matrix(rnorm(25), ncol=5)
h = heatmap(x, keep.dendro=TRUE)

Next, convert the dendrogram object to a hclust object with the as.hclust function. Then, as stated in the comments, you can use cutree to get clusters.

row.clusters = as.hclust(h$Rowv)
cutree(row.clusters, k=3)  # break into k=3 clusters

Of course, you could have gotten to row.clusters by just doing

row.clusters = hclust(dist(x))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top