Skip to content

Use label_glue() to perform string interpolation using the glue package. Enclosed expressions will be evaluated as R code.

Usage

label_glue(pattern = "{x}", ..., parse = FALSE, .envir = caller_env())

Arguments

pattern

A glue string used for formatting. The x variable holds the breaks, so that "{x}" (default) returns the breaks as-is.

...

Arguments passed on to glue::glue().

parse

Whether to return labels as expressions.

.envir

[environment: parent.frame()]
Environment to evaluate each expression in. Expressions are evaluated from left to right. If .x is an environment, the expressions are evaluated in that environment and .envir is ignored. If NULL is passed, it is equivalent to emptyenv().

Value

All label_() functions return a "labelling" function, i.e. a function that takes a vector x and returns a character vector of length(x) giving a label for each input value.

Labelling functions are designed to be used with the labels argument of ggplot2 scales. The examples demonstrate their use with x scales, but they work similarly for all scales, including those that generate legends rather than axes.

See also

Other labels for continuous scales: label_bytes(), label_currency(), label_number_auto(), label_number_si(), label_ordinal(), label_parse(), label_percent(), label_pvalue(), label_scientific()

Other labels for discrete scales: label_dictionary(), label_parse(), label_wrap()

Examples

# Example variables
animal  <- "penguin"
species <- c("Adelie",  "Chinstrap", "Emperor", "Gentoo")

# Typical use, note that {x} will become the breaks
demo_discrete(species, labels = label_glue("The {x}\n{animal}"))
#> scale_x_discrete(labels = label_glue("The {x}\n{animal}"))

# It adapts to the breaks that are present
demo_discrete(species[-3], labels =  label_glue("The {x}\n{animal}"))
#> scale_x_discrete(labels = label_glue("The {x}\n{animal}"))

# Contrary to directly glueing species + animal, which results in mislabelling!
demo_discrete(species[-3], labels = glue::glue("The {species}\n{animal}"))
#> scale_x_discrete(labels = glue::glue("The {species}\n{animal}"))