r - 如何标记切割树状图的终端节点?

标签 r hierarchical-clustering dendrogram dendextend

我使用以下代码在特定高度切割树状图。我遇到的问题是,当我切割树状图时,我不知道如何向节点添加标签。如何用标签切割树状图使用R程序?

library(Heatplus)
cc=as.dendrogram(hclust(as.dist(mat),method="single"))
cutplot.dendrogram(cc,h=20)

最佳答案

在对 ?dendrogram 的帮助文档进行了大量挖掘之后,我偶然发现了dendrapply包含一个例子的函数来做一些非常相似的事情。这是您的解决方案,基于对 ?dendrapply 中示例的修改:

创建树状图并在高度处切割 h=20 :

dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave"))
chc <- cut(dhc, h=20)$upper

使用 newLabels 定义一个向量和一个函数 newLab修改单个节点标签。然后将此传递给 dendrapply :
newLabels <- paste("Custom", 1:22, sep="_")

local({
      newLab <<- function(n) {
        if(is.leaf(n)) {
          a <- attributes(n)
          i <<- i+1
          attr(n, "label") <- newLabels[i]
        }
        n
      }
      i <- 0
    })

nhc <- dendrapply(chc, newLab)
labels(nhc)
 [1] "Custom_1"  "Custom_2"  "Custom_3"  "Custom_4"  "Custom_5"  "Custom_6" 
 [7] "Custom_7"  "Custom_8"  "Custom_9"  "Custom_10" "Custom_11" "Custom_12"
[13] "Custom_13" "Custom_14" "Custom_15" "Custom_16" "Custom_17" "Custom_18"
[19] "Custom_19" "Custom_20" "Custom_21" "Custom_22"

plot(nhc)

enter image description here

关于r - 如何标记切割树状图的终端节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7199571/

相关文章:

linux - 在 Linux 上用 g++ 编译 RInside 程序

R : help to analyse cluster content in hierarchical clustering

python - 树状图 y 轴标记困惑

python - 显示 scipy 树状图的聚类标签

python - 从 seaborn clustermap 中提取树状图

r - 我应该在 API GET 请求中的何处插入 API key ?

R:在 Shiny 的表格输出中将部分单元格加粗

r - 重命名 data.table 的问题

python - 在 Scipy/Matplotlib 中注释树状图节点

r - 如何翻转pheatmap中的层次聚类节点