[英]Color dendrogram branches based on external labels uptowards the root until the label matches
從問題使用現有色譜柱的樹狀圖的顏色分支 ,我可以為樹形圖的葉子附近的樹枝着色。 編碼:
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)
生成樹狀圖,如下所示: 但是,我想將樹枝朝向根部着色,直到當前分支中的所有樹葉具有相同的標簽。 即使有一個不匹配的開關到默認的黑色。 我希望得到的樹形圖看起來像
我想要的與使用color_branches
有點不同
x.clust.dend <-color_branches(x.clust.dend,k=3)
因為它的顏色基於自己的集群而不是基於某些外部標簽。
您正在尋找的功能是branches_attr_by_clusters
。 以下是如何使用它:
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的結果。 請參閱插圖以獲取另一個示例 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.