2012-09-17 3 views
8

r에 'back to back'이라는 두 개의 dendrogram을 배치하는 방법이 있습니까? 두 개의 dendrogram은 동일한 객체를 포함하지만 약간 다른 방식으로 클러스터됩니다. 나는 그 멍크 그램이 어떻게 다른지 강조 할 필요가있다. 따라서 soilDB 패키지로 수행되었지만 덜 관련되어 토양 과학이 지향 된 것과 같은 것이 있습니까?r에 결투 dendrograms (r에 dendrograms를 연속적으로 배치)

enter image description here

dendrograms 사이의 차이를 강조하는 것이이 같은 개체 (위 참조) 사이에가는 직선의 수를 최대화하기 위해 dendrograms을 줄 수있을 수 있도록 좋은 것입니다.

아이디어가 있으십니까?

답변

10

는 간단한 방법이있을 수 있지만, 그래서 여기가 처음부터입니다 표시되지 않습니다

# 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)) 

enter image description here

내가 최적화하기 위해 순열을 할 수있는 방법을 생각할 수 없다 직선 화살표 (처음에는 드로잉 드로잉에 익숙하지 않습니다.) 누군가가 아이디어를 가지고 있다면 자신의 대답을 편집하거나 추가 할 수 있습니다.

나는 ape 패키지를 사용해야한다고 생각합니다.이 패키지는 계통 발생 수를 조작하는 기능이있는 패키지입니다.

2

당신이 찾고있는 것은 이라고 부릅니다. 얽힌 그램을 시각적으로 비교하는 데 사용되는 Tanglegram 플롯입니다.

tanglegram 패키지는 dendextendR 패키지로 제공됩니다. 실제로 이것은 위의 코드를 기초로하여 개발되었습니다 plannapusuntangle_step_rotate_2side과 같은 최소 얽힘을 갖는 플롯을 얻기 위해 몇 가지 연관된 기능을 사용할 수 있습니다.