Question: How To Get The Subclusters From The Object Of Hclust() Using Cutree() According To The Order On The Map Produced By Heatmap.2?
gravatar for
4.7 years ago by
liux.bio250 wrote:

Hello Biostars,

I have a question that is perhaps naive. I use hclust() to cluster my data and heatmap.2() to map, then use cutree() to get subclusters. But the order of subclusters I got from cutree() is not the same as the order visualized on the map. I know the problem is related to the order. I have read some materials but still feel confused. How to solve it?

Thank you!

ADD COMMENTlink modified 5 months ago by joneill4x10 • written 4.7 years ago by liux.bio250

Perhaps you could provide your R code to help us understand the precise nature of the problem?

ADD REPLYlink written 4.7 years ago by Malachi Griffith16k

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!

ADD REPLYlink written 4.7 years ago by liux.bio250
gravatar for seidel
4.7 years ago by
United States
seidel6.5k wrote:

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:


# 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,"none", trace="none", RowSideColors= myClusterSideBar)

# cutree returns a vector of cluster membership
# in the order of the original data rows
# examine it

# examine the cluster membership by it's order
# in the heatmap

# 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

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.

ADD COMMENTlink modified 4.7 years ago • written 4.7 years ago by seidel6.5k

It works. My mistake is that I think the order of the heat map is 1:n.

ADD REPLYlink written 4.7 years ago by liux.bio250

I mostly agree with the code above except for the line: 

# grab a cluster
cluster1 <- d[mycl == 1,]

I think the correct code would be: 

1)reorder your d table according to the heatmap 
2)grab clusters
cluster1 <- d[mycl == 1,]
ADD REPLYlink modified 3.1 years ago • written 3.1 years ago by molla.linda10
gravatar for joneill4x
5 months ago by
joneill4x10 wrote:

Thank you, this was really helpful. EDIT: this was meant to be under seidel's answer.

ADD COMMENTlink modified 5 months ago • written 5 months ago by joneill4x10
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 927 users visited in the last hour