In R, how to order a vector of data according to both tips and edges of a phylogenetic tree?

StackOverflow https://stackoverflow.com/questions/22100809

  •  18-10-2022
  •  | 
  •  

سؤال

I want to plot a phylogenetic tree with colors according to some values estimated for each branch of that tree. According to plot.phylo and various functions of phytools, it is possible: plot.phylo(tree,edge.color=col) but the vector col must follow the order of tree$edge.

Here is the issue: my data include both edges and tips, is that possible to plot both in a consistent palette?

Short example:

test.mod:
((a:1,b:1)a-b:1,(c:1,d:1)c-d:1)a-d;

test.data:
a 0.5
b 0.6
a-b 1
c 2.5
d 2
c-d 2.1
a-d 1.5

Thanks in advance, Xavier

هل كانت مفيدة؟

المحلول

It depends what you are trying to achieve. Some approaches could be easily automatized (e.g. my second example).

IMHO you have to color your tree manually if you want to give each branch a different color depending on its taxonomia or something like that, e.g.:

library("ape")
tree <- read.tree("tree.mod")

tree$edge
#     [,1] [,2]
#[1,]    5    6 # root -> a:b
#[2,]    6    1 # == "a"
#[3,]    6    2 # == "b"
#[4,]    5    7 # root -> c:d
#[5,]    7    3 # == "c"
#[6,]    7    4 # == "d"

cols <- rep(c("red", "green"), each=3)
plot.phylo(tree, edge.col=cols)

enter image description here

An easy example which could be done automatically is to color the last leaves, e.g.:

ntips <- length(tree$tip.label)
cols <- c(rainbow(ntips), rep("black", nrow(tree$edge)-ntips+1))
plot.phylo(tree, edge.col=cols[tree$edge[,2]])

enter image description here

EDIT: You have to order the labels by tree$edge[,2] before matching them. Please find an example below:

library("ape")
tree <- read.tree("tree.mod")

myColors <- c(node="black", a="red", b="green", c="blue", d="purple")
myLabels <- c(tree$tip.label, tree$node.label)

## match colors and labels (nomatch == node => select idx 1)
## (myLabels are reordered by edge ordering
selColors <- myColors[match(myLabels[tree$edge[,2]], names(myColors), nomatch=1)]

plot.phylo(tree, edge.col=selColors)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top