【问题标题】:How to colour the labels of a dendrogram by an additional factor variable in R如何通过 R 中的附加因子变量为树状图的标签着色
【发布时间】:2015-02-13 15:18:28
【问题描述】:

在使用以下代码在 R 中运行层次聚类分析后,我生成了一个树状图。我现在正在尝试根据另一个因子变量为标签着色,该变量保存为向量。我最接近实现这一点的是使用sparcl 包中的ColourDendrogram 函数对分支进行颜色编码。如果可能的话,我更喜欢对标签进行颜色编码。我在以下链接Color branches of dendrogram using an existing columnColouring branches in a dendrogram in R 中找到了类似问题的答案,但我无法弄清楚如何为我的目的转换示例代码。下面是一些示例数据和代码。

> 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="")   

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

【问题讨论】:

    标签: r colors plot dendrogram dendextend


    【解决方案1】:

    要为标签着色,最简单的方法是使用 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")
    

    更多礼包详情,可以看at its vignette

    【讨论】:

    • @Tal:刚看到这个。感谢您的回答!真的很有帮助。
    • 谢谢@jjulip。干杯,T
    • 嗨@jjulip - 我添加了一个更详细的答案。我希望您可以考虑将其标记为解决方案。谢谢。
    • 太好了,谢谢。我会检查出来的!非常感谢。
    【解决方案2】:

    试试

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

    【讨论】:

    • @luke:谢谢你的回答。这会给出正确的颜色,但不允许使用特定于用户的标签(上面用“实验室”指定的标签)。按照stackoverflow.com/questions/14118033/… 找到的代码,我尝试将以下代码添加到函数attr(n, "label") &lt;- labs,但最终得到一个带有难以辨认标签的树状图(看起来R 已经覆盖了一堆不同的标签)。跨度>
    • 我还尝试在运行 hclust 之前更改 df 行名称,但这不起作用。关于如何获得彩色用户特定标签的任何想法?感谢您的帮助。
    • @jjulip 尝试使用attr(n, "label") &lt;- labs[a$label] 而不是attr(n, "label") &lt;- labs
    猜你喜欢
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2021-09-17
    • 2016-04-29
    • 1970-01-01
    相关资源
    最近更新 更多