0

I'd like to colour the nodes of a dendrogram at the bottom of the dendrogram - but in separate rows This last dendrogram in the link below shows the nodes as coloured - however when its a big data set that one line gets very smeary Label and color leaf dendrogram in r So is is possible to have separate rows for each coloured group ?

Community
  • 1
  • 1
user3156942
  • 163
  • 1
  • 8

1 Answers1

0

Yes. You want to use the function colored_bars from the dendextend package. Please see the following example (from the dendextend vignette)

library(dendextend)
dend15 <- c(1:5) %>% dist %>% hclust(method = "average") %>% as.dendrogram
is_odd <- ifelse(labels(dend15) %% 2, 2,3)
is_345 <- ifelse(labels(dend15) > 2, 3,4)
is_12 <- ifelse(labels(dend15) <= 2, 3,4)
k_3 <- cutree(dend15,k = 3, order_clusters_as_data = FALSE) 
# The FALSE above makes sure we get the clusters in the order of the
# dendrogram, and not in that of the original data. It is like:
# cutree(dend15, k = 3)[order.dendrogram(dend15)]
the_bars <- cbind(is_odd, is_345, is_12, k_3)
the_bars[the_bars==2] <- 8

dend15 %>% plot
colored_bars(colors = the_bars, dend = dend15)

enter image description here

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