3

質問Color branches of dendrogram using an existing columnから、私は樹状図の葉の近くの枝に色を付けることができます。コード:に示すように外部のラベルに基づく樹状図の色分けは、ラベルが一致するまで根元から上方に移動します

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) 

は、樹形図を生成します:enter image description here しかし、私は、現在のブランチ内のすべての葉が同じラベルを持つまでのルートに向けて枝をアップ色にしたいです。単一の不一致があっても、デフォルトの色は黒に変わります。私は私がしたいことは、いくつかの外部のラベルに基づいていない、独自のクラスタに基づいてcolor_branches

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

のような、それなぜなら色を使用しては少々異なっていた系統樹が enter image description here

に見えるようにしたいです。

答えて

1

あなたが探している機能はbranches_attr_by_clustersです。ここではそれを使用する方法である:この機能は、もともとdynamicTreeCutの結果を表示するために作成された

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) 

enter image description here

the vignette for another exampleを参照してください。

+1

ありがとうございます!正確に私が望んでいたとリンクのために、非常に便利。 – discipulus

+1

:)喜んで。 –

関連する問題