# 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)
# grouping
ggplot(my.data, aes(x, y, color = group)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(formula = formula)
# rotation
ggplot(my.data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(formula = formula, angle = 90, hjust = 1)
# label location
ggplot(my.data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(formula = formula, 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, 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)
# 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)
# user specified label
ggplot(my.data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(aes(label =  paste(after_stat(rr.label),
                                  after_stat(n.label), sep = "*\", \"*")),
               formula = formula)
ggplot(my.data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(aes(label =  paste(after_stat(eq.label),
                                  after_stat(adj.rr.label), sep = "*\", \"*")),
               formula = formula)
ggplot(my.data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(aes(label =  paste(after_stat(f.value.label),
                                  after_stat(p.value.label),
                                  sep = "*\", \"*")),
               formula = formula)
# x on y regression
ggplot(my.data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula, orientation = "y") +
  stat_poly_eq(aes(label =  paste(after_stat(eq.label),
                                  after_stat(adj.rr.label),
                                  sep = "*\", \"*")),
               formula = x ~ poly(y, 3, raw = TRUE))
# conditional user specified label
ggplot(my.data, aes(x, y, color = group)) +
  geom_point() +
  geom_smooth(method = "lm", 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() +
  geom_smooth(method = "lm", 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() +
  geom_smooth(method = "lm", 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()
if (requireNamespace("gginnards", quietly = TRUE)) {
  library(gginnards)
# This provides a quick way of finding out the names of the variables that
# are available for mapping to aesthetics.
# the whole of data
  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(formula = formula, geom = "debug", output.type = "numeric")
# names of the variables
  ggplot(my.data, aes(x, y)) +
    geom_point() +
    geom_smooth(method = "lm", formula = formula) +
    stat_poly_eq(formula = formula, geom = "debug",
                 summary.fun = colnames)
# only data$eq.label
  ggplot(my.data, aes(x, y)) +
    geom_point() +
    geom_smooth(method = "lm", formula = formula) +
    stat_poly_eq(formula = formula, geom = "debug",
                 output.type = "expression",
                 summary.fun = function(x) {x[["eq.label"]]})
# only data$eq.label
  ggplot(my.data, aes(x, y)) +
    geom_point() +
    geom_smooth(method = "lm", 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
  ggplot(my.data, aes(x, y)) +
    geom_point() +
    geom_smooth(method = "lm", formula = formula) +
    stat_poly_eq(formula = formula, geom = "debug",
                 output.type = "latex",
                 summary.fun = function(x) {x[["eq.label"]]})
# only data$eq.label
  ggplot(my.data, aes(x, y)) +
    geom_point() +
    geom_smooth(method = "lm", 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
  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