library(tidyverse, warn.conflicts = F)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.1     ✔ tibble    3.2.0
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth(n = 5)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth() + 
  stat_smooth(geom = "point", 
              xseq = mtcars$wt,
              color = "blue")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth(method = lm) + 
  stat_smooth(geom = "point", 
              xseq = mtcars$wt,
              color = "blue",
              method = lm)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth(method = lm, formula = y ~ 1) + 
  stat_smooth(geom = "point", 
              xseq = mtcars$wt,
              color = "blue",
              method = lm,
              formula = y ~ 1)

mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth(method = lm, formula = y ~ 1) + 
  stat_smooth(geom = "point", 
              xseq = mtcars$wt,
              color = "blue",
              method = lm,
              formula = y ~ 1)

formula = "y ~ x + I(x^2)"
mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth(method = lm, formula = formula) + 
  stat_smooth(geom = "point", 
              xseq = mtcars$wt,
              color = "blue",
              method = lm,
              formula = formula)

formula = "y ~ x + I(x^2) + I(x^3)"
mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth(method = lm, formula = formula) + 
  stat_smooth(geom = "point", 
              xseq = mtcars$wt,
              color = "blue",
              method = lm,
              formula = formula)

formula = "y ~ x + I(x^2) + I(x^3)"
mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth(method = lm, formula = formula) + 
  stat_smooth(geom = "point", 
              xseq = mtcars$wt,
              color = "blue",
              method = lm,
              formula = formula)

formula = 'y ~ splines::bs(x, 3)'
method = "lm"
mtcars %>% 
  ggplot() +
  aes(wt, mpg) + 
  geom_point() + 
  geom_smooth(method = method, formula = formula) + 
  stat_smooth(geom = "point", 
              xseq = mtcars$wt,
              color = "blue",
              method = method, formula = formula)

mtcars %>% 
  rename(y = mpg, x = wt) %>% 
glm(formula = formula, data = .)
## 
## Call:  glm(formula = formula, data = .)
## 
## Coefficients:
##        (Intercept)  splines::bs(x, 3)1  splines::bs(x, 3)2  splines::bs(x, 3)3  
##              32.25              -12.29              -19.99              -20.38  
## 
## Degrees of Freedom: 31 Total (i.e. Null);  28 Residual
## Null Deviance:       1126 
## Residual Deviance: 203.7     AIC: 160
binomial_smooth <- function(...) {
  geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
}

ggplot(rpart::kyphosis, aes(Age, Kyphosis)) +
  geom_jitter(height = 0.05) +
  binomial_smooth()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Computation failed in `stat_smooth()`
## Caused by error:
## ! y values must be 0 <= y <= 1

ggplot(rpart::kyphosis, aes(Age, as.numeric(Kyphosis) - 1)) +
  geom_jitter(height = 0.05) +
  binomial_smooth()
## `geom_smooth()` using formula = 'y ~ x'

ggplot(rpart::kyphosis, aes(Age, as.numeric(Kyphosis) - 1)) +
  geom_jitter(height = 0.05) +
  binomial_smooth(formula = y ~ splines::ns(x, 2))