I have gone through other questions asked on this topic and have managed to "partially" get what I need. I want my dendrogram 's leaves to be color coded. Each leave represents a market and I have another column within my DF that tells what type of market it is via color codes "Red", "Yellow" or "Green" (which have be encoded as numbers: "1", "2", "3"). Each market has a color code. I want the labels to be the markets themselves, but the color of the labels to be based on color codes.
Labels <- DF$Markets
color_codes <- DF$Type
Data_scale # obtained after removing the columns of 'Markets' and 'Type'
# from DF and scaling it.
row.names(Data_scale) <- Labels
hc <- hclust(dist(Data_scale)))
dend <- as.dendrogram(hc)
colors_to_use <- color_codes
colors_to_use <- colors_to_use[order.dendrogram(dend)]
labels_colors(dend) <- colors_to_use
plot(dend, cex = 0.8)
My issue is that when I plot this, labels do get coded but the tree is really elongated. It is so long that the labels too are being cut off. What do I do?
I needed to "hang" the labels rather than plot them all on a single height. Following did the trick:
dend %>% set("leaves_pch", 19) %>% set("leaves_cex", 2) %>%
set("leaves_col", 2) %>% # adjust the leaves
hang.dendrogram(dend, hang_height = 0.01) %>% # hang the leaves
plot(main = "Hanging a tree")
https://cran.r-project.org/web/packages/dendextend/vignettes/introduction.html