Graham Etherington
2009-Sep-17 13:00 UTC
[R] How to colour the tip labels in a phylogenetic tree
Hi, Using Ape, I have constructed an object of class "phylo", using the method 'nj' (lets call the object 'tree_ja'). I also have a given subset of 'tree_ja' in a vector (lets call the vector 'subspecies'). What I want to do, is construct a nj tree - plot(tree_ja) - but have the species in vector 'subspecies' shown as red at the tips of the tree. The closest I've come is this: Given that 'tree_ja$tip.label' provides the following: [1] "1_T1" "2_T1" "3_T1" "4_T1" "5_T1" "6_T1" [7] "7_T1" "8_T1" "9_T1" "10_T1" "11_T1" "12_T1" and that my 'subspecies' vector is: subspecies <- c("1_T1", "2_T1", "3_T1", "4_T1", "6_T1") which can also be written as: subspecies <- c(tree_ja$tip.label[1:4], tree_ja$tip.label[5]) I can construct a method which gives me the following statement: plot(tree_ja, tip.col = c('red', 'red', 'red', 'red', 'black', 'red', 'black', 'black', 'black', 'black', 'black', 'black')) But this doesn't work (at least not on my full dataset, which as 118 tips - reduced to 12 here for brevity) and I'm SURE there must be a better way of doing it. Could anyone help me with this? Many thanks, Graham -- Dr. Graham Etherington Post-doctoral Bioinformatician, Department of Computational and Systems Biology John Innes Centre Norwich Research Park Colney Norwich NR4 7UH UK
Graham Etherington wrote:> > Hi, > Using Ape, I have constructed an object of class "phylo", using the > method 'nj' (lets call the object 'tree_ja'). > I also have a given subset of 'tree_ja' in a vector (lets call the > vector 'subspecies'). > What I want to do, is construct a nj tree - plot(tree_ja) - but have the > species in vector 'subspecies' shown as red at the tips of the tree. > > The closest I've come is this: > Given that 'tree_ja$tip.label' provides the following: > [1] "1_T1" "2_T1" "3_T1" "4_T1" "5_T1" "6_T1" > [7] "7_T1" "8_T1" "9_T1" "10_T1" "11_T1" "12_T1" > > and that my 'subspecies' vector is: > subspecies <- c("1_T1", "2_T1", "3_T1", "4_T1", "6_T1") > which can also be written as: > subspecies <- c(tree_ja$tip.label[1:4], tree_ja$tip.label[5]) > > I can construct a method which gives me the following statement: > > plot(tree_ja, tip.col = c('red', 'red', 'red', 'red', 'black', 'red', > 'black', 'black', 'black', 'black', 'black', 'black')) > > But this doesn't work (at least not on my full dataset, which as 118 > tips - reduced to 12 here for brevity) and I'm SURE there must be a > better way of doing it. > > Could anyone help me with this? > Many thanks, > Graham > > -- > Dr. Graham Etherington > Post-doctoral Bioinformatician, > Department of Computational and Systems Biology > John Innes Centre > Norwich Research Park > Colney > Norwich > NR4 7UH > UK > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >In general the r-sig-phylo group is better for this kind of question, and it would be better to give us a reproducible example, but here's an example that (I think) does what you want:> library(ape) > set.seed(1001) > z = rcoal(10) > zPhylogenetic tree with 10 tips and 9 internal nodes. Tip labels: t1, t7, t6, t10, t3, t9, ... Rooted; includes branch lengths.> names(z)[1] "edge" "edge.length" "tip.label" "Nnode"> ss <- z$tip.label[c(1,3,5,7)] > ?plot.phylo > plot(z,tip.color=ifelse(z$tip.label %in% ss, "red","black"))-- View this message in context: http://www.nabble.com/How-to-colour-the-tip-labels-in-a-phylogenetic-tree-tp25490805p25491995.html Sent from the R help mailing list archive at Nabble.com.
Maybe Matching Threads
- splitting scientific names into genus, species, and subspecies
- Multiple Range Means Test
- Mixed effects model with a phylogenetic tree/ distance, matrix as a random effect
- distance between phylogenetic trees (quartet, NNI)
- question about importing phylogenetic tree