Use label_glue()
to perform string interpolation using the glue
package. Enclosed expressions will be evaluated as R code.
label_glue(pattern = "{x}", ..., parse = FALSE, .envir = caller_env())
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.
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()
.
Whether to return labels as expressions.
[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()
.
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()
# 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}"))
# It adapts to the breaks that are present
demo_discrete(species[-3], 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}"))
Run the code above in your browser using DataLab