如何通过R中的附加因子变量为树形图的标签着色 [英] How to colour the labels of a dendrogram by an additional factor variable in R

查看:832
本文介绍了如何通过R中的附加因子变量为树形图的标签着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码在R中运行层次聚类分析后生成了一个树形图。我现在试图根据另一个因子变量,将其保存为向量的颜色标签。我最近实现这一点是使用 sparcl 包中的 ColourDendrogram 函数对分支进行颜色编码。如果可能,我更喜欢对标签进行颜色编码。我在以下链接中找到了类似问题的答案:树状图的颜色分支使用现有列& 在R 的树状图中着色分支,但我还没能以解决如何转换示例代码为我的目的。下面是一些示例数据和代码。

I have produced a dendrogram after running hierarchical clustering analysis in R using the below code. I am now trying to colour the labels according to another factor variable, which is saved as a vector. The closest that I have come to achieving this is to colour code the branches using the ColourDendrogram function in the sparcl package. If possible, I would prefer to colour-code the labels. I have found answers to a similar questions at the following links Color branches of dendrogram using an existing column & Colouring branches in a dendrogram in R, but I have not been able to work out how to convert the example code for my purpose. Below is some example data and code.

> dput(df)
structure(list(labs = c("a1", "a2", "a3", "a4", "a5", "a6", "a7", 
"a8", "b1", "b2", "b3", "b4", "b5", "b6", "b7"), var = c(1L, 
1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L), td = c(13.1, 
14.5, 16.7, 12.9, 14.9, 15.6, 13.4, 15.3, 12.8, 14.5, 14.7, 13.1, 
14.9, 15.6, 14.6), fd = c(2L, 3L, 3L, 1L, 2L, 3L, 2L, 3L, 2L, 
4L, 2L, 1L, 4L, 3L, 3L)), .Names = c("labs", "var", "td", "fd"
), class = "data.frame", row.names = c(NA, -15L))

df.nw = df[,3:4]
labs = df$labs

d = dist(as.matrix(df.nw))                          # find distance matrix 
hc = hclust(d, method="complete")                   # apply hierarchical clustering 
plot(hc, hang=-0.01, cex=0.6, labels=labs, xlab="") # plot the dendrogram

hcd = as.dendrogram(hc)                             # convert hclust to dendrogram 
plot(hcd, cex=0.6)                                  # plot using dendrogram object

Var = df$var                                        # factor variable for colours
varCol = gsub("1","red",Var)                        # convert numbers to colours
varCol = gsub("2","blue",varCol)

# colour-code dendrogram branches by a factor 
library(sparcl)
ColorDendrogram(hc, y=varCol, branchlength=0.9, labels=labs,
                xlab="", ylab="", sub="")   

任何建议如何做非常感谢。

Any advise on how to do this would be greatly appreciated.

推荐答案

尝试

# ... your code
colLab <- function(n) {
  if(is.leaf(n)) {
    a <- attributes(n)
    attr(n, "label") <- labs[a$label]
    attr(n, "nodePar") <- c(a$nodePar, lab.col = varCol[a$label]) 
  }
  n
}
plot(dendrapply(hcd, colLab))


b $ b

通过

这篇关于如何通过R中的附加因子变量为树形图的标签着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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