如何用R中的附加因子变量给树状图的标签上色

如何用R中的附加因子变量给树状图的标签上色,r,colors,plot,dendrogram,dendextend,R,Colors,Plot,Dendrogram,Dendextend,在使用下面的代码在R中运行分层聚类分析后,我生成了一个树状图。我现在尝试根据另一个因子变量给标签上色,该变量保存为向量。我最接近于实现这一点的是使用sparcl包中的colorddengram函数对分支进行颜色编码。如果可能的话,我更愿意给标签加上颜色代码。我在下面的链接中找到了类似问题的答案&,但是我还没有找到转换示例代码的方法。下面是一些示例数据和代码 > dput(df) structure(list(labs = c("a1", "a2", "a3", "a4", "a5", "a

在使用下面的代码在R中运行分层聚类分析后,我生成了一个树状图。我现在尝试根据另一个因子变量给标签上色,该变量保存为向量。我最接近于实现这一点的是使用
sparcl
包中的
colorddengram
函数对分支进行颜色编码。如果可能的话,我更愿意给标签加上颜色代码。我在下面的链接中找到了类似问题的答案&,但是我还没有找到转换示例代码的方法。下面是一些示例数据和代码

> 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="")   
如果您能提供任何建议,我们将不胜感激。

试试看

# ... 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))
#。。。你的代码

colLab对于标签的着色,使用软件包中的
labels\u colors
功能是最简单的。例如:

# install.packages("dendextend")
library(dendextend)

small_iris <- iris[c(1, 51, 101, 2, 52, 102), ]
dend <- as.dendrogram(hclust(dist(small_iris[,-5])))
# Like: 
# dend <- small_iris[,-5] %>% dist %>% hclust %>% as.dendrogram

# By default, the dend has no colors to the labels
labels_colors(dend)
par(mfrow = c(1,2))
plot(dend, main = "Original dend")

# let's add some color:
colors_to_use <- as.numeric(small_iris[,5])
colors_to_use
# But sort them based on their order in dend:
colors_to_use <- colors_to_use[order.dendrogram(dend)]
colors_to_use
# Now we can use them
labels_colors(dend) <- colors_to_use
# Now each state has a color
labels_colors(dend) 
plot(dend, main = "A color for every Species")
#安装程序包(“Dendestend”)
图书馆(Dendestend)
小虹膜%as.树状图
#默认情况下,dend没有标签颜色
标签颜色(dend)
par(mfrow=c(1,2))
地块(dend,main=“原始dend”)
#让我们添加一些颜色:

颜色使用@luke:谢谢你的回答。这提供了正确的颜色,但不允许使用特定于用户的标签(上面用“实验室”指定的标签)。根据在上找到的代码,我尝试将以下内容添加到函数
attr(n,“label”)中,我还尝试在运行hclust之前更改df行名称,但这不起作用。关于如何获得特定于用户的彩色标签,有什么想法吗?谢谢你的帮助。@jjulip尝试使用
attr(n,“label”)@Tal:刚刚看到了这个。谢谢你的回答!真的很有帮助。谢谢@jjulip。干杯,THi@jjulip-我已经添加了一个更详细的答案。我希望你可以考虑把它标记为解决方案。谢谢,太好了,谢谢。我去看看!非常感谢。