3

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

Elizabeth
  • 6,391
  • 17
  • 62
  • 90
  • 1
    n is not causing the problem, that's a local variable from the function. But you have five colors specified, and there's a k=12 so you have 12 groups. You set your labels to FALSE, but the function uses the labels. Without your data it's impossible to check what exactly went wrong, but try reading the code instead of blindly copying it. – Joris Meys May 13 '12 at 11:44
  • 1
    Are you trying to color the branches or branch labels? – Roman Luštrik May 13 '12 at 12:31

2 Answers2

2

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

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Hi @Andrie Thanks for your reply. I tried the following: dhc <-as.dendrogram(hc <-hclust(Gdis, method ="average, members=NULL)) 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(dhc,"members")) i <- 0}) dL <- dendrapply(dhc, colLab) plot(dL) but keep getting the error message Error: unexpected symbol in: " local({colLab <<- function(n) {if(is.leaf(n)) {a <- attributes(n) i <<- i+1 attr(n, "edgePar" any ideas what might be going wrong? – Elizabeth May 13 '12 at 16:39
  • @Elizabeth Your code and example isn't reproducible, so it's hard to say what you are doing wrong. – Andrie May 13 '12 at 17:24
1

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

gringer
  • 39
  • 5