title: Constraining slope in stat_smooth with ggplot (plotting ANCOVA)
body: <p>Using <code>ggplot()</code>, I am trying to plot the results of an ANCOVA in which slopes of the two linear components are equal: i.e., <code>lm(y ~ x + A)</code>. The default behavior for <code>geom_smooth(method = "lm")</code> is to plot separate slopes and intercepts for each level of each factor. For example, with two levels of <code>A</code></p>
  
  <pre><code>library(ggplot2)
  set.seed(1234)
  
  n &lt;- 20
  
  x1 &lt;- rnorm(n); x2 &lt;- rnorm(n)
  y1 &lt;- 2 * x1 + rnorm(n)
  y2 &lt;- 3 * x2 + (2 + rnorm(n))
  A &lt;- as.factor(rep(c(1, 2), each = n))
  df &lt;- data.frame(x = c(x1, x2), y = c(y1, y2), A = A)
  
  p &lt;- ggplot(df, aes(x = x, y = y, color = A))
  p + geom_point() + geom_smooth(method = "lm")
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/6ul8y.png" alt="Default ggplot()"></p>
  
  <p>I can fit the ANCOVA separately with <code>lm()</code> and then use <code>geom_abline()</code> to manually add the lines. This approach has a couple of drawbacks like having the lines extend beyond the range of the data and manually specify the colors.</p>
  
  <pre><code>fm &lt;- lm(y ~ x + A, data = df)
  summary(fm)
  
  a1 &lt;- coef(fm)[1]
  b &lt;- coef(fm)[2]
  a2 &lt;- a1 + coef(fm)[3]
  
  p + geom_point() + 
    geom_abline(intercept = a1, slope = b) + 
    geom_abline(intercept = a2, slope = b)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/OUhjy.png" alt="With geom_abline()"></p>
  
  <p>I know <code>ancova()</code> in the HH package automates the plotting, but I don't really care for lattice graphics. So I am looking for a <code>ggplot()</code>-centric solution.</p>
  
  <pre><code>library(HH)
  ancova(y ~ x + A, data = df)
  </code></pre>
  
  <p>Is there a method to accomplish this using <code>ggplot()</code>? For this example, <code>A</code> has two levels, but I have situations with 3, 4, or more levels. The <code>formula</code> argument to <code>geom_smooth()</code> doesn't seem to have the answer (as far as I can tell).</p>