如何通过R中的附加因子变量为树形图的标签着色

时间:2022-02-14 14:58:25

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.

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

> 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.

任何关于如何做到这一点的建议将不胜感激。

2 个解决方案

#1


3  

Try

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

(via)

#2


2  

For coloring your labels, it would be the easiest to use the labels_colors function from the dendextend package. For example:

要使标签着色,最容易使用dendextend包中的labels_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")

For more details on the package, you can have a look at its vignette.

有关包装的更多详细信息,您可以查看其小插图。

如何通过R中的附加因子变量为树形图的标签着色

#1


3  

Try

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

(via)

#2


2  

For coloring your labels, it would be the easiest to use the labels_colors function from the dendextend package. For example:

要使标签着色,最容易使用dendextend包中的labels_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")

For more details on the package, you can have a look at its vignette.

有关包装的更多详细信息,您可以查看其小插图。

如何通过R中的附加因子变量为树形图的标签着色