<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/xlim_tree-set-x-axis-limits-for-only-tree-panel/"></div></div><div><a data-url="https://www.r-bloggers.com/xlim_tree-set-x-axis-limits-for-only-tree-panel/" data-counturl="https://www.r-bloggers.com/xlim_tree-set-x-axis-limits-for-only-tree-panel/" data-count="horizontal" data-text="xlim_tree: set x axis limits for only Tree panel" 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/10/xlim_tree-set-x-axis-limits-for-only-tree-panel"> 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 <a target="_blank" rel="nofollow" href="https://guangchuangyu.github.io/ggtree">ggtree</a> user recently asked me the following question in <a target="_blank" rel="nofollow" href="https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/6ec5krxpBMc/sd6jr53KBQAJ">google group</a>:</p>
<blockquote>
<p>I try to plot long tip labels in ggtree and usually adjust them using xlim(), however when creating a facet_plot xlim affects all plots and minimizes them.</p>
<p>Is it possible to work around this and only affect the tree and it’s tip labels leaving the other plots in facet_plot unaffected?</p>
</blockquote>
<p>This is indeed a desire feature, as <code>ggplot2</code> can’t automatically adjust <code>xlim</code> for text since the units are in two different spaces (data and pixel).</p>
<p>Here is an example, the tip labels are truncated.</p>
<pre><code>set.seed(2016-10-31)
tr =rtree(50)
tr$tip.label = paste(tr$tip.label, tr$tip.label, sep=&quot;_&quot;)
p &lt;- ggtree(tr) + geom_tiplab(align=TRUE) + theme_tree2()
d = data.frame(id = tr$tip.label, v= rnorm(50))

facet_plot(p, geom=geom_point, data=d, mapping=aes(x=v), panel='dot') + 
        ggtitle(&quot;truncated tip labels&quot;)
</code></pre>
<p><img alt="" src="https://i0.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/Screen%20Shot%202016-10-31%20at%2011.31.39%20AM.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>If we only visualize the tree, it is easy to solve by using <code>xlim()</code> to allocate more space for the lables. But <code>xlim()</code> works for all panels. Combination of <code>facet_plot()</code> and <code>xlim()</code> will produce figure with many spaces.</p>
<pre><code>facet_plot(p+xlim(NA, 6), geom=geom_point, data=d, mapping=aes(x=v), panel='dot') + 
        ggtitle(&quot;xlim applies to all panels&quot;)
</code></pre>
<p><img alt="" src="https://i1.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/Screen%20Shot%202016-10-31%20at%2011.33.05%20AM.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>To overcome this issue, <code>ggtree</code> provides <code>xlim_tree</code> to set <code>x</code> axis limits for only the <code>Tree</code> panel.</p>
<pre><code>facet_plot(p+xlim_tree(6), geom=geom_point, data=d, mapping=aes(x=v), panel='dot') + 
        ggtitle('*xlim_tree* only change x axis limits of *Tree* panel')

# or using:
# facet_plot(p, geom=geom_point, data=d, mapping=aes(x=v), panel='dot') + xlim_tree(6)
</code></pre>
<p><img alt="" src="https://i1.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/Screen%20Shot%202016-10-31%20at%2011.34.56%20AM.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Citation</h2>
<p><strong>G Yu</strong>, DK Smith, H Zhu, Y Guan, TTY Lam<sup>*</sup>. <a target="_blank" rel="nofollow" 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" rel="nofollow" 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/xlim_tree-set-x-axis-limits-for-only-tree-panel/"></div></div><div><a data-url="https://www.r-bloggers.com/xlim_tree-set-x-axis-limits-for-only-tree-panel/" data-counturl="https://www.r-bloggers.com/xlim_tree-set-x-axis-limits-for-only-tree-panel/" data-count="vertical" data-text="xlim_tree: set x axis limits for only Tree panel" 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/10/xlim_tree-set-x-axis-limits-for-only-tree-panel"> 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/j7hQzDUMsfE"></a></div>