彩色树状图根据外部标签向上分支直到根部匹配 [英] Color dendrogram branches based on external labels uptowards the root until the label matches

查看:153
本文介绍了彩色树状图根据外部标签向上分支直到根部匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自问题使用现有列的树状图的彩色分支,我可以为树状图的叶子附近的分支着色.代码:

From question Color branches of dendrogram using an existing column, I can color the branches near the leaf of the dendrogram. The code:

x<-1:100
dim(x)<-c(10,10)
set.seed(1)
groups<-c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
x.clust<-as.dendrogram(hclust(dist(x)))

x.clust.dend <- x.clust
labels_colors(x.clust.dend) <- groups
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = groups, edgePar = "col") # add the colors.
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = 3, edgePar = "lwd") # make the lines thick
plot(x.clust.dend) 

生成树状图,如下所示: 但是,我想为分支着色直到根,直到当前分支中的所有叶子都具有相同的标签.即使只有一个不匹配项,也会切换为默认的黑色.我希望生成的树状图看起来像

generates a dendrogram as shown in: However, I want to color the branches up towards the root until the all the leaves in the current branch have the same labels. Even if there is a single mismatch switch to the default color of black. I want the resulting dendrogram to look like

我想要的与使用

x.clust.dend <-color_branches(x.clust.dend,k=3)

因为它根据自己的群集而不是某些外部标签来着色.

because it colors based on its own clusters not based on some external labels.

推荐答案

您要查找的功能是branches_attr_by_clusters.使用方法如下:

The function you are looking for is branches_attr_by_clusters. Here is how to use it:

library(dendextend)

x <- 1:100
dim(x) <- c(10, 10)
set.seed(1)
groups <- c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
dend <- as.dendrogram(hclust(dist(x)))

clusters <- as.numeric(factor(groups, levels = c("red", "blue")))
dend2 <-
  branches_attr_by_clusters(dend , clusters, values = groups)
plot(dend2)

最初创建此函数是为了显示dynamicTreeCut的结果.请参见该小插图.

This function was originally created to display the results of dynamicTreeCut. See the vignette for another example.

这篇关于彩色树状图根据外部标签向上分支直到根部匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆