How to rename labels in dendrogram (dendextend)

Hey all,

The labels of my dendrogram contain too much unnecessary information, so I want to change them into shorter versions. Additionally, all the labels are colored by their group affiliation.

ward <- as.dendrogram(hclust(bc_dist, method = "ward.D2"))
metad <- data.frame(phyloseq::sample_data(gps_rel_abund))
colorCode <- c(`1` = "cyan3", `2` = "tomato", `3` = "mediumorchid", `4` = "chartreuse4")
labels_colors(ward) <- colorCode[metad$group][order.dendrogram(ward)]
par(mar=c(7, 3, 1, 1))

Bild 07.11.20 um 17.37

Now I want to have the labels without the first three and the last two numbers: 012.4A16 -> 4A. But without changing anything else (color, group affiliation) in the dendrogram.
I searched the web for instructions on how to do it, but or the code was too complex for me to understand it, or it changed my group-colors.

Could anyone of you please tell me if it is possible to make these changes and if so, how?

Thank you so much for your help!

A simple way to do this could be to change the names in your original dataset rather than trying to rename the labels. Based on your screenshot, it looks like the two digits that you want to keep are enough to uniquely identify each observation, is that correct?

In this case and if your original data has a column that contains the names, you could for example use tidyr::separate to create a column with the short names in your original data.

The functions works like this:


#sample data

#split up the names after the forth and sixth digit
  separate(name,into=c(NA,"shortNname",NA),sep=c(4,6),remove = FALSE)
#>       name shortNname
#> 1 012.4C16         4C
#> 2 002.1B16         1B
#> 3 010.4A16         4A
#> 4 006.2C16         2C

Created on 2020-11-09 by the reprex package (v0.3.0)

If you need help in how to do this in your own data, it would be good if you could post a full reproducible example.

Thank you for your answer.
I will try this the next days.

Because the labels are the SampleIDs I decided not to change them.
Finally, I found this solution:

# save as dendrogram
ward <- as.dendrogram(hclust(bc_dist, method = "ward.D2"))
#provide color codes to the labels
metad <- data.frame(phyloseq::sample_data(gps_rel_abund))
colorCode <- c(`1` = "cyan3", `2` = "tomato", `3` = "mediumorchid", `4` = "chartreuse4")
labels_colors(ward) <- colorCode[metad$group][order.dendrogram(ward)]
# check the current label-order and names
# check order and names of the labels
# rename and plot
ward %>% set("labels", c("1C31", "2A31", "1B31", "4C31", "2C31", "4A31", "2B31", "1A31", "3B31", "3C31", "3A31", "4B31")) %>% plot(main="Dendrogram day 42")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.