简体   繁体   中英

How to get the node order of phylogenetic tree in R?

I used the R package ggtree to plot a phylogenetic tree and I need to know the order of the tips so that I can combine it with specific information for each tip. Of course I can manually record the order, but I have a lot of these trees. What's more, tr$tip.label gives me a different order of nodes comparing to the order showing in the tree plot.

I need is some R scripts that can get a correct order of tips from a phylogenetic tree object, which should be the same order when R plot the tree.

The tree:

tr$tip.label gives:

"seq6" "seq16" "seq12" "seq34" "seq5" "seq35" "seq41" "seq19" "seq22" "seq54" "seq7" "seq26" "seq9" "seq14" "seq4" "seq8" "seq33" "seq11" "seq2" "seq13" "seq45" "seq42" "seq50" "seq3" "seq18" "seq29" "seq52" "seq53" "seq32" "seq21" "seq38" "seq51" "seq24" "seq47" "seq48" "seq43" "seq39" "seq23" "seq28" "seq1" "seq25" "seq40" "seq46" "seq36" "seq15" "seq49"

What I want:

Each tree tip aligns to a line with a specific color.

> library(ggtree)
> set.seed(123)
> tr = rtree(15)
> d=fortify(tr)
> dd = subset(d, isTip)
> dd$label[order(dd$y, decreasing=TRUE)]
 [1] "t5"  "t8"  "t4"  "t10" "t12" "t1"  "t2"  "t3"  "t11" "t13" "t9"  "t6" 
[13] "t14" "t7"  "t15"

Here is an example of getting the tip labels corresponding to their positions.

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