spholmes / F1000_workflow

43 stars 33 forks source link

Scale bar for tree using phangorn and phyloseq #33

Open AMFBio opened 5 years ago

AMFBio commented 5 years ago

Hi there,

I'm trying to get a scale bar for my phlyogenetic tree.

I’m using the instructions found in this tutorial: https://f1000research.com/articles/5-1492/v2

After I process my sequences and run the alignment, my script is as follows:

phang.align <- phyDat(as(alignment, "matrix"), type="DNA") dm <- dist.ml(phang.align) treeNJ <- NJ(dm) fit = pml(treeNJ, data=phang.align) negative edges length changed to 0! fitGTR <- update(fit, k=4, inv=0.2) fitGTR <- optim.pml(fitGTR, model="GTR", optInv=TRUE, optGamma=TRUE, rearrangement = "stochastic", control = pml.control (trace = 0))

I combine this into my phyloseq object, then I filter and prune my data set to give me the new phyloseq object: test15_2

I plot this tree using:

plot_tree(test15_2, color="Species", ladderize="left", label.tips=labeltips)

But I’m unsure of how to add the scale bar in here?

Any help would be appreciated.

Cheers,

papelypluma commented 3 years ago

Hi @AMFBio . I'm wondering if by any chance did you find a way to add a scale bar using phyloseq?

papelypluma commented 3 years ago

Better late than never. Sharing what I found and eventually used to generate a scale bar. The library ggtree does the job, although importing the library may generate unusual warnings along the way (I surmise they can be safely ignored). In brief, a scale bar can be generated by appending geom_treescale(x=int, y=int, fontsize=int, linesize=int, offset=int, color='str') to the plot_tree()/ggplot object. The arguments specified in x, y and offset will tell how the scale bar is positioned relative to the tree.