How To Get The Subclusters From The Object Of Hclust() Using Cutree() According To The Order On The Map Produced By Heatmap.2?
1
8
Entering edit mode
10.4 years ago
liux.bio ▴ 360

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!

• 40k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
20
Entering edit mode
10.4 years ago
seidel 11k

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.

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

    d=(d)[row.hc$order,]
    
  2. grab clusters

    cluster1 <- d[mycl == 1,]
    
ADD REPLY

Login before adding your answer.

Traffic: 1852 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6