简体   繁体   中英

Dendrogram in R: how to properly hang branches (after coloring the labels)?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM