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

R中的树状图:如何正确悬挂树枝(在给标签上色后)?,r,colors,dendrogram,dendextend,R,Colors,Dendrogram,Dendextend,我已经讨论了关于这个话题的其他问题,并设法“部分”得到了我所需要的。我希望我的树状图的叶子是彩色编码的。每个假期代表一个市场,我的DF中有另一列,通过颜色代码“红色”、“黄色”或“绿色”(编码为数字:“1”、“2”、“3”)告诉我它是什么类型的市场。每个市场都有一个色码。我希望标签是市场本身,但标签的颜色是基于颜色代码 Labels <- DF$Markets color_codes <- DF$Type Data_scale # obtained after remo

我已经讨论了关于这个话题的其他问题,并设法“部分”得到了我所需要的。我希望我的树状图的叶子是彩色编码的。每个假期代表一个市场,我的DF中有另一列,通过颜色代码“红色”、“黄色”或“绿色”(编码为数字:“1”、“2”、“3”)告诉我它是什么类型的市场。每个市场都有一个色码。我希望标签是市场本身,但标签的颜色是基于颜色代码

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)
Labels我需要“挂起”标签,而不是将它们全部标在一个高度上。以下是成功的秘诀:

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")

我需要“挂起”标签,而不是将它们全部标在一个高度上。以下是成功的秘诀:

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")