# 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 * 1e-5,
group = c("A", "B"),
y2 = y * 1e-5 + c(2, 0))
# give a name to a formula
formula <- y ~ poly(x, 3, raw = TRUE)
# default label constructed by use_label()
ggplot(data = my.data,
mapping = aes(x = x, y = y2, colour = group)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(mapping = use_label(),
formula = formula)
# user specified label components
ggplot(data = my.data,
mapping = aes(x = x, y = y2, colour = group)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(mapping = use_label("eq", "F"),
formula = formula)
# user specified label components and separator
ggplot(data = my.data,
mapping = aes(x = x, y = y2, colour = group)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(mapping = use_label("R2", "F", sep = "*\" with \"*"),
formula = formula)
# combine the mapping to the label aesthetic with other mappings
ggplot(data = my.data,
mapping = aes(x = x, y = y2)) +
geom_point(mapping = aes(colour = group)) +
stat_poly_line(mapping = aes(colour = group), formula = formula) +
stat_poly_eq(mapping = use_label("grp", "eq", "F",
aes(grp.label = group)),
formula = formula)
# combine other mappings with default labels
ggplot(data = my.data,
mapping = aes(x = x, y = y2)) +
geom_point(mapping = aes(colour = group)) +
stat_poly_line(mapping = aes(colour = group), formula = formula) +
stat_poly_eq(mapping = use_label(aes(colour = group)),
formula = formula)
# example with other available components
ggplot(data = my.data,
mapping = aes(x = x, y = y2, colour = group)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(mapping = use_label("eq", "adj.R2", "n"),
formula = formula)
# multiple labels
ggplot(data = my.data,
mapping = aes(x, y2, colour = group)) +
geom_point() +
stat_poly_line(formula = formula) +
stat_poly_eq(mapping = use_label("R2", "F", "P", "AIC", "BIC"),
formula = formula) +
stat_poly_eq(mapping = use_label(c("eq", "n")),
formula = formula,
label.y = "bottom",
label.x = "right")
# quantile regression
ggplot(data = my.data,
mapping = aes(x, y)) +
stat_quant_band(formula = formula) +
stat_quant_eq(mapping = use_label("eq", "n"),
formula = formula) +
geom_point()
# major axis regresion
ggplot(data = my.data, aes(x = x, y = y)) +
stat_ma_line() +
stat_ma_eq(mapping = use_label("eq", "n")) +
geom_point()
# correlation
ggplot(data = my.data,
mapping = aes(x = x, y = y)) +
stat_correlation(mapping = use_label("r", "t", "p")) +
geom_point()
Run the code above in your browser using DataLab