1
votes

I have a phylogenetic tree that I drew on R. I want to color my tip edges based on the order of my species. How can I choose the color of every tip label alone? I tried first:

EdgeCols <- rep("black", Nedge(tree))
EdgeCols[which.edge(tree, tree$edge[1]) ] <- "red"
plot( tree, space = 30, assoc = AMat, 
            show.tip.label = T, gap = 1,  length.line = 0,  edge.color =EdgeCols1)

But I would not get any change in the color of this edge.

Can anyone tell me where the problem is?

1
Should be edge.color =EdgeCols? A typo? Which package did you use to plot the tree? I guess it's ape. Can you give a minimum example?Ven Yao

1 Answers

2
votes

I am not exactly sure what you are trying to do, but here is how to color specific edges of a phylogeny with the ape package. Here is code for coloring all edges:

library(ape)

# Simulate tree
ntax <- 20
tree <- rcoal(ntax)

# Color branches
colors <- rainbow(Nedge(tree))
plot(tree, edge.color=colors)

And for coloring all terminal branches:

# Color terminal branches
colors2 <- rep("black", Nedge(tree))
colors2[which(tree$edge[,2] %in% 1:20)] <- rainbow(ntax)
plot(tree, edge.color=colors2)

I also would point out that there are obvious issues in your code:

  1. You have tree$edge[1], but tree$edge is a matrix, so you can't index it with one value.
  2. The which.edge function requires a vector of tips and returns the index of all the edges within the monophyletic clade defined by those tips. It seems like you are trying to give it a single value, which doesn't make any sense.
  3. You define EdgeCols, but then in your plot function you have EdgeCols1.