# NOT RUN {
library(broom)
library(gginnards)
library(quantreg)
# Regression by panel, exploring computed variables with geom_debug()
ggplot(mtcars, aes(x = disp, y = mpg)) +
stat_smooth(method = "lm") +
geom_point(aes(colour = factor(cyl))) +
stat_fit_tidy(method = "lm",
method.args = list(formula = y ~ x),
geom = "debug")
# Regression by panel example
ggplot(mtcars, aes(x = disp, y = mpg)) +
stat_smooth(method = "lm") +
geom_point(aes(colour = factor(cyl))) +
stat_fit_tidy(method = "lm",
label.x = "right",
method.args = list(formula = y ~ x),
mapping = aes(label = sprintf("Slope = %.3g\np-value = %.3g",
stat(x_estimate),
stat(x_p.value))))
# Regression by group example
ggplot(mtcars, aes(x = disp, y = mpg, colour = factor(cyl))) +
stat_smooth(method = "lm") +
geom_point() +
stat_fit_tidy(method = "lm",
label.x = "right",
method.args = list(formula = y ~ x),
mapping = aes(label = sprintf("Slope = %.3g, p-value = %.3g",
stat(x_estimate),
stat(x_p.value))))
# Weighted regression example
ggplot(mtcars, aes(x = disp, y = mpg, weight = cyl)) +
stat_smooth(method = "lm") +
geom_point(aes(colour = factor(cyl))) +
stat_fit_tidy(method = "lm",
label.x = "right",
method.args = list(formula = y ~ x, weights = quote(weight)),
mapping = aes(label = sprintf("Slope = %.3g\np-value = %.3g",
stat(x_estimate),
stat(x_p.value))))
# Correlation test
ggplot(mtcars, aes(x = disp, y = mpg)) +
stat_smooth(method = "lm") +
geom_point() +
stat_fit_tidy(method = "cor.test",
label.y = "bottom",
method.args = list(formula = ~ x + y),
mapping = aes(label = sprintf("R = %.3g\np-value = %.3g",
stat(`_estimate`),
stat(`_p.value`))))
# Quantile regression
ggplot(mtcars, aes(x = disp, y = mpg)) +
stat_smooth(method = "lm") +
geom_point() +
stat_fit_tidy(method = "rq",
label.y = "bottom",
method.args = list(formula = y ~ x),
tidy.args = list(se.type = "nid"),
mapping = aes(label = sprintf("Slope = %.3g\np-value = %.3g",
stat(x_estimate),
stat(x_p.value))))
# }
Run the code above in your browser using DataLab