To get the subclusters of your data after clustering, use the vector returned by cutree as an index into your original data matrix. Here is a simple example:
library(gplots)
d <- matrix(rnorm(120),12,10)
hr <- hclust(as.dist(1-cor(t(d), method="pearson")), method="complete")
mycl <- cutree(hr, h=max(hr$height/1.5))
clusterCols <- rainbow(length(unique(mycl)))
myClusterSideBar <- clusterCols[mycl]
myheatcol <- rev(redgreen(75))
heatmap.2(d, main="Hierarchical Cluster", Rowv=as.dendrogram(hr), Colv=NA, dendrogram="row", scale="row", col=myheatcol, density.info="none", trace="none", RowSideColors= myClusterSideBar)
mycl
mycl[hr$order]
cluster1 <- d[mycl == 1,]
foo <- cbind(d, clusterID=mycl)
foo[hr$order,]
If you run the code, you'll see that mycl returns the cluster membership for each row of your original data. To see it in the order as it is plotted in the heatmap, you have order it according to the heat map index. Another caveat many find confusing, is that the first position of the vector refers to the bottom of the heat map.
Perhaps you could provide your R code to help us understand the precise nature of the problem?
I am sorry, I should have. I have found the problem.The order of the clusters on the heat map is not 1:n.Thanks for your attention!
Thank you, this was really helpful. EDIT: this was meant to be under seidel's answer.