<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/annotating-phylogenetic-tree-with-images-using-ggtree-and-ggimage/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://guangchuangyu.github.io/2018/03/annotating-phylogenetic-tree-with-images-using-ggtree-and-ggimage/"> R on Guangchuang YU</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>With <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggimage/index.html" target="_blank">ggimage</a>, we are able to plot images using grammar of graphics. The layers defined in <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggimage/index.html" target="_blank">ggimage</a> can be directly applied to <a rel="nofollow" href="https://guangchuangyu.github.io/ggtree" target="_blank">ggtree</a> to annotate phylogenetic tree using local/online image files.</p>
<p><a rel="nofollow" href="https://guangchuangyu.github.io/ggtree" target="_blank">ggtree</a> seamlessly work with <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggimage/index.html" target="_blank">ggimage</a>. The <code>geom_tiplab</code> and <code>geom_nodelab</code> can accept parameter of <code>geom=&quot;image&quot;</code> to parse taxa labels as image files and use them to “label” the taxa using images instead of text strings. Here are some examples for demonstration.</p>
<p><span></span></p>
<div>
<h2>Annotate tips</h2>
<pre><code>library(ggimage)
library(ggtree)
url &lt;- paste0(&quot;https://raw.githubusercontent.com/TreeViz/&quot;,
            &quot;metastyle/master/design/viz_targets_exercise/&quot;)
x &lt;- read.tree(paste0(url, &quot;tree_boots.nwk&quot;))
info &lt;- read.csv(paste0(url, &quot;tip_data.csv&quot;))
p &lt;- ggtree(x) %&lt;+% info + xlim(NA, 6)
p + geom_tiplab(aes(image= imageURL), geom=&quot;image&quot;, offset=2, align=T, size=.16, hjust=0) +
    geom_tiplab(geom=&quot;label&quot;, offset=1, hjust=.5)</code></pre>
<p><img src="https://i2.wp.com/guangchuangyu.github.io/post/R/ggtree/ggtree-ggimage_files/figure-html/unnamed-chunk-2-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>Annotate internal nodes</h2>
<pre><code>## example from https://rgriff23.github.io/2017/05/11/primate-phylogeny-ggtree.html
library(ape)
tree &lt;- read.nexus(&quot;https://raw.githubusercontent.com/rgriff23/Dissertation/master/Chapter_2/data/tree.nex&quot;)
phylopic_info &lt;- data.frame(node = c(124, 113, 110, 96, 89, 70),
                            phylopic = c(&quot;7fb9bea8-e758-4986-afb2-95a2c3bf983d&quot;,
                                        &quot;bac25f49-97a4-4aec-beb6-f542158ebd23&quot;,
                                        &quot;f598fb39-facf-43ea-a576-1861304b2fe4&quot;,
                                        &quot;aceb287d-84cf-46f1-868c-4797c4ac54a8&quot;,
                                        &quot;0174801d-15a6-4668-bfe0-4c421fbe51e8&quot;,
                                        &quot;72f2f854-f3cd-4666-887c-35d5c256ab0f&quot;),
                            species = c(&quot;galagoids&quot;, &quot;lemurs&quot;, &quot;tarsiers&quot;,
                                        &quot;cebids&quot;, &quot;hominoids&quot;, &quot;cercopithecoids&quot;))
pg &lt;- ggtree(tree)
pg %&lt;+% phylopic_info + geom_nodelab(aes(image=phylopic), geom=&quot;phylopic&quot;, alpha=.5, color='steelblue')</code></pre>
<p><img src="https://i0.wp.com/guangchuangyu.github.io/post/R/ggtree/ggtree-ggimage_files/figure-html/unnamed-chunk-3-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>d &lt;- data.frame(node = c(&quot;70&quot;,&quot;89&quot;,&quot;96&quot;,&quot;110&quot;,&quot;113&quot;,&quot;124&quot;),
                images = c(&quot;https://i.imgur.com/8VA9cYw.png&quot;,
                            &quot;https://i.imgur.com/XYM1T2x.png&quot;,
                            &quot;https://i.imgur.com/EQs5ZZe.png&quot;,
                            &quot;https://i.imgur.com/2xin0UK.png&quot;,
                            &quot;https://i.imgur.com/hbftayl.png&quot;,
                            &quot;https://i.imgur.com/3wDHW8n.png&quot;))
pg %&lt;+% d + geom_nodelab(aes(image=images), geom=&quot;image&quot;)</code></pre>
<p><img src="https://i0.wp.com/guangchuangyu.github.io/post/R/ggtree/ggtree-ggimage_files/figure-html/unnamed-chunk-3-2.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>Use image as background</h2>
<pre><code>pg + geom_phylopic(image=&quot;79ad5f09-cf21-4c89-8e7d-0c82a00ce728&quot;, size=Inf, alpha=.5, color='firebrick')</code></pre>
<p><img src="https://i2.wp.com/guangchuangyu.github.io/post/R/ggtree/ggtree-ggimage_files/figure-html/unnamed-chunk-4-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<hr>
<p>Here is another example that uses <code>Tabikaeru</code> (known as traveling frog, a popular mobile game) image to decorate a phylogenetic tree of frog, see reproducible source code on <a rel="nofollow" href="https://github.com/GuangchuangYu/frog_tree" target="_blank">https://github.com/GuangchuangYu/frog_tree</a>.</p>
<div>
<img src="https://i2.wp.com/raw.githubusercontent.com/GuangchuangYu/frog_tree/master/ggtree_frog.png?w=456&ssl=1" data-recalc-dims="1">
</div>
</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/03/annotating-phylogenetic-tree-with-images-using-ggtree-and-ggimage/"> 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/4nVHhckgCvw" height="1"></a></div>