Search code examples
rdendextend

How to label just one observation in hierarchical clustering tree with dendextend?


I'd like to create a hierarchical clustering tree of a relatively large dataset (>3000 obs). Unfortunately, by including so many labels at the terminal nodes, the tree looks very cluttered and contains lots of unnecessary information. So to reduce the clutter, I'd like to just label one observation of interest. I have removed all of the labels but I don't know how to retrieve and add the label that I'm interested in.

For this MWE, let's assume, I'd like to add the letter k to my dendrogram.

library(dendextend)
library(cluster)
library(tidyverse)
set.seed(1)
a <- rnorm(20)
b <- rnorm(20)
c <- rnorm(20)

df <- as.data.frame(a, b, c)
names(df) <- letters[length(df)]

my_dist <- dist(df)
my_clust <- hclust(my_dist)
my_dend <- as.dendrogram(my_clust)
plot(color_branches(my_dend, k = 3), leaflab = "none", horiz = T)

Solution

  • You can specify the labels set function. If you only want to show one, make the others be the null string.

    LAB = rep("", nobs(my_dend))
    LAB[15] = "N15"
    my_dend = set(my_dend, "labels", LAB) 
    plot(color_branches(my_dend, k = 3), horiz = T)
    

    enter image description here