2

I made a cluster analysis using simple following code

hc2 = hclust(dist(geno.imp))
pdf(file="file.pdf", width=50)
plot(hc2,cex=0.2) 
dev.off()

I want to highlight some of the specific leaves (not the nodes). I have the list of those leaves in a separate vector. How do I do highlight only specific leaves, keeping all other leaves black?

Tal Galili
  • 24,605
  • 44
  • 129
  • 187

2 Answers2

2

Have a look at ?dendrapply. dendrapply allows you to apply a function to each node of a dendrogram. In that function you could change the properties of the node, e.g.:

## create some example data
set.seed(1)
highlight <- rownames(USArrests)[sample(nrow(USArrests), 10)]

## function to change color etc. of a leaf
colorLeafs <- function(x) {
  if (is.leaf(x) && attr(x, "label") %in% highlight) {
    attr(x, "nodePar") <- list(lab.col="red", pch=NA)
  }
  return(x)
}

hc <- hclust(dist(USArrests), "ave")

dd <- dendrapply(as.dendrogram(hc), colorLeafs)

plot(dd)

enter image description here

sgibb
  • 25,396
  • 3
  • 68
  • 74
  • Hi sgibb, thank you for your answer. I just gave a more detailed answer based on the dendextend package. I would appreciate it if you could give your feedback on my answer. With regards, Tal – Tal Galili Aug 11 '14 at 07:20
  • 1
    @TalGalili: The *dendextend* package is great (and I didn't know it in April 2014) and makes this example much easier. – sgibb Aug 20 '14 at 16:57
2

sgibb answer is a good one for a specific case. What you could also try is using the dendextend package, designed exactly for this sort of thing.

Here are two ways in which you would get the same result as in the example of sgibb:

dend <- as.dendrogram(hclust(dist(USArrests), "ave"))
## create some example data
set.seed(1)
highlight <- rownames(USArrests)[sample(nrow(USArrests), 10)]

install.packages("dendextend")
library(dendextend)
# create a dendrogram with colored labels:
dend2 <- color_labels(dend, labels = highlight , col = 2) 
# ploting it
plot(dend2)

# Here is a second way for doing the same thing:
dend2 <- color_labels(dend, col = ifelse(labels(dend) %in% highlight, 2, 1)) 
plot(dend2)

enter image description here

Tal Galili
  • 24,605
  • 44
  • 129
  • 187