Sunday, July 24, 2016

Adding features to a phylogeny with a bar plot

I have recently posted (1, 2) on showing a bar plot next to a plotted tree. Enrico Rezende showed a figure in which the bars were colored black or white depending on whether the trait value fell below or above the trait median, respectively, and in which the median value is also overlain using a arrowhead on the abscissa combined with a vertical dashed line.

I just wanted to show (and record for posterity) how this can be duplicated, more or less, in R.

Here, I'm once again working with the eel body size data from my prior posts:

library(phytools)
packageVersion("phytools")
## [1] '0.5.41'
eel.tree
## 
## Phylogenetic tree with 61 tips and 60 internal nodes.
## 
## Tip labels:
##  Moringua_edwardsi, Kaupichthys_nuchalis, Gorgasia_taiwanensis, Heteroconger_hassi, Venefica_proboscidea, Anguilla_rostrata, ...
## 
## Rooted; includes branch lengths.
bsize
##                 Albula_vulpes             Anguilla_anguilla 
##                         104.0                          50.0 
##              Anguilla_bicolor             Anguilla_japonica 
##                         120.0                         150.0 
##             Anguilla_rostrata                Ariosoma_anago 
##                         152.0                          60.0 
##           Ariosoma_balearicum           Ariosoma_shiroanago 
##                          35.0                          40.0 
##        Bathyuroconger_vicinus   Brachysomophis_crocodilinus 
##                          88.0                         120.0 
##              Conger_japonicus              Conger_myriaster 
##                         140.0                         100.0 
##               Conger_verreaxi                Conger_wilsoni 
##                         200.0                         150.0 
##        Congresox_talabonoides            Cynoponticus_ferox 
##                         250.0                         200.0 
##            Dysomma_anguillare                  Elops_saurus 
##                          52.0                         100.0 
##         Facciolella_gilbertii          Gavialiceps_taeniola 
##                          61.0                          64.7 
##         Gnathophis_longicauda          Gorgasia_taiwanensis 
##                          38.6                          74.1 
##         Gymnothorax_castaneus   Gymnothorax_flavimarginatus 
##                         150.0                         240.0 
##            Gymnothorax_kidako           Gymnothorax_moringa 
##                          91.5                         200.0 
## Gymnothorax_pseudothyrsoideus       Gymnothorax_reticularis 
##                          80.0                          60.0 
##            Heteroconger_hassi          Ichthyapus_ophioneus 
##                          40.0                          45.0 
##      Kaupichthys_hyoproroides          Kaupichthys_nuchalis 
##                          30.0                          16.0 
##          Megalops_cyprinoides             Moringua_edwardsi 
##                         150.0                          15.0 
##             Moringua_javanica              Muraenesox_bagio 
##                         120.0                         200.0 
##           Muraenesox_cinereus          Myrichthys_breviceps 
##                         220.0                         102.0 
##          Myrichthys_maculosus         Myrichthys_magnificus 
##                         100.0                          78.0 
##                Myrophis_vafer        Nemichthys_scolopaceus 
##                          46.0                         130.0 
##          Nettastoma_melanurum        Ophichthus_serpentinus 
##                          77.3                          68.0 
##          Ophichthus_zophochir        Oxyconger_leptognathus 
##                          88.0                          60.0 
## Parabathymyrus_macrophthalmus           Paraconger_notialis 
##                          47.0                          62.7 
##      Pisodonophis_cancrivorus          Poeciloconger_kapala 
##                         108.0                          60.0 
##         Rhinomuraena_quaesita          Rhynchoconger_flavus 
##                         130.0                         150.0 
##        Saurenchelys_fierasfer      Scolecenchelys_breviceps 
##                          50.0                          60.0 
##            Scuticaria_tigrina             Serrivomer_beanii 
##                         120.0                          78.0 
##             Serrivomer_sector        Simenchelys_parasitica 
##                          76.0                          61.0 
##            Uroconger_lepturus      Uropterygius_micropterus 
##                          52.0                          30.0 
##          Venefica_proboscidea 
##                         100.0
col<-c("black","white")[
    as.numeric(bsize[reorder(eel.tree)$tip.label]>median(bsize))+1]
obj<-plotTree.barplot(eel.tree,bsize,list(fsize=0.6),
    list(col=col,log="x",
    xlim=c(10,max(bsize)),xlab="length (cm)"))
lines(rep(median(bsize),2),par()$usr[3:4],lty="dashed",col="grey")
rw<-diff(range(bsize))
polygon(x=c(median(bsize),median(bsize)^0.98,median(bsize)^1.02),
    y=c(par()$usr[3],-0.5,-0.5),col="grey")

plot of chunk unnamed-chunk-1

The trickiest part of this, really, was figuring out how to plot the polygon on the abscissa in a way that it appeared symmetric on a log-scale!

In general, any features we would normally add to a barplot we can do it to in R. Slightly more complicated is the task of adding features to the tree. Let me think about that one.

2 comments:

  1. Hi Liam,

    That's a great tool for visualization! I wondered if it is possible to plot multiple bars for each tip (or ideally stacked bars). E.g. each tip has a bar with a positive as well as a negative value.

    Thanks a lot in advance!

    Kind regards,
    Madeleine

    ReplyDelete
    Replies
    1. Hi Madeleine.

      I just posted about this now here: http://blog.phytools.org/2017/01/plottreebarplot-with-more-user-options.html. Let me know if this is more or less what you had in mind.

      All the best, Liam

      Delete