user3533677
user3533677

Reputation: 21

How can I highlight specific labels in a dendrogram?

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?

Upvotes: 2

Views: 2240

Answers (2)

Tal Galili
Tal Galili

Reputation: 25306

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

Upvotes: 2

sgibb
sgibb

Reputation: 25736

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

Upvotes: 2

Related Questions