<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/edge-coloring-with-user-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://guangchuangyu.github.io/2016/12/edge-coloring-with-user-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>Coloring edges in ggtree is quite easy, as we can map the color to numerical or categorical values via the <code>aes(color=VAR)</code> syntax. For user’s own data, it is also easy as <code>ggtree</code> provide the <code>%&lt;+%</code> operator to attach user data.</p>
<p>But as it seems not so obviously for <code>ggtree</code> users, see question <a target="_blank" rel="nofollow" href="https://groups.google.com/forum/#!topic/bioc-ggtree/4GgivKqVjB8">1</a>, <a target="_blank" rel="nofollow" href="https://github.com/GuangchuangYu/ggtree/issues/76">2</a>, and <a target="_blank" rel="nofollow" href="https://github.com/GuangchuangYu/ggtree/issues/105">3</a>, I will demonstrate how to color edges using user data here.</p>
<p>There are several solutions to this question. Let’s first start with a hard one by using <code>phylo4d</code> class defined by <code>phylobase</code> package:</p>
<pre><code>require(ggtree)
require(phylobase)

set.seed(123)
tr = rtree(30)
g1 = as(tr, 'phylo4')
d = data.frame(color=sample(c('red', 'blue', 'green'), 30, replace=T))
rownames(d) = tr$tip.label
g2 = phylo4d(g1, d)

rNodeData &lt;- data.frame(randomTrait = rnorm(nNodes(g1)),
                        color = sample(c('purple', 'yellow', 'black'), nNodes(g1), replace=T),
                        row.names = nodeId(g1, &quot;internal&quot;))

nodeData(g2) &lt;- rNodeData
</code></pre>
<p>We can store the information in a <code>phylo4d</code> object and as this object is supported by <code>ggtree</code>. We can directly coloring the tree by mapping the color to specific variable.</p>
<pre><code>ggtree(g2, aes(color=I(color)))
</code></pre>
<p><img alt="" src="https://i2.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/color_edge_with_user_data/unnamed-chunk-1-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><code>ggtree</code> itself also provide solution by using the operator, <code>%&lt;+%</code> to attach user data. After attaching, we can re-scale edge color by <code>aes</code>.</p>
<pre><code>d = data.frame(node=1:59, color=sample(c('red', 'blue', 'green'), 59, replace=T))
ggtree(tr) %&lt;+% d + aes(color=I(color))
</code></pre>
<p><img alt="" src="https://i0.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/color_edge_with_user_data/unnamed-chunk-1-2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<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/2016/12/edge-coloring-with-user-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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/avopKXJoZBo"></a></div>