简体   繁体   English

R中的树状图:如何正确悬挂树枝(为标签着色后)?

[英]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"). 每个休假代表一个市场,我在DF中还有另一列,它通过颜色代码“红色”,“黄色”或“绿色”(已编码为数字:“ 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 https://cran.r-project.org/web/packages/dendextend/vignettes/introduction.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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