GGPLOT - geom_smooth

Creates smoothed conditional means and then convert them with ggplotly

p <-   
 ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth()
plotly::ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
p <-   
 ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(orientation = "y")
plotly::ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 80, 0
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 80, 0
p <-   
 ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(span = 0.3)
plotly::ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
p <-   
 ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(method = lm, se = FALSE)
plotly::ggplotly(p)
## `geom_smooth()` using formula 'y ~ x'
p <-   
 ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(method = lm, formula = y ~ splines::bs(x, 3), se = FALSE)
plotly::ggplotly(p)
p <-   
 ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point() +
  geom_smooth(se = FALSE, method = lm)
plotly::ggplotly(p)
## `geom_smooth()` using formula 'y ~ x'
p <-   
 ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(span = 0.8) +
  facet_wrap(~drv)
plotly::ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
binomial_smooth <- function(...) {
  geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
}
p <-   
 ggplot(rpart::kyphosis, aes(Age, Kyphosis)) +
  geom_jitter(height = 0.05) +
  binomial_smooth()
plotly::ggplotly(p)
## `geom_smooth()` using formula 'y ~ x'
## Warning: Computation failed in `stat_smooth()`:
## y values must be 0 <= y <= 1
binomial_smooth <- function(...) {
  geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
}
p <-   
 ggplot(rpart::kyphosis, aes(Age, as.numeric(Kyphosis) - 1)) +
  geom_jitter(height = 0.05) +
  binomial_smooth()
plotly::ggplotly(p)
## `geom_smooth()` using formula 'y ~ x'
binomial_smooth <- function(...) {
  geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
}
p <-   
 ggplot(rpart::kyphosis, aes(Age, as.numeric(Kyphosis) - 1)) +
  geom_jitter(height = 0.05) +
  binomial_smooth(formula = y ~ splines::ns(x, 2))
plotly::ggplotly(p)