title: if else condition in ggplot to add an extra layer
body: <p>say I want to plot two layers in ggplot, one containing points and another one containing lines if a certain criteria is fulfilled.</p>
  
  <p>The code without the criteria could look like this:</p>
  
  <pre><code>library("ggplot2")
  
  # Summarise number of movie ratings by year of movie
  mry &lt;- do.call(rbind, by(movies, round(movies$rating), function(df) {
    nums &lt;- tapply(df$length, df$year, length)
    data.frame(rating=round(df$rating[1]), year = as.numeric(names(nums)), number=as.vector(nums))
  }))
  
  p &lt;- ggplot(mry, aes(x=year, y=number, group=rating))
  
  p + 
  geom_point()+
  geom_line()
  </code></pre>
  
  <p>now the condition for plotting the points and not only the lines would be, that an object called tmp.data does not equal the expression "no value".</p>
  
  <pre><code>tmp.data&lt;-c(1,2,3) # in this case the condition is fulfilled
  
  # attempt to plot the two layers including the condition in the plotting function
  p+ 
    if(tmp.data[1]!="no value"){ geom_point()+}
    geom_line()
  </code></pre>
  
  <p>fails....</p>
  
  <pre><code>Error: unexpected '}' in:
  "p+ 
  if(tmp.data[1]!="no value"){ geom_point()+}"
  </code></pre>
  
  <blockquote>
    <p>geom_line()
    geom_line:<br>
    stat_identity:<br>
    position_identity: (width = NULL, height = NULL)</p>
  </blockquote>