# NOT RUN {
library(broom)
library(gginnards)
library(quantreg)
# Regression by panel example, using geom_debug.
ggplot(mtcars, aes(x = disp, y = mpg)) +
stat_smooth(method = "lm") +
geom_point(aes(colour = factor(cyl))) +
stat_fit_glance(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_glance(method = "lm",
label.y = "bottom",
method.args = list(formula = y ~ x),
mapping = aes(label = sprintf('r^2~"="~%.3f~~italic(P)~"="~%.2g',
stat(r.squared), stat(p.value))),
parse = TRUE)
# Regression by group example
ggplot(mtcars, aes(x = disp, y = mpg, colour = factor(cyl))) +
stat_smooth(method = "lm") +
geom_point() +
stat_fit_glance(method = "lm",
label.y = "bottom",
method.args = list(formula = y ~ x),
mapping = aes(label = sprintf('r^2~"="~%.3f~~italic(P)~"="~%.2g',
stat(r.squared), stat(p.value))),
parse = TRUE)
# Weighted regression example
ggplot(mtcars, aes(x = disp, y = mpg, weight = cyl)) +
stat_smooth(method = "lm") +
geom_point(aes(colour = factor(cyl))) +
stat_fit_glance(method = "lm",
label.y = "bottom",
method.args = list(formula = y ~ x, weights = quote(weight)),
mapping = aes(label = sprintf('r^2~"="~%.3f~~italic(P)~"="~%.2g',
stat(r.squared), stat(p.value))),
parse = TRUE)
# correlation test
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point() +
stat_fit_glance(method = "cor.test",
label.y = "bottom",
method.args = list(formula = ~ x + y),
mapping = aes(label = sprintf('r[Pearson]~"="~%.3f~~italic(P)~"="~%.2g',
stat(estimate), stat(p.value))),
parse = TRUE)
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point() +
stat_fit_glance(method = "cor.test",
label.y = "bottom",
method.args = list(formula = ~ x + y, method = "spearman", exact = FALSE),
mapping = aes(label = sprintf('r[Spearman]~"="~%.3f~~italic(P)~"="~%.2g',
stat(estimate), stat(p.value))),
parse = TRUE)
# Quantile regression by group example
ggplot(mtcars, aes(x = disp, y = mpg)) +
stat_smooth(method = "lm") +
geom_point() +
stat_fit_glance(method = "rq",
label.y = "bottom",
method.args = list(formula = y ~ x),
mapping = aes(label = sprintf('AIC = %.3g, BIC = %.3g',
stat(AIC), stat(BIC))))
# }
Run the code above in your browser using DataLab