繁体   English   中英

如何基于使用R的子项的标签来标记树形图中的每个节点

[英]How to label each node in a dendrogram based on label for the children using R

我在R中有一个树形图,其中每个叶子都有一个值。 我喜欢通过对其子节点的值求和来定义每个节点的值。 我熟悉dendrapply,但是我不知道如何在函数中访问节点的子节点以及如何递归地编写函数。

这是开头的代码:

library("stats")
library("fastcluster")
library("cluster")
D = rbind( + c(1,1,1,1,1), 
 + c(1,2,1,1,1),
 + c(2,2,2,2,2), 
 + c(3,4,5,6,9)

)
dnd = as.dendrogram(hclust.vector(D))

apply_text <<- function(n) {
   if (!is.leaf(n)) {

      attr(n, "edgetext") <- add the value of the branches
   }
   if (is.leaf(n)) {
      attr(n, "edgetext") <- 1
   }
   n
}

tmp <- dendrapply(dnd, apply_text)
plot(tmp)

这可能是一个答案,然而,它正在重新实现dendrapply。

apply_text <<- function(n){
  if (!is.leaf(n)) {
    cutversion = cut(n, h = attributes(n)$height)
    leftLabel = attr(apply_text(cutversion$lower[[1]]), "edgetext")  
    rightLabel= attr(apply_text(cutversion$lower[[2]]), "edgetext")
    attr(n, "edgetext") = as.numeric(as.character(leftLabel)) + as.numeric(as.character(rightLabel)) 
     }
  if(is.leaf(n)) {
    attr(n,"edgetext") <- 1
  }
    n
}

tmp <- dendrapply(dnd, apply_text)

有没有人知道如何删除标签上的多边形? 其他人似乎也要求将其删除。 有什么进展吗?

暂无
暂无

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

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