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

问题描述:

我用下面的代码在特定的高度切割树状图。我遇到的问题是,当我切割树状图时,我无法弄清楚如何向节点添加标签。如何切割树状图使用R程序的标签?如何标记切割树状图的终端节点?

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

什么是树状图,而语言是什么呢? –

+0

@john这是R语言。我正在使用R搭档做群集聚类。 – akash

+0

好吧,什么是树状图?至少要说明什么是知识领域。统计?物理? –

相当数量挖到帮助文档?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

这里是为了什么Andrie写的,而是采用了全新的包装被称为“​​”,正是专为这样的事情修改方案。

你可以看到在以下网址的“用法”一节中的包的介绍和护身符许多例子,:https://github.com/talgalili/dendextend

以下是此问题的解决方案:

# define dendrogram object to play with: 
dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave")) 
chc <- cut(dhc, h=20)$upper 
# loading the package 
require(dendextend)# let's add some color: 
# change labels with a simple assignment: 
labels(chc) <- paste("Custom", 1:22, sep="_") 
plot(chc) 

对于安装包(因为我还没有把它上传到CRAN),用途:

#################### 
## installing dendextend for the first time: 

if (!require('installr')) install.packages('installr'); require('installr') 
## install.Rtools() # run this if you are using Windows and don't have Rtools 
require2(devtools) 
install_github('dendextend', 'talgalili') 
require2(Rcpp) 
install_github('dendextendRcpp', 'talgalili') 

最佳, 塔尔

cc$labels 

这是树状图中所有元素的向量。

cc$labels <- myVector 

您可以在自己的向量添加到更改标签