Vitória Stavis
Vitória Stavis

Reputation: 11

For a dendrogram plot in R, how to add custom text while coloring the branches according to the clusters?

I have this dendrogram: hc <- hclust(dist_s, method = 'average')

At first, I was showing it with dendextend:

dend = as.dendrogram(hc)
par(mar = c(3, 2, 2, 8)) 
dend %>% 
  set("labels_cex", 0.9) %>% 
  set("branches_k_color", 
             value = c("#E69F00", "#56B4E9", "#009E73"), k = 3) %>% 
  plot(main = "Hierarchical clustering", horiz= TRUE)

I need to add a custom subtitle to explain the samples, like a footnote text. There are a few solutions so my first question is: how to do that using dendextend?

I was able to add this text using ggplot. but using this method I could not color the branches according to the cluster assignments. So the complementary question is: would be how to correctly map the branch colors to the clusters.

I don't know which approach is easier to fix.

These are my trials using ggplot, with the correct added text but having problems with coloring.

dend_data <- dendro_data(as.dendrogram(hc), type = "rectangle")

cluster_colors <- c("orange", "green", "blue") 
clusters <- c(1, 2, 2, 3, 3, 3, 3)  # cluster assignments

#where to add the clusters column?
dend_data$segments$cluster <- clusters

p <- ggplot(dend_data$segments) + 
  ggtitle("Hierarchical clustering") +
  geom_segment(aes(x = x, y = y, xend = xend, yend = yend, color = factor(cluster)))+
  scale_colour_manual(values = cluster_colors) +
  geom_text(data = dend_data$labels, aes(x, y, label = label),
            hjust = 1, angle = 90, size = 3)+
  geom_text(x = x_coord, y = y_coord,
            label = "this is my custom text",
            size = 3, color = "black")

I am aware that to map the color to the cluster, it is necessary a column in my data assigning each sample to a cluster. I have this column here:

clust_df <- mutate(as.data.frame(tpms), cluster = cut)

In clust_df$cluster I can get this info, but in dend_data, I don't know where to find it or where to add it. In the ggplot code above I tried to added it to dend_data$segments, but I got the following error: Error in $<-.data.frame(*tmp*, cluster, value = c(1, 2, 2, 3, 3, 3, : replacement has 7 rows, data has 24 I also tried to add it to $labels:

#[...]
dend_data$labels$cluster <- clusters
#[...]
  geom_segment(aes(x = x, y = y, xend = xend, yend = yend, color = factor(labels$cluster)))+
#[...]

But then I got the error:

Error in geom_segment(aes(x = x, y = y, xend = xend, yend = yend, color = factor(labels$cluster))) :

Error occurred in the 1st layer. Caused by error in labels$cluster: object of type 'closure' is not subsettable

and like this:

geom_segment(aes(x = x, y = y, xend = xend, yend = yend, color = factor(dend_data_s$labels$cluster)))+

I got the error:

Error in geom_segment(aes(x = x, y = y, xend = xend, yend = yend, color = factor(dend_data_s$labels$cluster))) :

Error occurred in the 1st layer. Caused by error in check_aesthetics(): Aesthetics must be either length 1 or the same as the data (24) Fix the following mappings: colour

The dend_data object looks like this: dend_data

I have also tried to add colorful rectangles and to color the labels according to the clusters, but ended up with similar problems.

And last but not least, I was able to add colorful rectangles this way:

rect.hclust(dendr_l, k = 3, border = c("orange", "green", "blue"))

but then I cannot add the custom text.

Upvotes: 1

Views: 168

Answers (0)

Related Questions