# generate artificial data
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
y <- y / max(y)
my.data <- data.frame(x = x, y = y,
group = c("A", "B"),
y2 = y * c(1, 2) + c(0, 0.1),
w = sqrt(x))
# give a name to a formula
formula <- y ~ poly(x, 3, raw = TRUE)
# using defaults
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line() +
stat_poly_eq()
# no weights
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula)
# other labels
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(use_label("eq"), formula = formula)
# other labels
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(use_label("eq"), formula = formula, decreasing = TRUE)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(use_label("eq", "R2"), formula = formula)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(use_label("R2", "R2.CI", "P", "method"), formula = formula)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(use_label("R2", "F", "P", "n", sep = "*\"; \"*"),
formula = formula)
# grouping
ggplot(my.data, aes(x, y2, color = group)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula)
# rotation
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, angle = 90)
# label location
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, label.y = "bottom", label.x = "right")
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, label.y = 0.1, label.x = 0.9)
# modifying the explanatory variable within the model formula
# modifying the response variable within aes()
formula.trans <- y ~ I(x^2)
ggplot(my.data, aes(x, y + 1)) +
geom_point() +
stat_poly_line(formula = formula.trans) +
stat_poly_eq(use_label("eq"),
formula = formula.trans,
eq.x.rhs = "~x^2",
eq.with.lhs = "y + 1~~`=`~~")
# using weights
ggplot(my.data, aes(x, y, weight = w)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula)
# no weights, 4 digits for R square
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, rr.digits = 4)
# manually assemble and map a specific label using paste() and aes()
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(aes(label = paste(after_stat(rr.label),
after_stat(n.label), sep = "*\", \"*")),
formula = formula)
# manually assemble and map a specific label using sprintf() and aes()
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(aes(label = sprintf("%s*\" with \"*%s*\" and \"*%s",
after_stat(rr.label),
after_stat(f.value.label),
after_stat(p.value.label))),
formula = formula)
# x on y regression
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula, orientation = "y") +
stat_poly_eq(use_label("eq", "adj.R2"),
formula = x ~ poly(y, 3, raw = TRUE))
# conditional user specified label
ggplot(my.data, aes(x, y2, color = group)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(aes(label = ifelse(after_stat(adj.r.squared) > 0.96,
paste(after_stat(adj.rr.label),
after_stat(eq.label),
sep = "*\", \"*"),
after_stat(adj.rr.label))),
rr.digits = 3,
formula = formula)
# geom = "text"
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(geom = "text", label.x = 100, label.y = 0, hjust = 1,
formula = formula)
# using numeric values
# Here we use columns b_0 ... b_3 for the coefficient estimates
my.format <-
"b[0]~`=`~%.3g*\", \"*b[1]~`=`~%.3g*\", \"*b[2]~`=`~%.3g*\", \"*b[3]~`=`~%.3g"
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula,
output.type = "numeric",
parse = TRUE,
mapping =
aes(label = sprintf(my.format,
after_stat(b_0), after_stat(b_1),
after_stat(b_2), after_stat(b_3))))
# Inspecting the returned data using geom_debug()
# This provides a quick way of finding out the names of the variables that
# are available for mapping to aesthetics with after_stat().
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed)
library(gginnards)
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, geom = "debug")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, geom = "debug", output.type = "numeric")
# names of the variables
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, geom = "debug",
summary.fun = colnames)
# only data$eq.label
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, geom = "debug",
output.type = "expression",
summary.fun = function(x) {x[["eq.label"]]})
# only data$eq.label
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(aes(label = after_stat(eq.label)),
formula = formula, geom = "debug",
output.type = "markdown",
summary.fun = function(x) {x[["eq.label"]]})
# only data$eq.label
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, geom = "debug",
output.type = "latex",
summary.fun = function(x) {x[["eq.label"]]})
# only data$eq.label
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, geom = "debug",
output.type = "text",
summary.fun = function(x) {x[["eq.label"]]})
# show the content of a list column
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(formula = formula, geom = "debug", output.type = "numeric",
summary.fun = function(x) {x[["coef.ls"]][[1]]})
Run the code above in your browser using DataLab