简体   繁体   English

用正确的组号标记R树状图分支

[英]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: 这是我当前的R代码和生成的树状图:

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? 如何更改标签以匹配我的实际组号?

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)

在此处输入图片说明

Straight from the documentation here about the color_branches() function: 直接从这里的文档中获得有关color_branches()函数的信息:

"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 (eg 1:5) and must return the formatted group labels." “如果groupLabels=TRUE则将数字组标签添加到每个群集。 如果提供了矢量,则这些条目将用作组标签 。如果提供了函数,则将传递组的数字矢量(例如1 :5),并且必须返回格式化的组标签。”

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM