【问题标题】:groupLabels not shown when using dendextend colour_branches使用 dendextend colour_branches 时未显示 groupLabels
【发布时间】:2021-06-26 06:22:56
【问题描述】:

我要实现的工作流程是:

dm <- dist(data)
dend <- hclust(dm)
k <- stats::cutree(dend, k = 10)
data$clusters <- k
plot(hclust, colorBranchees = k) #???? What I can use here.

所以我使用cutree 输出搜索颜色树状图分支。我找到的只是dendextend。

问题是我无法使用dendextend 实施工作流。

这是我想出的,但我现在想显示clusterLabels

library(dendextend)
hc <- hclust(dist(USArrests))
dend <- as.dendrogram(hc)

kcl <- dendextend::cutree(dend, k = 4)
dend1 <- color_branches(dend, clusters = kcl[order.dendrogram(dend)], groupLabels = TRUE)%>% set("labels_cex", 1)
plot(dend1, main = "Dendrogram dist JK")

另外,尝试groupLabels = 1:4 之类的方法也无济于事。

使用参数k(o 簇数)指定 groupLable 确实有效。但不幸的是,标签与dendextend自己的cutree方法生成的标签不同。

请注意,此处集群 4 有 2 个成员。

> table(kcl)
kcl
 1  2  3  4 
14 14 20  2

这篇文章建议使用dendextend::cutree(dend,k = nrCluster, order_clusters_as_data = FALSE) r dendrogram - groupLabels not match real labels (package dendextend)

但是我不能使用dendextend::cutree的输出来分组数据(因为排序不匹配。

我很乐意在 R 中使用不同的树状图绘图库,但到目前为止,我的网络搜索“通过 cutree 输出着色树状图分支”指向 dendextend 包。

【问题讨论】:

    标签: r dendrogram dendextend


    【解决方案1】:

    很抱歉,我不确定我是否完全理解您的问题。

    您似乎想在 curtree 的输出和原始数据之间对齐。 如果是这种情况,那么您需要使用dendextend::cutree(dend,k = nrCluster, order_clusters_as_data = TRUE) 例如:

    require(dendextend)
    d1 <- USArrests[1:10,]
    hc <- hclust(dist(d1))
    dend <- as.dendrogram(hc)
    k <- dendextend::cutree(dend, k = 3, order_clusters_as_data = TRUE)
    d2 <- cbind(d1, k)
    plot(color_branches(dend, 3))
    d2
    # an easier way to see the clusters is by ordering the rows of the data based on the order of the dendrogram
    d2[order.dendrogram(dend),]
    

    剧情不错:

    并且集群被正确映射到数据(见输出)

    > require(dendextend)
    > d1 <- USArrests[1:10,]
    > hc <- hclust(dist(d1))
    > dend <- as.dendrogram(hc)
    > k <- dendextend::cutree(dend, k = 3, order_clusters_as_data = TRUE)
    > d2 <- cbind(d1, k)
    > plot(color_branches(dend, 3))
    > d2
                Murder Assault UrbanPop Rape k
    Alabama       13.2     236       58 21.2 1
    Alaska        10.0     263       48 44.5 1
    Arizona        8.1     294       80 31.0 2
    Arkansas       8.8     190       50 19.5 1
    California     9.0     276       91 40.6 2
    Colorado       7.9     204       78 38.7 1
    Connecticut    3.3     110       77 11.1 3
    Delaware       5.9     238       72 15.8 1
    Florida       15.4     335       80 31.9 2
    Georgia       17.4     211       60 25.8 1
    > # an easier way to see the clusters is by ordering the rows of the data based on the order of the dendrogram
    > d2[order.dendrogram(dend),]
                Murder Assault UrbanPop Rape k
    Connecticut    3.3     110       77 11.1 3
    Florida       15.4     335       80 31.9 2
    Arizona        8.1     294       80 31.0 2
    California     9.0     276       91 40.6 2
    Arkansas       8.8     190       50 19.5 1
    Colorado       7.9     204       78 38.7 1
    Georgia       17.4     211       60 25.8 1
    Alaska        10.0     263       48 44.5 1
    Alabama       13.2     236       58 21.2 1
    Delaware       5.9     238       72 15.8 1
    

    如果这回答了您的问题或者您在此处有后续问题,请 LMK。

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多