Label and color leaf dendrogram

前端 未结 2 1814
感动是毒
感动是毒 2020-11-27 18:37

I am trying to create a dendrogram, were my samples have 5 group codes (act as sample name/species/etc but its repetitive).

Therefore, I have two issues that a help

2条回答
  •  北海茫月
    2020-11-27 19:07

    You could convert you hclust object into a dendrogram and use ?dendrapply to modify the properties (attributes like color, label, ...) of each node, e.g.:

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

    enter image description here

    EDIT: Add code for your minimal example:

        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)
    

    enter image description here

提交回复
热议问题