# NOT RUN {
# 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)
my.data <- data.frame(x = x, y = y,
group = c("A", "B"),
y2 = y * c(0.5,2),
w = sqrt(x))
# give a name to a formula
formula <- y ~ poly(x, 3, raw = TRUE)
# no weights
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, parse = TRUE)
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, parse = TRUE,
label.y = "bottom", label.x = "right")
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, parse = TRUE,
label.y = 0.1, label.x = 0.9)
# using weights
ggplot(my.data, aes(x, y, weight = w)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, parse = TRUE)
# no weights, digits for R square
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, rr.digits = 4, parse = TRUE)
# user specified label
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(aes(label = paste(stat(eq.label),
stat(adj.rr.label), sep = "*\", \"*")),
formula = formula, parse = TRUE)
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(aes(label = paste(stat(f.value.label),
stat(p.value.label), sep = "*\", \"*")),
formula = formula, parse = TRUE)
# user specified label and digits
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(aes(label = paste(stat(eq.label),
stat(adj.rr.label), sep = "*\", \"*")),
formula = formula, rr.digits = 3, coef.digits = 4,
parse = TRUE)
# geom = "text"
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(geom = "text", label.x = 100, label.y = 0, hjust = 1,
formula = formula, parse = TRUE)
# using numeric values
# Here we use column "Estimate" from the matrix.
# Other available columns are "Std. Error", "t value" and "Pr(>|t|)".
my.format <-
"b[0]~`=`~%.3g*\", \"*b[1]~`=`~%.3g*\", \"*b[2]~`=`~%.3g*\", \"*b[3]~`=`~%.3g"
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula,
output.type = "numeric",
parse = TRUE,
mapping =
aes(label = sprintf(my.format,
stat(coef.ls)[[1]][[1, "Estimate"]],
stat(coef.ls)[[1]][[2, "Estimate"]],
stat(coef.ls)[[1]][[3, "Estimate"]],
stat(coef.ls)[[1]][[4, "Estimate"]])
)
)
# Examples using geom_debug() to show computed values
#
# This provides a quick way of finding out which variables are available for
# use in mapping of aesthetics when using other geoms as in the examples
# above.
library(gginnards)
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, geom = "debug")
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(aes(label = stat(eq.label)),
formula = formula, geom = "debug",
output.type = "markdown")
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, geom = "debug", output.type = "text")
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, geom = "debug", output.type = "numeric")
# show the content of a list column
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", 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