Color dendrogram branches based on external labels uptowards the root until the label matches

喜欢而已 提交于 2019-12-07 14:42:31

问题


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

What I want is little different from using color_branches 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.


回答1:


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)

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



来源:https://stackoverflow.com/questions/36617699/color-dendrogram-branches-based-on-external-labels-uptowards-the-root-until-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!