<p>I have splitted <code>ggtree</code> to 2 packages, <code>treeio</code> and <code>ggtree</code>. Now <code>ggtree</code> is mainly focus on visualization and annotation, while <code>treeio</code> focus on parsing and exporting tree files. Here is a welcome message from <code>treeio</code> that you can convert <code>ggtree</code> output to tree object which can be exported as newick or nexus file if you want.</p>
<p>Thanks to <code>ggplot2</code>, output of <code>ggtree</code> is actually a <code>ggplot</code> object. The <code>ggtree</code> object can be rendered as graph by <code>print</code> method, but internally it is an object that contains data. <code>treeio</code> defines <code>as.phylo</code> and <code>as.treedata</code> to convert <code>ggtree</code> object to <code>phylo</code> or <code>treedata</code> object.</p>
<pre><code>require(ggtree)
nhxfile &lt;- system.file(&quot;extdata/NHX&quot;, &quot;ADH.nhx&quot;, package=&quot;treeio&quot;)
nhx &lt;- read.nhx(nhxfile)
p &lt;- ggtree(nhx)
</code></pre>
<p>After parsing the NHX file via <code>read.nhx</code> function, we can visualize it using <code>ggtree</code>, and the output <code>ggtree</code> object can be converted back as a <code>phylo</code> object using <code>as.phylo</code> method:</p>
<blockquote>
<p>as.phylo(p)</p>
<p>Phylogenetic tree with 8 tips and 4 internal nodes.</p>
<p>Tip labels:
ADH2, ADH1, ADHY, ADHX, ADH4, ADH3, …</p>
<p>Rooted; includes branch lengths.</p>
</blockquote>
<p>The output <code>phylo</code> object contains the tree structure.</p>
<p>If we want to also save associated annotation data, we can use <code>as.treedata</code> method:</p>
<blockquote>
<p>as.treedata(p)</p>
<p>‘treedata’ S4 object that stored information of
“.</p>
<p>…@ tree:
Phylogenetic tree with 8 tips and 4 internal nodes.</p>
<p>Tip labels:</p>
<p>ADH2, ADH1, ADHY, ADHX, ADH4, ADH3, …</p>
<p>Rooted; includes branch lengths.</p>
<p>with the following features available:
’S’, ’D’, ‘B’.</p>
</blockquote>
<p>In <code>ggtree</code>, we can use <code>%&lt;+%</code> operator to attach user’s own data to graphic object and then use it to annotate the tree. <code>as.treedata</code> can also export these attached data to the tree object.</p>
<blockquote>
<p>d = data.frame(label=as.phylo(nhx)$tip.label, <strong>trait</strong>=abs(rnorm(Ntip(nhx))))</p>
<p>p = p %&lt;+% d</p>
<p>as.treedata(p)</p>
<p>‘treedata’ S4 object that stored information of
“.</p>
<p>…@ tree:</p>
<p>Phylogenetic tree with 8 tips and 4 internal nodes.</p>
<p>Tip labels:</p>
<p>ADH2, ADH1, ADHY, ADHX, ADH4, ADH3, …</p>
<p>Rooted; includes branch lengths.</p>
<p>with the following features available:
’S’, ’D’, ‘B’, ‘<strong>trait</strong>’.</p>
</blockquote>
<p>Here as an example, the <strong>trait</strong> variable was attached and exported to the tree object.</p>
<p>This <strong>trait</strong> variable can also be used in tree annotation.</p>
<pre><code>x &lt;- as.treedata(p)

ggtree(x) + geom_tiplab(align=T, offset=.005) + 
    geom_tippoint(aes(size=trait)) + xlim(NA, 0.28) + 
        geom_label(aes(x=branch, label=S)) 
</code></pre>
<p><img alt src="https://guangchuangyu.github.io/blog_images/Bioconductor/treeio/nhx.jpg"></p>
<p>We can visualize the tree by <code>ggtree(tree_object)</code> and we can convert the <code>ggtree</code> object back to tree object via <code>as.treedata(ggtree_object)</code>.</p>
<p>If you lost your tree file but have the ggtree object saved, you can convert it back to tree object and export the tree object to newick or nexus file.</p>