View source: R/label-dictionary.R
label_dictionary | R Documentation |
Use label_dictionary()
for looking up succinct breaks in a named character
vector giving complete labels.
label_dictionary(dictionary = character(), nomatch = NULL)
dictionary |
A named character vector of labels. The names are expected to match the breaks, and the values become the labels. |
nomatch |
A string to label breaks that do not match any name in
|
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.
Other labels for discrete scales:
label_glue()
,
label_parse()
,
label_wrap()
# Example lookup table
lut <- c(
"4" = "four wheel drive",
"r" = "rear wheel drive",
"f" = "front wheel drive"
)
# Typical usage
demo_discrete(c("4", "r", "f"), labels = label_dictionary(lut))
# By default, extra values ('w') will remain as-is
demo_discrete(c("4", "r", "f", "w"), labels = label_dictionary(lut))
# Alternatively, you can relabel extra values
demo_discrete(
c("4", "r", "f", "w"),
labels = label_dictionary(lut, nomatch = "unknown")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.