Skip to contents

Assemble model-fit-derived text or expressions and map them to the label aesthetic.

Usage

use_label(
  labels = c("eq", "p.value"),
  other.mapping = NULL,
  sep = "*\", \"*"
)

Arguments

labels

character A vector with the name of the label components in the order they will be included in the combined label.

other.mapping

An unevaluated expression constructed with function aes.

sep

character A string used as separator when pasting the label components together.

Value

A to the label aesthetic as an unevaluated R expression, built using function aes.

Details

Statistics stat_poly_eq, stat_ma_eq, stat_quant_eq and stat_correlation return multiple text strings to be used individually or assembled into longer character strings depending on the labels actually desired. Assembling and mapping them requires verbose R code and familiarity with R expression syntax. Function use_label() automates these two taks and accepts abbreviated familiar names for the parameters in addition to the name of the columns in the data object returned by the statistics. The default separator is that for expressions.

The statistics return variables with names ending in .label. This ending can be omitted, as well as .value for f.value.label, t.value.label, z.value.label, S.value.label and p.value.label. R2 can be used in place of rr. Furthermore, case is ignored.

Note

Function use_label() can be only used to generate an argument passed to formal parameter mapping of the statistics stat_poly_eq, stat_ma_eq, stat_quant_eq and stat_correlation.

Examples

# 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)

ggplot(my.data, aes(x, y2, colour = group)) +
  geom_point() +
  stat_poly_line(formula = formula) +
  stat_poly_eq(use_label(c("eq", "F")), formula = formula)


ggplot(my.data, aes(x, y2, colour = group)) +
  geom_point() +
  stat_poly_line(formula = formula) +
  stat_poly_eq(use_label(c("R2", "F"), sep = "*\" with \"*"),
               formula = formula)


ggplot(my.data, aes(x, y2, colour = group)) +
  geom_point() +
  stat_poly_line(formula = formula) +
  stat_poly_eq(use_label(c("eq", "adj.R2", "n")), formula = formula)


ggplot(my.data, aes(x, y2, colour = group)) +
  geom_point() +
  stat_poly_line(formula = formula) +
  stat_poly_eq(use_label(c("F", "P", "AIC", "BIC")), formula = formula)


ggplot(my.data, aes(x, y)) +
  stat_quant_band(formula = formula) +
  stat_quant_eq(use_label(c("eq", "n")), formula = formula) +
  geom_point()


ggplot(my.data, aes(x, y)) +
  stat_ma_line() +
  stat_ma_eq(use_label(c("eq", "n"))) +
  geom_point()


ggplot(my.data, aes(x, y)) +
  stat_correlation(use_label(c("r", "t", "p"))) +
  geom_point()


# pass additional mappings if desired
ggplot(my.data, aes(x, y2)) +
  geom_point(aes(colour = group)) +
  stat_poly_line(aes(colour = group), formula = formula) +
  stat_poly_eq(use_label(c("eq", "F"), aes(grp.label = group)), formula = formula)