简体   繁体   English

在r中决定树状图(在树中将树状图背靠背放置)

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

Is there any fairly straight forward way of placing two dendrogram 'back to back' in r? 是否有任何相当直接的方法在r中放置两个“背对背”的树状图? The two dendrograms contain the same objects but are clustered in slightly different ways. 两个树形图包含相同的对象,但是以稍微不同的方式聚类。 I need to emphasise how the dendrograms differ. 我需要强调树形图的不同之处。 So something like what has been done with the soilDB package but perhaps less involved and soil science orientated? 就像使用soilDB软件包所做的那样,但可能更少涉及土壤科学的定位?

在此输入图像描述

It would be great to be able to be able to line the dendrograms to maximise the number of straight lines going between objects (see above) as this would emphasise any differences between the dendrograms. 能够对树状图进行排列以最大化物体之间的直线数量(见上文)会很棒,因为这会强调树形图之间的任何差异。

Any ideas? 有任何想法吗?

There may be a simpler way but I don't see it so here it is from scratch: 可能有一种更简单的方法,但我没有看到它,所以这里是从头开始:

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

在此输入图像描述

I can't think of a way to do the permutations to optimize the straight arrows though (I'm not very familiar with drawing dendrograms to begin with), so if anyone have an idea you're welcome to comment, edit or add your own answer. 我想不出一种方法来进行排列以优化直箭(我不熟悉绘制树形图),所以如果有人有想法,欢迎你评论,编辑或添加你的自己的答案。

I suspect one should use package ape , which is a package with functions to manipulate phylogenetic trees. 我怀疑应该使用package ape ,这是一个具有操作系统发育树的功能的包。

What you are looking for is called a Tanglegram plot which is used to visually compare dendrograms. 您正在寻找的是一个Tanglegram图 ,用于在视觉上比较树形图。

An implementation tanglegram is available in the package dendextend in R . R中的包dendextend中提供了一个实现tanglegram In fact it has been developed based on the above code by plannapus Several associated functions are also available for getting plots with minimum entanglements such as untangle_step_rotate_2side . 事实上,它是基于上述代码通过plannapus开发的。几个相关的函数也可用于获取具有最小纠缠的图,例如untangle_step_rotate_2side

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM