<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/ggtree-for-microbiome-data/"></div></div><div><a data-url="https://www.r-bloggers.com/ggtree-for-microbiome-data/" data-counturl="https://www.r-bloggers.com/ggtree-for-microbiome-data/" data-count="horizontal" data-text="ggtree for microbiome data" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://guangchuangyu.github.io/2016/09/ggtree-for-microbiome-data"> R on Guangchuang YU</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://onlinelibrary.wiley.com/doi/10.1111/2041-210X.12628/abstract">ggtree</a> can parse many software outputs and the evolution evidences inferred by these software can be used directly for tree annotation. <a target="_blank" href="http://onlinelibrary.wiley.com/doi/10.1111/2041-210X.12628/abstract">ggtree</a> not only works as an infrastructure that enables evolutionary data that inferred by commonly used software packages to be used in R, but also serves as a general tree visualization and annotation tool for the R community as it supports many S3/S4 objects defined by other R packages.</p>
<h2>phyloseq for microbiome data</h2>
<p><code>phyloseq</code> class defined in the <a target="_blank" href="https://www.bioconductor.org/package/phyloseq">phyloseq</a> package was designed for microbiome data. <code>phyloseq</code> package implemented <code>plot_tree</code> function using <code>ggplot2</code>. Although the function was implemented by <code>ggplot2</code> and we can use <code>theme</code>, <code>scale_color_manual</code> etc for customization, the most valuable part of <code>ggplot2</code>, adding layer, is missing. <code>plot_tree</code> only provides limited parameters to control the output graph and it is hard to adding layer unless user has expertise in both <code>phyloseq</code> and <code>ggplot2</code>.</p>
<pre><code>library(phyloseq)

data(GlobalPatterns)
GP &lt;- prune_taxa(taxa_sums(GlobalPatterns) > 0, GlobalPatterns)
GP.chl &lt;- subset_taxa(GP, Phylum==&quot;Chlamydiae&quot;)

plot_tree(GP.chl, color=&quot;SampleType&quot;, shape=&quot;Family&quot;, label.tips=&quot;Genus&quot;, size=&quot;Abundance&quot;) + ggtitle(&quot;tree annotation using phyloseq&quot;)
</code></pre>
<p><img alt="" src="https://i0.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/tree_phyloseq.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>PS: If we look at the plot careful, we will find that legend produce by <code>plot_tree</code> is not correct (<code>plot_tree</code> map <code>SampleType</code> to color text which was shown in legend, but we can’t find the mapping in the plot).</p>
<h2>ggtree supports phyloseq object</h2>
<p>One of the advantage of R is the community. R users develop packages that can work together and complete each other. <a target="_blank" href="http://onlinelibrary.wiley.com/doi/10.1111/2041-210X.12628/abstract">ggtree</a> fits the R ecosystem in phylogenetic analysis. It supports several classes defined in other R packages that designed for storing phylogenetic tree with associated data, including <code>phyloseq</code>.</p>
<pre><code>library(scales)
library(ggtree)
p &lt;- ggtree(GP.chl, ladderize = FALSE) + geom_text2(aes(subset=!isTip, label=label), hjust=-.2, size=4) +
    geom_tiplab(aes(label=Genus), hjust=-.3) +
    geom_point(aes(x=x+hjust, color=SampleType, shape=Family, size=Abundance),na.rm=TRUE) +
    scale_size_continuous(trans=log_trans(5)) +
    theme(legend.position=&quot;right&quot;) + ggtitle(&quot;reproduce phyloseq by ggtree&quot;)
print(p)
</code></pre>
<p><img alt="" src="https://i2.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/tree_ggtree.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>With <a target="_blank" href="https://guangchuangyu.github.io/ggtree">ggtree</a>, it would be more flexible to combine different layers using grammar of graphics syntax and more powerful since layers can be added without limitation (i.e. those predefined in <code>plot_tree</code> function). As an example, I extract the barcode sequence from the tree object and use <code>msaplot</code> to visualize the barcode sequence with the tree.</p>
<pre><code>df &lt;- fortify(GP.chl)
barcode &lt;- as.character(df$Barcode_full_length)
names(barcode) &lt;- df$label
barcode &lt;- barcode[!is.na(barcode)]
msaplot(p, Biostrings::BStringSet(barcode), width=.3, offset=.05)
</code></pre>
<p><img alt="" src="https://i1.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/tree_ggtree_msaplot.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>PS: I am thinking about writing a tutorial through examples. If you have any interesting topic, please let me know.</p>
<h2>Citation</h2>
<p><strong>G Yu</strong>, DK Smith, H Zhu, Y Guan, TTY Lam<sup>*</sup>. <a target="_blank" href="http://onlinelibrary.wiley.com/doi/10.1111/2041-210X.12628/abstract">ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data</a>. <strong><em>Methods in Ecology and Evolution</em></strong>. <a target="_blank" href="http://dx.doi.org/10.1111/2041-210X.12628"><code>doi:10.1111/2041-210X.12628</code></a>.</p>
<p></p>
<div data-badge-popover="right" data-doi="10.1111/2041-210X.12628" data-badge-type="donut"></div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/ggtree-for-microbiome-data/"></div></div><div><a data-url="https://www.r-bloggers.com/ggtree-for-microbiome-data/" data-counturl="https://www.r-bloggers.com/ggtree-for-microbiome-data/" data-count="vertical" data-text="ggtree for microbiome data" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://guangchuangyu.github.io/2016/09/ggtree-for-microbiome-data"> R on Guangchuang YU</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/zrQqYQpqX4c"></a></div>