2017-02-02 41 views
2

我有一個dendrogram切一個樹狀

set.seed(10) 
mat <- matrix(rnorm(20*10),nrow=20,ncol=10) 
dend <- as.dendrogram(hclust(dist(mat))) 

,並給予深度截止:

我想削減是要到截止正確的所有分支。

depth.cutoff <- 4.75 

我想切斷所有分支的虛線右側:此dendrogram落得

plot(dend,horiz = TRUE) 
abline(v=depth.cutoff,col="red",lty=2) 

enter image description here

和:

enter image description here

我得到的最接近的是usin g apedrop.tip,但問題在於如果我的depth.cutoff包含所有葉子,如本例中所示,則返回NULL

也許任何人都知道是否和如何我可以刪除它表示的nested list元素我dendrogram如果他們的depth低於depth.cutoff

或者,也許我可以轉換dendrogramdata.frame,其中還列出了各node(包括葉,其將具有depth = 0)的depth,與來自data.framedepth<depth.cutoff刪除所有行,然後轉換該回到dendrogram

回答

2

cut將在指定的高度切割樹。這將返回upperlower部分

cut(dend, h = depth.cutoff)$upper 

# $upper 
# 'dendrogram' with 2 branches and 5 members total, at height 5.887262 
# 
# $lower 
# $lower[[1]] 
# 'dendrogram' with 2 branches and 6 members total, at height 4.515119 
# 
# $lower[[2]] 
# 'dendrogram' with 2 branches and 2 members total, at height 3.789259 
# 
# $lower[[3]] 
# 'dendrogram' with 2 branches and 5 members total, at height 3.837733 
# 
# $lower[[4]] 
# 'dendrogram' with 2 branches and 3 members total, at height 3.845031 
# 
# $lower[[5]] 
# 'dendrogram' with 2 branches and 4 members total, at height 4.298743 


plot(cut(dend, h = depth.cutoff)$upper, horiz = T) 

enter image description here

+0

非常感謝@SymbolixAU。你知道是否有辦法讓所有分支在同一條線上結束?不是在情節中,而是在對象中? – dan

2

一個讓你的圖片或許更直接的方式的名單只是設置要繪製的限制。

plot(dend, horiz = TRUE, xlim=c(6,4.75)) 

Cropped Dendrogram

+0

如果所有的OP都想繪製一個特定的部分,那麼它肯定是更方便的方式。 – SymbolixAU