2

I am trying to draw a dendrogram so that the labels on the branches match the group number from my cluster analysis. Currently the branches are simply labelled from left to right in the order that they appear, not the actual group number. Here is my current R code and resulting dendrogram:

dst <- dist(Model_Results,method="binary") 
hca <- hclust(dst)
clust <- cutree(hca,k=40)
dend <-as.dendrogram(hca)
library(dendextend)
dend1 <- color_branches(dend, k = 40, groupLabels = TRUE)
plot(dend1)

enter image description here

How can I change the labels to match to my actual group number?

Damian M
  • 41
  • 6
  • I would actually say this is a bug, since I would have preferred the numbers to match the results of the cutree (or at least have it as an option). Thanks for this post, I've added it as an issue: https://github.com/talgalili/dendextend/issues/65 – Tal Galili Feb 07 '18 at 08:47

2 Answers2

2

I think I finally managed to figure it out...

dst <- dist(Model_Results,method="binary") 
hca <- hclust(dst)
clust <- cutree(hca,k=40)
dend <-as.dendrogram(hca)
library(dendextend)
clust.cutree <- dendextend:::cutree(dend, k=40, order_clusters_as_data = FALSE)
idx <- order(as.numeric(names(clust.cutree)))
clust.cutree <- clust.cutree[idx]
tbl <- table(clust, clust.cutree)
lbls <- apply(tbl,2,which.max)
dend1 <- color_branches(dend, k = 40, groupLabels = lbls)
plot(dend1)

enter image description here

Damian M
  • 41
  • 6
1

Straight from the documentation here about the color_branches() function:

"If groupLabels=TRUE then numeric group labels will be added to each cluster. If a vector is supplied then these entries will be used as the group labels. If a function is supplied then it will be passed a numeric vector of groups (e.g. 1:5) and must return the formatted group labels."

I hope this helps.

InfiniteFlash
  • 1,038
  • 1
  • 10
  • 22
  • This may sound like a dumb question however I am relatively new to R programming - how do I generate a vector containing the values of my current group labels and the corresponding actual group values? I have 40 groups so it is not something I can do manually (not easily anyway). – Damian M Feb 06 '18 at 07:19
  • Look at your `clust` object and see if you can see the "groups" within it. There should be a vector of values in it. – InfiniteFlash Feb 06 '18 at 08:16
  • Getting the groups from the `clust` object is not a problem, however clearly the `color_branches` function is assigning the arbitrary label numbers to the dendrogram. I need to derive a vector that links the two. I found a similar post [here](https://stackoverflow.com/questions/44188612/r-dendrogram-grouplabels-not-match-real-labels-package-dendextend), however with my limited R knowledge, I can't figure out how to apply it to my situation, or if there's an alternative work-around – Damian M Feb 06 '18 at 12:03