Dendextend:关于如何根据定义的组为树状图标签着色 [英] Dendextend: Regarding how to color a dendrogram’s labels according to defined groups

查看:402
本文介绍了Dendextend:关于如何根据定义的组为树状图标签着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用名为dendextend的出色R包来绘制树状图并为其分支着色&标签根据一组预先定义的组. 我已经在Stack Overflow和dendextend vignette的FAQ中阅读了您的答案,但是我仍然不确定如何实现我的目标.

I'm trying to use an awesome R-package named dendextend, to plot a dendrogram and color its branches & labels according to a set of previously defined groups. I've read your answers in Stack Overflow, and the FAQs of dendextend vignette, but I'm still not sure on how to achieve my goal.

让我们想象一下,我有一个数据框,其中第一列包含要用于聚类的个人的名称,然后是几列要分析的因素,最后一列包含每个人的组信息(请参见下表).

Let's imagine I have a dataframe with a first column with the names of the individual to use for the clustering, then several columns with the factors to be analyzed, and the last column with the group information for each of the individuals (See following table).

individual  282856  282960  283275  283503  283572  283614  284015  group
pat15612    0   0   0   0   0   0   0   g2
pat38736    0   0   0   0   0   0   0   g2
pat38740    0   0   0   0   0   1   0   g2
pat38742    0   0   0   0   0   1   0   g4
pat38743    0   0   1   0   0   1   0   g3
pat38745    0   0   1   0   1   0   0   g4
pat38750    0   0   0   1   0   1   0   g4
pat38753    0   0   0   1   0   0   0   g3
pat40120    0   0   0   0   1   0   0   g4
pat40124    0   0   0   0   1   0   0   g4
pat40125    0   0   0   0   1   1   0   g4
pat40126    0   0   0   1   0   0   0   g4
pat40137    1   0   0   0   0   0   0   g4
pat40142    0   1   0   0   0   0   0   g5
pat46903    0   0   0   0   0   1   0   g1
pat67612    1   0   0   0   1   0   0   g1
pat67621    0   0   0   0   0   0   0   g2
pat67630    0   0   1   0   0   0   0   g2
pat67634    0   0   0   0   0   0   0   g5
pat67657    0   1   0   1   0   0   0   g5
pat67680    0   0   0   0   0   1   0   g5
pat67683    0   0   1   1   0   0   0   g6

即使它们可能聚集在不同的块中,我如何根据它们所属的组为代表每个个体的分支和标签着色?

How do I do to color the branches and labels representing each of the individuals based on the group they belong, even though they may cluster in different blocks?

如果可以实现,是否有办法定义分配给每个组的颜色?

In case this can be achieved, is there a way to define the colors assigned to each group?

推荐答案

很高兴您自己解决了这个问题. 更简单的解决方案是在set函数中使用order_value = TRUE自变量.例如:

I'm glad you solved this on your own. The simpler solution is to use the order_value = TRUE argument in the set function. For example:

library(dendextend)
iris2 <- iris[,-5]
rownames(iris2) <- paste(iris[,5],iris[,5],iris[,5], rownames(iris2))
dend <- iris2 %>% dist %>% hclust %>% as.dendrogram
dend <- dend %>% set("labels_colors", as.numeric(iris[,5]), order_value = TRUE) %>%
        set("labels_cex", .5)
par(mar = c(4,1,0,8))
plot(dend, horiz = T)

将导致(如您所见,标签的颜色基于虹膜数据集中的其他变量"Species"):

Will result in (as you can see, the colors of the labels is based on the other variable "Species" in the iris dataset):

(附言:我将一个物种出现的次数增加了三倍,以便更容易地看到颜色与标签的长度之间的关系)

(p.s.: I tripled the number of times a species appears in order to make it easier to see how the color relates to the length of the label)

这篇关于Dendextend:关于如何根据定义的组为树状图标签着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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