Label R dendrogram branches with correct group number

邮差的信 提交于 2019-12-11 17:04:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!