2

I'm trying to produce a non-ultrametric tree using the ape package in R and the function plot.phylo(). I'm struggling to find any documentation on how to keep the tip label vertically aligned on their left edge and with a series of dots (variable length) linking the species' name to the tip of the node.

Any help would be much appreciated as well as links to other packages within R that may be able to achieve this.

An example of the newick tree

I don't have any tree examples of what i want, however, the description seems self explanatory. the labels would all be shifted to the very right, and aligned on their left side, then a series of dots (.......) would link the label to where there old position was.

  MLJTT = newickTree (as a string)
  plot.phylo(read.tree(text = MLJTT), show.tip.label = T,use.edge.length = T, no.margin = T, cex = 0.55)

non-aligned phylogeny

And example of three that I want to copy the layout of from here:enter image description here

Rambatino
  • 4,716
  • 1
  • 33
  • 56
  • Is there anyway you can provide some sample data (A sample tree) and perhaps a link to an image of a plot you are trying to replicate? – MrFlick Aug 13 '14 at 03:00
  • @MrFlick I'll have a look tomorrow to see if i can find an example, but I've edited my question and hopefully my description should be good enough now. – Rambatino Aug 13 '14 at 04:11
  • That's very helpful. Did you generate the plot in R? If so, can you also share the R code used to make it. It may be possible to make an easy change in the code to do what you want. I do think this sample plot with your description makes sense. Basically you want all the text left-aligned on the right side of the plot with dotted lines connecting the name to the corresponding terminal tree node. – MrFlick Aug 13 '14 at 04:24
  • @MrFlick I've add the code to make it, though without colours. and yep, that's what I want! – Rambatino Aug 13 '14 at 14:35
  • @MrFlick Okay, I think it should be really obvious now! ^^ – Rambatino Aug 13 '14 at 15:48
  • This kind of cosmetic fine-tuning is often easier to accomplish in a vector editor like Inkscape, rather than fighting with R. Using the output you have now, you could open the file in Inkscape and manually line up the labels and add dotted lines. I'd still be interested in a native R solution if there is one! – Tyler Aug 13 '14 at 15:53
  • @Tyler I did find a relatively simple fix for this. But you are correct in that if you want to do a lot of fine tuning on plots, it may be best to export as a vector format (pdf) and fix it up in a proper graphics program. – MrFlick Aug 13 '14 at 16:31

2 Answers2

3

Ok, I ended up slightly modifying the default plot.phylo code to accomidate such a change. Here's how it looks

library(ape)
plot.phylo2 <- plot.phylo
environment(plot.phylo2) <- environment(plot.phylo)

body(plot.phylo2)[[c(34, 3, 6, 3, 4, 3)]] <- quote({
    mx <- max(xx[1:Ntip])
    segments(xx[1:Ntip], yy[1:Ntip] + loy, mx, yy[1:Ntip] + loy, 
        lty=2, col="grey")
    text(mx + lox, yy[1:Ntip] + loy, x$tip.label, adj = adj, 
        font = font, srt = srt, cex = cex, col = tip.color)
})

This is somewhat fragile and may change in different version of ape, I've tested this with version ape_3.1-4. You can check if this will work by verifying that

body(plot.phylo)[[c(34, 3, 6, 3, 4, 3)]]

returns

text(xx[1:Ntip] + lox, yy[1:Ntip] + loy, x$tip.label, adj = adj, 
    font = font, srt = srt, cex = cex, col = tip.color)

just to make sure we are changing the correct line. But the code above basically replaces that line where the labels are drawn by moving the x axis where they are drawn and adding in the segments for the dotted lines. Then you can run this with your test data

MLJTT = read.tree(text="..<sample data>..")
plot.phylo2(MLJTT, 
    show.tip.label = T,use.edge.length = T, no.margin = T, cex = 0.55)

And this produces

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • you sir, are a legend: thank-you! - I do however get this error: Error in `[[<-`(`*tmp*`, c(34, 3, 6, 3, 4, 3), value = { : no such index at level 3. I'll have a look into the code a bit more, I should be able to work it out! – Rambatino Aug 13 '14 at 16:21
  • @MarkRamotowski As i mentioned this method is fragile and is very much library version dependent. I forgot to mention I tested on `ape_3.1-4`. You can check which version you are running with `sessionInfo()`. If yours is different, you may want to upgrade, or go though the trouble of figuring out the correct line for your version. – MrFlick Aug 13 '14 at 16:27
  • Ahh I'm on ape_3.1-1, I'll upgrade later this evening. Thanks for your solution and time on this problem! – Rambatino Aug 13 '14 at 16:34
1

I think what you may be looking for is the argument to plot.phylo:

align.tip.label = TRUE

Have you tried this?

MLJTT <- rtree(100)
plot.phylo(MLJTT, show.tip.label = T, align.tip.label = T, use.edge.length = T, no.margin = T, cex = 0.55)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Hi - it's been so long since I've looked at this I actually can't remember - although I do remember the previous solution did work really well for me! :) – Rambatino Feb 10 '17 at 12:48