Damian M
Damian M

Reputation: 61

Label R dendrogram branches with correct group number

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?

Upvotes: 3

Views: 2819

Answers (2)

Damian M
Damian M

Reputation: 61

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

Upvotes: 3

InfiniteFlash
InfiniteFlash

Reputation: 1058

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.

Upvotes: 1

Related Questions