根据标签重新排序和颜色树形图

时间:2017-10-10 10:42:25

标签: r dendrogram hclust dendextend

我试图控制树状图的顺序和颜色。显然,dendorgram的要点是按相似性排序,但在分支内我想设置一个有意义的顺序(字母数字)。

library(vegan)
library(stats) 

x <-data.frame(data = c(1:10)) 
y = data.frame(type = c("A","B","C","A","C","D","A","B","C","B"), site_name = c("A1","B1","C1","A2","C2","D1","A3","B2","C3","B3"))  
row.names(x) = y$site_name 
dis = vegdist(x) 
hc <- hclust(dis) 
dd <- as.dendrogram(hc) 
plot(dd)

enter image description here

我的数据标签是文字,但它们确实有变量

中列出的设定顺序
site_order = c("A1","A2","A3","B1","B2","B3","C1","C2","C3","D1")

1)我想找到一个解决方案,根据分支内的site_order对树形图进行排序。

  

e.g。 A1,B1,A2,C1,C2,D2,A3,B2,B3,C3

我还想使用site_type为标签着色和整形 例如(A =红色圆圈,B =蓝色方形,C =绿色三角形,D =黄色十字形)

这可能吗?

1 个答案:

答案 0 :(得分:0)

这是使用dendextend中的rotate函数最好的工作。

library(vegan)
library(stats) 

x <-data.frame(data = c(1:10)) 
y = data.frame(type = c("A","B","C","A","C","D","A","B","C","B"), site_name = c("A1","B1","C1","A2","C2","D1","A3","B2","C3","B3"))  
row.names(x) = y$site_name 
dis = vegdist(x) 
hc <- hclust(dis) 
dd <- as.dendrogram(hc) 

par(mfrow = c(1,2))
plot(dd, main = "orig")

library(dendextend)
dd2 <- rotate(dd, sort(labels(dd)) )
plot(dd2, main = "as sorted as possible \n(under the constraints)")

输出:

enter image description here

您可以从the online vignettes了解有关dendextend软件包的更多信息。