使用现有列为树状图着色 [英] Color branches of dendrogram using an existing column

查看:103
本文介绍了使用现有列为树状图着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要集群的数据框。我现在正在使用 hclust 。在我的数据框中,有一个 FLAG 列,我希望以此为树状图着色。根据结果​​,我试图找出各个 FLAG 类别之间的相似之处。我的数据框如下所示:

I have a data frame which I am trying to cluster. I am using hclust right now. In my data frame, there is a FLAG column which I would like to color the dendrogram by. By the resulting picture, I am trying to figure out similarities among various FLAG categories. My data frame looks something like this:

FLAG    ColA    ColB    ColC    ColD

我在 colA colB colC colD 。我想将它们聚类并根据 FLAG 类别为其着色。例如-如果为1,则为红色,如果为0,则为蓝色(我只有两个类别)。现在,我正在使用簇图的原始版本。

I am clustering on colA, colB, colC and colD. I would like to cluster these and color them according to FLAG categories. Ex - color red if 1, blue if 0 (I have only two categories). Right now I am using the vanilla version of cluster plotting.

hc<-hclust(dist(data[2:5]),method='complete')
plot(hc)

在这方面的任何帮助都会

Any help in this regard would be highly appreciated.

推荐答案

如果您要基于某个变量为树状图的分​​支着色,则可以使用以下代码(主要是取自dendrapply函数的帮助)应该给出期望的结果:

If you want to color the branches of a dendrogram based on a certain variable then the following code (largely taken from the help for the dendrapply function) should give the desired result:

x<-1:100
dim(x)<-c(10,10)
groups<-sample(c("red","blue"), 10, replace=TRUE)

x.clust<-as.dendrogram(hclust(dist(x)))

local({
  colLab <<- function(n) {
    if(is.leaf(n)) {
      a <- attributes(n)
      i <<- i+1
      attr(n, "edgePar") <-
        c(a$nodePar, list(col = mycols[i], lab.font= i%%3))
    }
    n
  }
  mycols <- groups
  i <- 0
})

x.clust.dend <- dendrapply(x.clust, colLab)
plot(x.clust.dend)

这篇关于使用现有列为树状图着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆