【问题标题】:Dendextend: Regarding how to color a dendrogram’s labels according to defined groupsDendextend:关于如何根据定义的组为树状图的标签着色
【发布时间】:2017-12-26 07:24:26
【问题描述】:

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

假设我有一个数据框,其中第一列包含用于聚类的个人姓名,然后是几列包含要分析的因素,最后一列包含每个人的组信息(请参阅下表)。

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

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

如果可以做到这一点,有没有办法定义分配给每个组的颜色?

【问题讨论】:

    标签: r hierarchical-clustering dendextend


    【解决方案1】:

    我可以使用另一个名为“sparcl”的包来做到这一点。我是根据之前的帖子 (How to colour the labels of a dendrogram by an additional factor variable in R) 做的。

    这是我的代码:

    #load the dataset.....
    #calculate distances
    d <- dist(dataset2, method="Jaccard")
    ## Hierarchical cluster the data
    hc <- hclust(d)
    dend <- as.dendrogram(hc)
    #create labels
    labs=dataset$individual
    #format to dendrogram
    hcd = as.dendrogram(hc)                             
    plot(hcd, cex=0.6)
    # factor variable for colours                                  
    Var = dataset$group   
    # convert numbers to colours                                    
    varCol = gsub("g1.*","green",Var)                        
    varCol = gsub("g2.*","gold",varCol)
    varCol = gsub("g3.*","pink",varCol)                        
    varCol = gsub("g4.*","purple",varCol)
    varCol = gsub("g5.*","blue",varCol)                        
    varCol = gsub("g6.*","red",varCol)
    #colour-code dendrogram branches by a factor 
    library(sparcl)
    ColorDendrogram(hc, y=varCol, branchlength=0.9, labels=labs,
                xlab="", ylab="", sub="")  
    

    最后,我设法根据这篇文章 (How to colour the labels of a dendrogram by an additional factor variable in R) 的示例推断出一个“dendextend”包解决方案:

    # install.packages("dendextend")
    library(dendextend)
    
    #load the dataset.....
    dataset2<-dataset[,1:7]#same dataset as in the example
    
    #calculate the dendrogram
    dend <- as.dendrogram(hclust(dist(dataset2)))
    
    #capture the colors from the "group" column
    colors_to_use <- as.numeric(dataset$group)
    colors_to_use
    
    # sort the colors based on their order in dend:
    colors_to_use <- colors_to_use[order.dendrogram(dend)]
    colors_to_use
    
    #Apply colors 
    labels_colors(dend) <- colors_to_use
    
    # Patient labels have a color based on their group
    labels_colors(dend) 
    plot(dend, main = "Color in labels")
    

    【讨论】:

      【解决方案2】:

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

      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)
      

      将导致(如您所见,标签的颜色基于 iris 数据集中的另一个变量“Species”):

      (p.s.:为了更容易看出颜色与标签长度的关系,我将物种出现的次数增加了两倍)

      【讨论】:

      • 谢谢 Tal,您的代码更加优化了! ;)
      • 不客气 :) 有机会接受我的回答吗? (所以未来的人们会直接用这个来设置)
      • 亲爱的 Tal,很抱歉延迟接受您的回答。
      • 对了,你能推荐一种从树状图的每个分支中获取信息的方法吗?例如,以编程方式检索“dend”对象的每个分支上的标签名称。
      • 嗨@JLLavin - 你的意思是labels函数?
      猜你喜欢
      • 2015-09-16
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      相关资源
      最近更新 更多