title: If condition inside ggplot
body: <p>Why <code>if</code> statement does not work inside <code>ggplot</code> function but works as a last line:</p>
  
  <pre><code>index &lt;- 1
  </code></pre>
  
  <p>This works:</p>
  
  <pre><code>ggplot(mtcars, aes(wt, mpg))+
    geom_line()+
    if(index==1) geom_point()
  </code></pre>
  
  <p>However, when I move the <code>if</code> statement, it does not work:</p>
  
  <pre><code>ggplot(mtcars, aes(wt, mpg))+
    if(index==1) geom_point()+  #or if(index==1) {geom_point()}+
    geom_line()
  </code></pre>
  
  <p>Is there anyway to use <code>if</code> statement inside <code>ggplot</code>, not in the last line? I know I can do the following, but I am curious if I can use it inside <code>ggplot</code>:</p>
  
  <pre><code>p &lt;-  ggplot(mtcars, aes(wt, mpg))+
        geom_line()
  
  if(index==1) p &lt;- p + geom_point()
  </code></pre>