簡體   English   中英

在r中決定樹狀圖(在樹中將樹狀圖背靠背放置)

[英]Duelling dendrograms in r (Placing dendrograms back to back in r)

是否有任何相當直接的方法在r中放置兩個“背對背”的樹狀圖? 兩個樹形圖包含相同的對象,但是以稍微不同的方式聚類。 我需要強調樹形圖的不同之處。 就像使用soilDB軟件包所做的那樣,但可能更少涉及土壤科學的定位?

在此輸入圖像描述

能夠對樹狀圖進行排列以最大化物體之間的直線數量(見上文)會很棒,因為這會強調樹形圖之間的任何差異。

有任何想法嗎?

可能有一種更簡單的方法,但我沒有看到它,所以這里是從頭開始:

# First two dummy clusters (since you didn't provide with some...)
hc1 <- hclust(dist(USArrests), "average")
hc2 <- hclust(dist(USArrests), "complete")

l <- length(hc1$order)

# The matrix to draw the arrows:
cbind((1:l)[order(hc1$order)],(1:l)[order(hc2$order)]) -> ord_arrow

# The two vectors of ordered leave labels:
hc1$labels[hc1$order]->leaves1
hc2$labels[hc2$order]->leaves2

# And the plot:
layout(matrix(1:5,nrow=1),width=c(5,2,3,2,5))

# The first dendrogram:
par(mar=c(3,3,3,0))
plot(as.dendrogram(hc1),horiz=TRUE,leaflab="none", ylim=c(0,l))

# The first serie of labels (i draw them separately because, for the second serie, I didn't find a simple way to draw them nicely on the cluster):
par(mar=c(3,0,3,0))
plot(NA, bty="n",axes=FALSE,xlim=c(0,1), ylim=c(0,l),ylab="",xlab="")
sapply(1:l,function(x)text(x=0,y=x,labels=leaves1[x], pos=4, cex=0.8))

# The arrows:
par(mar=c(3,0,3,0))
plot(NA, bty="n",axes=FALSE,xlim=c(0,1), ylim=c(0,l),ylab="",xlab="")
apply(ord_arrow,1,function(x){arrows(0,x[1],1,x[2],code=3, length=0.05, col="blue")})

# The second serie of labels:
par(mar=c(3,0,3,0))
plot(NA, bty="n",axes=FALSE, xlim=c(0,1), ylim=c(0,l), ylab="",xlab="")
sapply(1:l,function(x)text(x=1,y=x,labels=leaves2[x], pos=2, cex=0.8))

# And the second dendrogram (to reverse it I reversed the xlim vector:
par(mar=c(3,0,3,3))
plot(as.dendrogram(hc2),horiz=TRUE, xlim=c(0,max(dist(USArrests))), leaflab="none", ylim=c(0,l))

在此輸入圖像描述

我想不出一種方法來進行排列以優化直箭(我不熟悉繪制樹形圖),所以如果有人有想法,歡迎你評論,編輯或添加你的自己的答案。

我懷疑應該使用package ape ,這是一個具有操作系統發育樹的功能的包。

您正在尋找的是一個Tanglegram圖 ,用於在視覺上比較樹形圖。

R中的包dendextend中提供了一個實現tanglegram 事實上,它是基於上述代碼通過plannapus開發的。幾個相關的函數也可用於獲取具有最小糾纏的圖,例如untangle_step_rotate_2side

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM