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))