1

I am using the R package adegenet to plot the neighbor-joining tree.

In my file I have 20,000 columns and 500 rows. Rows correspond to individuals. My first column is Population ID and second column is Individual ID. Columns contain values 0,1 & 2. I am able to plot a tree in one color, but depending upon the population I want every cluster to be a different color.

This is what I did, If "dat" is my data file,then

D<-dist(as.matrix(dat))
tre<-nj(D)
plot(tre, type = "unr", show.tip.lab = TRUE, cex=0.3, font=1, edge.col="Blue")

If I try edge.col=c("red","green","blue") I run into following error :

Error in if (use.edge.length) unrooted.xy(Ntip, Nnode, z$edge, z$edge.length,  : 
  argument is not interpretable as logical

Ill appreciate any help!

Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38
akang
  • 566
  • 2
  • 15

1 Answers1

1

Your example should be reproducible, so that it would be easier to help and reproduce your problem. See this post for more details. I'm trying with iris and it works like a charm. By the way, I think adegenet is not required here, the plot is actually a plot.phylo from the package ape), and all other functions are either built-in or from ape).

Documentation (?plot.phylo) says:

edge.col a vector of mode character giving the colours used to draw the branches of the plotted phylogeny. These are taken to be in the same order than the component edge of phy. If fewer colours are given than the length of edge, then the colours are recycled.

ape preserves the order or rows, and you can use a factor to index you vector of colors, so a reproducible example using iris could be:

library(ape)
D <-dist(as.matrix(iris[, 1:4]))
tree <- nj(D)
plot(tree, type = "unr", show.tip.lab = TRUE, cex=0.3, font=1, 
           edge.col=c("red","green","blue")[iris$Species])

Is that what you want?

Community
  • 1
  • 1
Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38
  • Thanks @Vincet! That is what i was looking for. I wonder if there is a way of selectively deleting the samples by name? Some samples are ruining the tree bigtime. – akang Mar 28 '16 at 02:58