Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show only selected labels in circular dendrogram

I am trying to plot some dendrograms using dendextend package in R.

How to show only selected labels in a circular dendrogram?

library(dplyr)
library(dendextend)

mtcars %>% 
  select(mpg, cyl, disp, drat, hp, carb) %>% 
  dist() %>% 
  hclust() %>% 
  as.dendrogram() -> dend

With a rectangular dendrogram, it can be done by using blank labels.

leafcols <- as.numeric(mtcars[, "am"])
leafcols <- leafcols[order.dendrogram(dend)]
leafcols[leafcols == 1] <- "darkred"
leafcols[leafcols == 0] <- "darkblue"

new_labels <- dend %>% labels
new_labels[!grepl("^Merc", new_labels)] <- ""

dend %>% 
  set("leaves_pch", 19) %>% 
  set("leaves_cex", 2) %>% 
  set("leaves_col", leafcols) %>% 
  set("labels", new_labels) %>%
  plot(type = "rectangle")

enter image description here

But with circlize_dendrogram, I am getting a warning Not all labels are unique. Therefore, we pad the labels with a running number, so to be able to produce the plot.

enter image description here

dend %>% 
  set("leaves_pch", 19) %>% 
  set("leaves_cex", 2) %>% 
  set("leaves_col", leafcols) %>% 
  set("labels", new_labels) %>%
  circlize_dendrogram(dend_track_height = 0.8)

How to enable use of blank labels with circlize_dendrogram.

like image 733
Crops Avatar asked Aug 31 '25 03:08

Crops


1 Answers

Do you accept a hacky approach, where we color labels which don't start with Merc in white to "hide" them?

library(dplyr)
library(dendextend)

dend <- mtcars %>% select(mpg, cyl, disp, drat, hp, carb) %>% 
dist() %>% hclust() %>% as.dendrogram()
leafcols <- c("darkblue", "darkred")[mtcars$am[order.dendrogram(dend)] + 1]

dend %>% 
  set("leaves_pch", 19) %>% 
  set("leaves_cex", 2) %>% 
  set("leaves_col", leafcols) %>% 
  set("labels_col", ifelse(grepl("^Merc", labels(dend)), "black", "white")) %>% 
  circlize_dendrogram(dend_track_height = 0.8)

out

like image 67
Tim G Avatar answered Sep 02 '25 18:09

Tim G