title: ggplot2 geom_smooth, extended model for method=lm
body: <p>I would like to use <code>geom_smooth</code> to get a fitted line from a certain linear regression model. </p>
  
  <p>It seems to me that the formula can only take <code>x</code> and <code>y</code> and not any additional parameter.</p>
  
  <p>To show more clearly what I want:</p>
  
  <pre><code>library(dplyr)
  library(ggplot2)
  set.seed(35413)
  df &lt;- data.frame(pred = runif(100,10,100),
             factor = sample(c("A","B"), 100, replace = TRUE)) %&gt;%
    mutate(
      outcome = 100 + 10*pred + 
      ifelse(factor=="B", 200, 0) + 
      ifelse(factor=="B", 4, 0)*pred +
      rnorm(100,0,60))
  </code></pre>
  
  <p>With</p>
  
  <pre><code>ggplot(df, aes(x=pred, y=outcome, color=factor)) +
    geom_point(aes(color=factor)) +
    geom_smooth(method = "lm") +
    theme_bw()
  </code></pre>
  
  <p>I produce fitted lines that, due to the <code>color=factor</code> option, are basically the output of the linear model <code>lm(outcome ~ pred*factor, df)</code></p>
  
  <p><a href="https://i.stack.imgur.com/ET6CI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ET6CI.png" alt="enter image description here"></a></p>
  
  <p>In some cases, however, I prefer the lines to be the output of a different model fit, like <code>lm(outcome ~ pred + factor, df)</code>, for which I can use something like:</p>
  
  <pre><code>fit &lt;- lm(outcome ~ pred+factor, df)
  predval &lt;- expand.grid(
    pred = seq(
      min(df$pred), max(df$pred), length.out = 1000),
    factor = unique(df$factor)) %&gt;%
    mutate(outcome = predict(fit, newdata = .))
  
  ggplot(df, aes(x=pred, y=outcome, color=factor)) +
    geom_point() +
    geom_line(data = predval) +
    theme_bw()
  </code></pre>
  
  <p>which results in :</p>
  
  <p><a href="https://i.stack.imgur.com/tKMYx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/tKMYx.png" alt="enter image description here"></a></p>
  
  <p>My question: is there a way to produce the latter graph exploiting the <code>geom_smooth</code> instead? I know there is a <code>formula =</code> - option in <code>geom_smooth</code> but I can't make something like <code>formula = y ~ x + factor</code> or <code>formula = y ~ x + color</code> (as I defined <code>color = factor</code>) work.</p>