B.5 Trend line with geom_smooth

We can add a trendline to our scatter plot using geom_smooth after geom_point.

Code
g = ggplot(d, aes(x = ascore, 
                  y = hscore))+
  geom_point()+
  geom_smooth(color = pubdarkgray)

g %>% 
  pub(xlim = c(65, 165), 
      ylim = c(65, 165))

[1] 57.35768
[1] 80
[1] 20
[1] 157.3577

By default it will be a smooth curve as opposed to a line. For a line, use method = 'lm' (lm is the function used for linear regression).

Code
g = ggplot(d, aes(x = ascore, 
                  y = hscore))+
  geom_point()+
  geom_smooth(method = 'lm', 
              color  = pubdarkgray)

g %>% 
  pub(xlim = c(65, 165), 
      ylim = c(65, 165))

[1] 57.35768
[1] 80
[1] 20
[1] 157.3577