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")
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.
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
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With