Elizabeth
Elizabeth

Reputation: 6581

Colouring branches in a dendrogram in R

Dear resident R geniuses,

I would like to colour the branches of cluster in a dendrogram where the leaves are not labelled.

I found the following script here on Stackoverflow:

clusDendro <- as.dendrogram(Clustering)
labelColors <- c("red", "blue", "darkgreen", "darkgrey", "purple")

## function to get colorlabels
colLab <- function(n) {
   if(is.leaf(n)) {
       a <- attributes(n)
       # clusMember - a vector designating leaf grouping
       # labelColors - a vector of colors for the above grouping
       labCol <- labelColors[clusMember[which(names(clusMember) == a$label)]]
       attr(n, "nodePar") <- c(a$nodePar, lab.col = labCol)
   }
   n
}

## Graph
clusDendro <- dendrapply(clusDendro, colLab)
op <- par(mar = par("mar") + c(0,0,0,2))
plot(clusDendro,
     main = "Major title",
     horiz = T, type = "triangle", center = T)

par(op)

I have tried adapting it to my data as follows without success.

Gdis.UPGMA<-hclust(Gdis, method = "average", members=NULL)
k<-12
Gdiswo<-reorder.hclust(Gdis.UPGMA, Gdis, labels = FALSE)
cutg <- cutree(Gdiswo, k=k)

clusDendro <- as.dendrogram(Gdiswo)
labelColors <- c("red", "blue", "darkgreen", "darkgrey", "purple")

## function to get colorlabels
colLab <- function(n) {
   if(is.leaf(n)) {
       a <- attributes(n)
       # cutg - a vector designating leaf grouping
       # labelColors - a vector of colors for the above grouping
       labCol <- labelColors[cutg[which(names(cutg) == a$label)]]
       attr(n, "nodePar") <- c(a$nodePar, lab.col = labCol)
   }
   n
}

## Graph
clusDendro <- dendrapply(clusDendro, colLab)
op <- par(mar = par("mar") + c(0,0,0,2))
plot(clusDendro,
     main = "Major title",
     horiz = T, type = "triangle", center = T)

par(op)

I suspect n is causing the problem but I am not sure what I am suppose to put instead of n. As dissertation deadlines are looming I would be most grateful for any advice. Thanks, -Elizabeth

Upvotes: 3

Views: 5223

Answers (2)

gringer
gringer

Reputation: 39

Just for a bit more information, if you want to colour the labels, change edgePar to nodePar, and use lab.col. Due to the node defaults, you also need to set pch to NA if you want things to look the same:

## a smallish simple dendrogram
dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave"))

## create random colours for leaves based on a md5 hash of the leaf labels
library(digest);
dL <- dendrapply(dhc, function(n){
  if(is.leaf(n)){
    labelCol <- paste("#",substring(digest(attr(n,"label")),1,6), sep="");
    attr(n, "edgePar") <- list(col = labelCol);
    attr(n, "nodePar") <- list(pch = NA, lab.col = labelCol, lab.cex = 0.75);
  }
  n;
});

plot(dL); ## --> colored labels

Dendrogram with Coloured Labels

Upvotes: 1

Andrie
Andrie

Reputation: 179558

You need to set the edgePar elements of the dendrogram object.

In the help for ?dendrapply there is an example to set the colours of the node labels. By changing just one line to point to "edgePar" and setting col, you are almost there:

attr(n, "edgePar") <- c(a$nodePar, list(col = mycols[i], lab.font= i%%3))

The full modified example:

## a smallish simple dendrogram
dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave"))

## toy example to set colored leaf labels :
local({
  colLab <<- function(n) {
    if(is.leaf(n)) {
      a <- attributes(n)
      i <<- i+1
      attr(n, "edgePar") <-
        c(a$nodePar, list(col = mycols[i], lab.font= i%%3))
    }
    n
  }
  mycols <- grDevices::rainbow(attr(dhc21,"members"))
  i <- 0
})
dL <- dendrapply(dhc21, colLab)
plot(dL) ## --> colored labels

enter image description here


You can read all about doing this by careful study of ?dendrapply and ?as.dendrogram

Upvotes: 2

Related Questions