Reorder dendrogram for a better heat map
1
0
Entering edit mode
6.6 years ago
sugus ▴ 150

Hi there! I am using aheatmap to find some pattern but I need to reorder the dendrogram. I find there is a function called reorder that seems to do this job but I am really having difficulty in understanding it. I tried messing around with 'weights' for reorder, I thought when the anglo.fun = mean it will ignore the size of each cluster than I could just give each cluster a vector of the same number but actually it is NOT!

I tried looking other pages like, https://www.mail-archive.com/r-help@r-project.org/msg195536.html. and there is an example on the man page.

set.seed(123)
x <- rnorm(10)
hc <- hclust(dist(x))
dd <- as.dendrogram(hc)
dd.reorder <- reorder(dd, 10:1)
plot(dd, main = "random dendrogram 'dd'")
>
op <- par(mfcol = 1:2)
plot(dd.reorder, main = "reorder(dd, 10:1)")
plot(reorder(dd, 10:1, agglo.FUN = mean), main = "reorder(dd, 10:1, mean)")
par(op)

Have no idea about how this function works! Really need help.

R dendrogram reorder • 5.8k views
ADD COMMENT
5
Entering edit mode
6.6 years ago

You are using the agglo.FUN function correctly. Usually, it is possible to get the ordering that you want by 'messing around with' (i.e. modifying) different mathematical summarisations for agglomeration (e.g. mean, max, min, etc.) in combination with different linkage metrics (e.g., ward.D2, complete, single, average, WPGMA, et cetera). Also, obviously, one cannot just re-work the entire branch structure because that would be fabrication of results.

If, after exhausting the methods that I've mentioned above, you still cannot get the ordering that you want, then you can still attempt to modify the ordering by assigning weights to each leaf in the dendrogram. Re-using your sample code, I'll show you how you can flip one branch from the left to the right.

First, generate a standard dendrogram, re-ordered with weights 10:1 (corresponding to number of leafs) (left), and then the same dendrogram with agglo.FUN = mean (right):

set.seed(123)
x <- rnorm(10)
hc <- hclust(dist(x))
dd <- as.dendrogram(hc)
dd.reorder <- reorder(dd, 10:1)

op <- par(mfrow = c(1,2))
   plot(dd.reorder, main = "reorder(dd, 10:1)")
   plot(reorder(dd, 10:1, agglo.FUN = mean), main = "reorder(dd, 10:1, mean)")
par(op)

Screen_Shot_2018_04_18_at_10_57_33

Now, let's flip the middle cluster to the left by assigning weights, Within that cluster, I'll also flip the number 7 leaf to the right. The order of the weights vector corresponds to the order from the original data matrix. As leaves 2, 4, 5, and 7 are the ones that I want to move, I only modify indices 2, 4, 5, and 7 in the weights vector. The 7th index gets a 2, which is necessary to just flip leaf 7 to the right-hand-side of that cluster (with a weight of just 1, it stays on the left-hand-side).

plot(reorder(dd, c(10,1,10,1,1,10,2,10,10,10), agglo.FUN=mean), main="reorder Kevin")

Screen_Shot_2018_04_18_at_10_58_34

Hope that this helps.

Kevin

ADD COMMENT
0
Entering edit mode

Hi Kevin, this explanation really helps. But how to flip the dendrogram if I have more than 10 leafs, such as 100? Assign the weights one by one?

ADD REPLY
0
Entering edit mode

Do you literally just want the mirror image of your dendrogram?

ADD REPLY

Login before adding your answer.

Traffic: 1685 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