title: Adding line with points to a plot in ggplot2
body: <p>For my convenience, I want to write a function that adds a line plot and a scatter plot to an already existing plot. I defined:</p>
  
  <pre><code>addlinetoplot &lt;- function(dataset, varx, vary)
       { 
         p &lt;- geom_line(data=dataset, aes_string(x=varx, y=vary)) + 
              geom_point(data=dataset, aes_string(x=varx, y=vary))
  
         p
       }
  </code></pre>
  
  <p>The above function does not work. It works if I remove the <code>+</code> sign and the <code>geom_point()</code> part. The above does not work because for some reason one cannot add those two geoms. The following example illustrates the problem. </p>
  
  <p>Then, I tried:</p>
  
  <pre><code>df1 &lt;- data.frame(c1 = c(1:10), c2 = c(1:10))
  c1 &lt;- c(1:10)
  csq &lt;- c1^2
  df2 &lt;- data.frame(c1 = c(1:10), csq)
  pltbase &lt;- ggplot() + geom_line(df1, aes(x="c1", y="c2")) 
  
  # This does not work.
  pltbase + addlinetoplot(dataset=df2, varx = "c1", vary = "csq")
  </code></pre>
  
  <p>I figured the problem is with the statement:</p>
  
  <pre><code>addthistotheplot &lt;- geom_line(data=df2, aes_string(x="c1", y="csq")) + 
                      geom_point(data=df2, aes_string(x="c1", y="csq"))
  </code></pre>
  
  <p>Trying to define the above statement throws an error: non-numeric argument to binary operator. </p>
  
  <p>How can I define the <code>addlinetoplot()</code> function so that I can make:</p>
  
  <pre><code>pltbase + addlinetoplot(dataset=df2, varx = "c1", vary = "csq")
  </code></pre>
  
  <p>work. One way is to separate out the geom_line and geom_point and have different function definition for each. Is there a way to do it in the same function?</p>
  
  <p>Thanks!</p>