简体   繁体   中英

Reorder and colour dendogram based on labels

I am trying to control the order and colour of a dendrogram. Obviously the point of the dendorgram is to order by similarity, but within branches I'd like to set an order that make sense (alphabetical-numeric).

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)

在此处输入图片说明

My data labels are text but they do have a set order listed in a variable

site_order = c("A1","A2","A3","B1","B2","B3","C1","C2","C3","D1")

1) I'd like to find a solution that sorts the dendogram according to site_order within branches.

eg A1,B1, A2, C1, C2,D2, A3,B2, B3,C3

I also want to colour and shape the labels using site_type eg (A= red circle , B= blue square , C= green triangle, D = yellow cross)

Is this possible?

This is a job best done using the rotate function from dendextend.

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

Output:

在此处输入图片说明

You can learn more on the dendextend package from the online vignettes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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