<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/rename-phylogeny-tip-labels-in-treeio/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://guangchuangyu.github.io/2018/04/rename-phylogeny-tip-labels-in-treeio/"> R on Guangchuang YU</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I don’t know whether ‘rename taxa’ is a common task or not. It seems not a good idea to rename taxa in Newick tree text, since it may introduce problems when mapping the original sequence alignment to the tree.</p>
<p>If you just want to show different or additional information when plotting the tree, it is fine and easy to do it using <code>ggtree</code>:</p>
<p><span></span></p>
<pre><code>require(treeio)</code></pre>
<pre><code>## Loading required package: treeio</code></pre>
<pre><code>require(ggtree)</code></pre>
<pre><code>## Loading required package: ggtree</code></pre>
<pre><code>## ggtree v1.11.6  For help: https://guangchuangyu.github.io/software/ggtree
## 
## If you use ggtree in published research, please cite:
## Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36, doi:10.1111/2041-210X.12628</code></pre>
<pre><code>tr &lt;- read.tree(text = &quot;((a,(b,c)),d);&quot;)
genus &lt;- c(&quot;Gorilla&quot;, &quot;Pan&quot;, &quot;Homo&quot;, &quot;Pongo&quot;)
species &lt;- c(&quot;gorilla&quot;, &quot;spp.&quot;, &quot;sapiens&quot;, &quot;pygmaeus&quot;)
geo &lt;- c(&quot;Africa&quot;, &quot;Africa&quot;, &quot;World&quot;, &quot;Asia&quot;)
d &lt;- data.frame(label = tr$tip.label, genus = genus,
                species = species, geo = geo)
d</code></pre>
<pre><code>##   label   genus  species    geo
## 1     a Gorilla  gorilla Africa
## 2     b     Pan     spp. Africa
## 3     c    Homo  sapiens  World
## 4     d   Pongo pygmaeus   Asia</code></pre>
<pre><code>ggtree(tr) %&lt;+% d + xlim(NA, 5) +
    geom_tiplab(aes(label=paste0('italic(', genus, ')~bolditalic(', species, ')~', geo)), parse=T)</code></pre>
<pre><code>## Warning: package 'bindrcpp' was built under R version 3.4.4</code></pre>
<p><img src="https://i0.wp.com/guangchuangyu.github.io/post/R/ggtree/treeio/rename_taxa_files/figure-html/unnamed-chunk-1-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>However, it is also possible to rename taxa of the tree object (either <code>treedata</code> or <code>phylo</code>) in <code>treeio</code>:</p>
<pre><code>tr2 = rename_taxa(tr, d, label, genus)
write.tree(tr2)</code></pre>
<pre><code>## [1] &quot;((Gorilla,(Pan,Homo)),Pongo);&quot;</code></pre>
<pre><code>d2 = dplyr::mutate(d, newlab = paste(genus, species, sep='|'))
d2</code></pre>
<pre><code>##   label   genus  species    geo          newlab
## 1     a Gorilla  gorilla Africa Gorilla|gorilla
## 2     b     Pan     spp. Africa        Pan|spp.
## 3     c    Homo  sapiens  World    Homo|sapiens
## 4     d   Pongo pygmaeus   Asia  Pongo|pygmaeus</code></pre>
<pre><code>tr3 = rename_taxa(tr, d2, label, newlab)
write.tree(tr3)</code></pre>
<pre><code>## [1] &quot;((Gorilla|gorilla,(Pan|spp.,Homo|sapiens)),Pongo|pygmaeus);&quot;</code></pre>
<p>If the input tree object is a <code>treedata</code> instance, you can use <code>write.beast</code> to export the tree with associated data to a BEAST compatible NEXUS file.</p>
<div>
<h2>Vignettes</h2>
<ul>
<li><a rel="nofollow" href="http://bioconductor.org/packages/devel/bioc/vignettes/treeio/inst/doc/Importer.html" target="_blank">Importing trees with data</a></li>
<li><a rel="nofollow" href="http://bioconductor.org/packages/devel/bioc/vignettes/treeio/inst/doc/Exporter.html" target="_blank">Exporting trees with data</a></li>
</ul>
</div>
	<p>
<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/2018/04/rename-phylogeny-tip-labels-in-treeio/"> 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/3YRzh5dZjKo" height="1"></a></div>