【问题标题】:Label and color leaf dendrogram标签和颜色叶树状图
【发布时间】:2013-09-19 02:20:55
【问题描述】:

我正在尝试创建一个树状图,我的样本是否有 5 个组代码(作为样本名称/物种/等,但它是重复的)。

因此,我有两个问题需要帮助:

  • 如何在叶子标签中显示组码(而不是样本号)?

  • 我希望为每个代码组分配一种颜色并根据它为叶子标签着色(可能会发生它们不在同一个进化枝中,这样我可以找到更多信息)?

是否可以使用我的脚本来执行此操作(ape 或 ggdendro):

sample<-read.table("C:/.../DOutput.txt", header=F, sep="")
groupCodes <- sample[,1]
sample2<-sample[,2:100] 
d <- dist(sample2, method = "euclidean")  
fit <- hclust(d, method="ward")
plot(as.phylo(fit), type="fan") 
ggdendrogram(fit, theme_dendro=FALSE)  

替换我的 read.table 的随机数据框:

sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=200))
groupCodes <- c(rep("A",25), rep("B",25), rep("C",25), rep("D",25)) # fixed error
sample2 <- data.frame(cbind(groupCodes), sample) 

【问题讨论】:

    标签: r dendrogram dendextend


    【解决方案1】:

    您可以将hclust 对象转换为dendrogram 并使用?dendrapply 修改每个节点的属性(颜色、标签等属性),例如:

    ## stupid toy example
    samples <- matrix(c(1, 1, 1,
                        2, 2, 2,
                        5, 5, 5,
                        6, 6, 6), byrow=TRUE, nrow=4)
    
    ## set sample IDs to A-D
    rownames(samples) <- LETTERS[1:4]
    
    ## perform clustering
    distSamples <- dist(samples)
    hc <- hclust(distSamples)
    
    ## function to set label color
    labelCol <- function(x) {
      if (is.leaf(x)) {
        ## fetch label
        label <- attr(x, "label") 
        ## set label color to red for A and B, to blue otherwise
        attr(x, "nodePar") <- list(lab.col=ifelse(label %in% c("A", "B"), "red", "blue"))
      }
      return(x)
    }
    
    ## apply labelCol on all nodes of the dendrogram
    d <- dendrapply(as.dendrogram(hc), labelCol)
    
    plot(d)
    

    编辑:为您的 minimal 示例添加代码:

        sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=200))
    groupCodes <- c(rep("A",25), rep("B",25), rep("C",25), rep("D",25))
    
    ## make unique rownames (equal rownames are not allowed)
    rownames(sample) <- make.unique(groupCodes)
    
    colorCodes <- c(A="red", B="green", C="blue", D="yellow")
    
    
    ## perform clustering
    distSamples <- dist(sample)
    hc <- hclust(distSamples)
    
    ## function to set label color
    labelCol <- function(x) {
      if (is.leaf(x)) {
        ## fetch label
        label <- attr(x, "label")
        code <- substr(label, 1, 1)
        ## use the following line to reset the label to one letter code
        # attr(x, "label") <- code
        attr(x, "nodePar") <- list(lab.col=colorCodes[code])
      }
      return(x)
    }
    
    ## apply labelCol on all nodes of the dendrogram
    d <- dendrapply(as.dendrogram(hc), labelCol)
    
    plot(d)
    

    【讨论】:

    • 您好,感谢您的快速回复和所有帮助,非常感谢。它可以工作,但是当“rownames(sample)
    • @user2676173:我不这么认为。 dim(sample) 显示 100 200。
    • 我正在用我的真实数据检查脚本,C 被命名为 Con。在这种情况下,当我将颜色代码中的 C="red" 更改为 "Con"="red" 或 Con="red" 时,它不会为匹配的标签着色(当我更改 A、B、D 时也会发生这种情况到乘字符实组名)。颜色代码仅限于一个字符的任何原因?如何解决。
    • @user2676173:因为那行code &lt;- substr(label, 1, 1)。 substr(label, 1, 3) 应该可以工作。阅读?substr。如果您的问题得到解决,请将答案标记为正确。
    • 如果我们知道实际类,除了树之外,是否还有将颜色传播到分支的方法?
    【解决方案2】:

    这是一个使用名为“dendextend”的新包来解决这个问题的方法,该包正是为这类事情而构建的。

    您可以在以下 URL 的“使用”部分的演示文稿和小插曲中看到许多示例:https://github.com/talgalili/dendextend

    这是这个问题的解决方案:(注意如何重新排序颜色以首先适合数据,然后适合树状图的新顺序的重要性)

    ####################
    ## Getting the data:
    
    sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=200))
    groupCodes <- c(rep("Cont",25), rep("Tre1",25), rep("Tre2",25), rep("Tre3",25))
    rownames(sample) <- make.unique(groupCodes)
    
    colorCodes <- c(Cont="red", Tre1="green", Tre2="blue", Tre3="yellow")
    
    distSamples <- dist(sample)
    hc <- hclust(distSamples)
    dend <- as.dendrogram(hc)
    
    ####################
    ## installing dendextend for the first time:
    
    install.packages('dendextend')
    
    ####################
    ## Solving the question:
    
    # loading the package
    library(dendextend)
    # Assigning the labels of dendrogram object with new colors:
    labels_colors(dend) <- colorCodes[groupCodes][order.dendrogram(dend)]
    # Plotting the new dendrogram
    plot(dend)
    
    
    ####################
    ## A sub tree - so we can see better what we got:
    par(cex = 1)
    plot(dend[[1]], horiz = TRUE)
    

    【讨论】:

    • 聚会晚了,但我在文档中找不到颜色代码? rdocumentation.org/packages/dendextend/versions/1.13.4我觉得你的回答很好,我想根据自己的问题进行调整;)
    • @takeITeasy 谢谢 :) 它是在解决方案中定义的,它不是包的一部分:colorCodes
    猜你喜欢
    • 2013-01-25
    • 2013-09-22
    • 2018-03-21
    • 2013-06-18
    • 2018-08-30
    • 2015-12-02
    • 2021-12-23
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多