<p>This is a question from <a href="https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/uPgksYWkwCA/oMtHKW7KEgAJ">ggtree google group</a>:</p>
<blockquote>
<p>Dear ggtree team,</p>
<p>how can I apply a geom_xxx to only one facet panel? For example if i want to get <code>geom_hline(yintersect=1:30)</code> or a <code>geom_text()</code> in the dot panel?
I cant see the <code>facet_grid(. ~ var)</code> function call, so I don’t know which subsetting to use. I have already read <a href="http://stackoverflow.com/questions/29873155/geom-text-and-facets-not-working">http://stackoverflow.com/questions/29873155/geom-text-and-facets-not-working</a></p>
<pre><code>  tr &lt;- rtree(30)
  
  d1 &lt;- data.frame(id=tr$tip.label, val=rnorm(30, sd=3))
  p &lt;- ggtree(tr)
  
  p2 &lt;- facet_plot(p, panel=&quot;dot&quot;, data=d1, geom=geom_point, aes(x=val), color='firebrick')
  d2 &lt;- data.frame(id=tr$tip.label, value = abs(rnorm(30, mean=100, sd=50)))
  
  p3 &lt;- facet_plot(p2, panel='bar', data=d2, geom=geom_segment, aes(x=0, xend=value, y=y, yend=y), size=3, color='steelblue') + theme_tree2()
</code></pre>
<p>Thanks!
Andreas</p>
</blockquote>
<p>If this can be done, we can create even more <a href="https://guangchuangyu.github.io/2016/12/ggtree-version-of-plottree/">comprehensive tree plots</a>.</p>
<p>In fact, it’s quite easy to do. In <code>facet_plot</code>, <code>ggtree</code> used a variable <code>.panel</code> to store the panel information. We can add a dummy column <code>.panel</code> to our data and pass it to <code>geom</code> layer function and the layer will be added to selected panel we specify in the <code>.panel</code> column.</p>
<p>To follow up the above example, we can create a <code>data.frame</code> that contains <code>y</code> and <code>.panel=&quot;dot&quot;</code> as we want to add horizontal lines in <code>dot</code> panel.</p>
<pre><code>d=data.frame(y=1:30, .panel='dot')
p3 + geom_hline(data=d, aes(yintercept=y))
</code></pre>
<p>Here is what we can get:</p>
<p></p>
<p>The <a href="https://guangchuangyu.github.io/2016/10/xlim_tree-set-x-axis-limits-for-only-tree-panel/">xlim_expand</a> is actually adding a <code>geom_blank</code> layer to selected panel for setting x-axis limits of that panel.</p>
<h2>Citation</h2>
<p><strong>G Yu</strong>, DK Smith, H Zhu, Y Guan, TTY Lam*. <a 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>Methods in Ecology and Evolution</strong>. <a href="http://dx.doi.org/10.1111/2041-210X.12628"><code>doi:10.1111/2041-210X.12628</code></a>.</p>
<p></p><div></div>