【问题标题】:Color branches of a dendrogram based on column in dataframe基于数据框中列的树状图的颜色分支
【发布时间】:2019-09-08 05:26:50
【问题描述】:

我想根据 hclust 函数中使用的数据框列中的值为树状图的分​​支着色。

在您将此问题标记为重复之前,就像在此question 中所做的那样,它链接到此question。请注意,这实际上从未在答案中完全解决。根据树状图的拓扑为分支着色很容易,但我不知道如何根据 hclust 函数中使用的数据框中的列为分支着色。

我尝试过以两种非常相似的方式使用 dendextend 包:

library(dendextend)
par(mar = c(2,1,0,8)) #make sure the whole plot is on the page
hc <- hclust(dist(mtcars)) #cluster dataframe based on distance
dend <- as.dendrogram(hc) #use dendextend to create dendrogram
dend2 <- color_branches(dend, col = mtcars$cyl) #attempt but fail at coloring branches
plot (dend2, horiz = TRUE) #plot dendrogram

dend3 <- assign_values_to_leaves_edgePar(dend, value = mtcars$cyl, edgePar = "col") #attempt but fail at coloring branches
plot (dend3, horiz = TRUE) #plot dendrogram

factor(mtcars$cyl 替换mtcars$cyl 也不能解决问题。

这两种解决方案都会生成未正确着色的树状图。 它似乎是根据 cyl 列中值的顺序从树形图的底部到顶部对颜色进行排序,但由于分支不再按该顺序排列,因此着色没有任何意义。我不希望对数据框进行排序以解决此问题。

谢谢。

【问题讨论】:

    标签: r dendrogram hclust dendextend


    【解决方案1】:

    您需要按照树状图的叶子顺序排列颜色。您可以使用labels() 提取叶子上使用的名称

    dend2 <- color_branches(dend, col=mtcars[labels(dend),"cyl"])
    

    【讨论】:

    • 谢谢!我知道有一个简单的解决方案。
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2021-10-11
    • 2019-11-23
    • 1970-01-01
    • 2018-03-21
    • 2015-12-02
    • 2015-07-15
    • 2017-07-24
    相关资源
    最近更新 更多