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)
# create some data
d <- matrix(rnorm(120),12,10)
# cluster it
hr <- hclust(as.dist(1-cor(t(d), method="pearson")), method="complete")
# define some clusters
mycl <- cutree(hr, h=max(hr$height/1.5))
# get a color palette equal to the number of clusters
clusterCols <- rainbow(length(unique(mycl)))
# create vector of colors for side bar
myClusterSideBar <- clusterCols[mycl]
# choose a color palette for the heat map
myheatcol <- rev(redgreen(75))
# draw the heat map
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)
# cutree returns a vector of cluster membership
# in the order of the original data rows
# examine it
mycl
# examine the cluster membership by it's order
# in the heatmap
mycl[hr$order]
# grab a cluster
cluster1 <- d[mycl == 1,]
# or simply add the cluster ID to your data
foo <- cbind(d, clusterID=mycl)
# examine the data with cluster ids attached, and ordered like the heat map
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.