【问题标题】:How to label each node in a dendrogram based on label for the children using R如何使用 R 根据子项的标签标记树状图中的每个节点
【发布时间】:2012-11-09 02:33:27
【问题描述】:

我在 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)

【问题讨论】:

  • 您应该添加您现在正在使用的代码,或者您知道所需代码的哪一部分。这将使人们更容易回答您的问题。
  • 我认为您要求做两件事:1)向树状图添​​加信息。 2)将该信息显示在图中。我说的对吗?

标签: r dendrogram hierarchical-clustering


【解决方案1】:

这可能是一个答案,但是,它正在重新实现 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)

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

【讨论】:

  • 为什么不将 tmp
  • 我尝试在树上应用 apply_text,但只有根和叶子被标记,中间的没有。我返回子树的方式很可能存在问题。 dendrapply 逐个节点扫描树节点,最终标记所有节点。我想我在这里遗漏了一些东西,因为我以某种方式在做 dendrapply 应该递归地做的事情。
猜你喜欢
  • 2021-12-23
  • 2018-08-26
  • 2016-07-27
  • 2019-04-18
  • 1970-01-01
  • 2015-09-16
  • 2018-05-27
  • 2020-06-27
  • 2016-02-10
相关资源
最近更新 更多