title: Plot a Linear Regression in ggplot2
body: <p>I have this dataframe:</p>
  
  <pre><code>&gt; head(data)
      sx   yd  sl
  1   male 35  36350
  2   male 22  35350
  3   male 23  28200
  4 female 27  26775
  5   male 30  33696
  6   male 21  28516
  </code></pre>
  
  <p>Where "sx" is sex, "yd" is years since acquired degree and "sl" is salary. Using ggplot or plot, I can plot the scatter easily.</p>
  
  <pre><code>palette(c("pink", "blue"))
  plot(data$yr, data$sl, col = factor(data$sx), xlab = "Years Since Earned Highest Degree", ylab = "Salary (dollars)", main = "Salary Increases with Experience", pch = 19)
  legend("topleft", legend = unique(data$sx), col = c("blue", "pink"), pch=19)
  
  library(ggplot2)
  ggplot(data, aes(x=yd,y=sl)) + 
      geom_point(shape=21, aes(col=sx, bg=sx)) + 
      xlab("Years Since Earned Highest Degree") + 
      ylab("Salary (dollars)") + 
      ggtitle("Salary Increases with Experience") + 
      scale_color_discrete(guide=FALSE) + 
      labs(fill="sex")
  </code></pre>
  
  <p>However, I have also made a linear model based on the data:</p>
  
  <pre><code>mod&lt;-lm(sl~sx*poly(yd,2),data)
  </code></pre>
  
  <p>And I am unable to figure out how to plot the data to the graphs. Specifically, I want two lines corresponding to male and female data superimposed on the scatterplot AND to be color-coded. I would assume that R has some way to do this so that I don't have to actually write out the model. Either base plot or ggplot answers are good. Thanks.</p>
  
  <p>Edit:</p>
  
  <p>Running this the above ggplot with <code>geom_smooth(aes(col=sx), se = FALSE, method = "lm", formula = sl ~ sx * poly(yd, 2))</code>: </p>
  
  <pre><code>ggplot(data, aes(x=yd,y=sl)) + geom_point(shape=21, aes(col=sx, bg=sx)) + geom_smooth(aes(col=sx), se = FALSE, method = "lm", formula = sl ~ sx * poly(yd, 2)) + xlab("Years Since Earned Highest Degree") + ylab("Salary (dollars)") + ggtitle("Salary Increases with Experience") + scale_color_discrete(guide=FALSE)+ labs(fill="sex")
  </code></pre>
  
  <p>Returns this error:</p>
  
  <pre><code>Error in model.frame.default(formula = formula, data = data, weights = weight,  : 
    variable lengths differ (found for '(weights)')
  Error in if (nrow(layer_data) == 0) return() : argument is of length zero
  </code></pre>