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

发布于 2024-12-01 09:35:07 字数 201 浏览 10 评论 0原文

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

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

I used the following code to cut the dendrogram at a particular height.The problem I'm having is that when I cut a dendrogram, I can't figure out how to add labels to the nodes.How can I cut a dendrogram with labels using R program?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赴月观长安 2024-12-08 09:35:07

在深入研究了 ?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)

在此处输入图像描述

After a fair amount of digging into the help documentation for ?dendrogram, I stumbled on the dendrapply function that contains an example to do something very similar. Here is your solution, based on a modification of the example in ?dendrapply:

Create dendrogram and cut at height h=20:

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

Define a vector with the newLabels, and a function newLab that modifies an individual node label. Then pass this to 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

绳情 2024-12-08 09:35:07

这是 Andrie 编写的修改后的解决方案,但使用了一个名为“dendextend”的新包,专门为这种事。

您可以在包的演示文稿和插图中看到许多示例,位于以下 URL 的“用法”部分: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')

Best,
塔尔

Here is a modified solution for what Andrie wrote, but using a new package called "dendextend", built exactly for this sort of thing.

You can see many examples in the presentations and vignettes of the package, in the "usage" section in the following URL: https://github.com/talgalili/dendextend

Here is the solution for this question:

# 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)

For installing the package (since I have yet to upload it to CRAN), use:

####################
## 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')

Best,
Tal

你穿错了嫁妆 2024-12-08 09:35:07
cc$labels

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

cc$labels <- myVector

您可以添加自己的矢量来更改标签

cc$labels

This is a vector of all the elements in the dendogram.

cc$labels <- myVector

You can add in your own vector to change the labels

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文