title: Remove geom(s) from an existing ggplot chart?
body: <p>I am trying to understand how I can make changes to the internals of a ggplot2 chart. I started reading the few ressources I could find about <code>ggplot_built</code> and <code>ggplot_gtable</code>, but I could not answer the question below.</p>
  
  <p>Given a plot <code>g</code> with 2 <code>geom</code>.</p>
  
  <pre><code>g &lt;- ggplot(iris, aes(Petal.Length, Petal.Width)) + 
    geom_point() + 
    geom_text(aes(label=Sepal.Width))
  g
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Ps5kL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Ps5kL.png" alt="enter image description here"></a></p>
  
  <p>Is there a way to dive into the <code>g</code> object and remove one/multiple geoms?</p>
  
  <p>Can I get, starting from <code>g</code>, a plot with no geoms?</p>
  
  <p><a href="https://i.stack.imgur.com/QWCAt.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QWCAt.png" alt="enter image description here"></a></p>
  
  <p>Or with just the <code>geom_text</code> removed?</p>
  
  <p><a href="https://i.stack.imgur.com/wEIgp.png" rel="noreferrer"><img src="https://i.stack.imgur.com/wEIgp.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <h2><strong>UPDATE after more thorough testing of the answers</strong></h2>
  
  <p>I just noticed that removing geoms can have an impact on the layout of the other geoms. Probably great as a default and the intended behaviour for most use cases, but I actually need the exact same chart "layout" (axis and positions of remaining geoms).</p>
  
  <p>Example, before removing one geom:</p>
  
  <pre><code>library(dplyr)
  library(ggplot2)
  count(mpg, class) %&gt;%
    mutate(pct=n/sum(n)) %&gt;%
    ggplot(aes(class, pct)) +
    geom_col(fill="blue") +
    geom_line(group=1) +
    geom_point(size=4) 
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/lWRRi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/lWRRi.png" alt="enter image description here"></a></p>
  
  <p>after removing one geom (note that the y axis doesn't start at 0 anymore, I guess the default behaviour for line/point without bars):</p>
  
  <pre><code>library(dplyr)
  library(ggplot2)
  count(mpg, class) %&gt;%
    mutate(pct=n/sum(n)) %&gt;%
    ggplot(aes(class, pct)) +
    geom_col(fill="blue") +
    geom_line(group=1) +
    geom_point(size=4) -&gt; p
  p$layers[[1]] &lt;- NULL
  p
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/5yUoT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5yUoT.png" alt="enter image description here"></a></p>
  
  <p>Any ways to force ggplot to keep the exact same layout?</p>