NewUsr_stat
NewUsr_stat

Reputation: 2583

Highlight and color a specific node on a dendrogram plotted on a heatmap

I have a dendrogram plotted on a heatmap. How can I select and color only one node and the corresponding leaves? Thanks in advance!

Eleonora

Upvotes: 4

Views: 1461

Answers (1)

Andrie
Andrie

Reputation: 179448

Make use of dendrapply to traverse the nodes. There is a worked example in ?dendrapply that illustrates how to set the colour of nodes:

require(graphics)

## a smallish simple dendrogram
dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave"))
(dhc21 <- dhc[[2]][[1]])

## too simple:
dendrapply(dhc21, function(n) utils::str(attributes(n)))

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

enter image description here

Upvotes: 6

Related Questions