问题
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)
How can I change the labels to match to my actual group number?
回答1:
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)
回答2:
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.
来源:https://stackoverflow.com/questions/48636522/label-r-dendrogram-branches-with-correct-group-number