R/labeller.r
Labeller meaning Alternative spelling of labeler. The labeller can be a function or it can be a named character vectors that will serve as a lookup table. Labeller (.rows = NULL.cols = NULL, keep.as.numeric = NULL.multiline = TRUE.default = labelvalue).
Labeller functions are in charge of formatting the strip labels offacet grids and wraps. Most of them accept a multi_line
argument to control whether multiple factors (defined in formulaesuch as ~first + second
) should be displayed on a singleline separated with commas, or each on their own line.
labels | Data frame of labels. Usually contains only oneelement, but faceting over multiple factors entails multiplelabel variables. |
---|---|
multi_line | Whether to display the labels of multiple factorson separate lines. |
sep | String separating variables and values. |
width | Maximum number of characters before wrapping the strip. |
label_value()
only displays the value of a factor whilelabel_both()
displays both the variable name and the factorvalue. label_context()
is context-dependent and useslabel_value()
for single factor faceting andlabel_both()
when multiple factors areinvolved. label_wrap_gen()
uses base::strwrap()
for line wrapping.
label_parsed()
interprets the labels as plotmathexpressions. label_bquote()
offers a more flexibleway of constructing plotmath expressions. See examples andbquote()
for details on the syntax of theargument.
Note that an easy way to write a labeller function is totransform a function operating on character vectors withas_labeller()
.
A labeller function accepts a data frame of labels (charactervectors) containing one column for each factor. Multiple factorsoccur with formula of the type ~first + second
.
The return value must be a rectangular list where each 'row'characterises a single facet. The list elements can be eithercharacter vectors or lists of plotmath expressions. When multipleelements are returned, they get displayed on their own new lines(i.e., each facet gets a multi-line strip of labels).
To illustrate, let's say your labeller returns a list of twocharacter vectors of length 3. This is a rectangular list becauseall elements have the same length. The first facet will get thefirst elements of each vector and display each of them on theirown line. Then the second facet gets the second elements of eachvector, and so on.
If it's useful to your labeller, you can retrieve the type
attribute of the incoming data frame of labels. The value of thisattribute reflects the kind of strips your labeller is dealingwith: 'cols'
for columns and 'rows'
for rows. Notethat facet_wrap()
has columns by default and rowswhen the strips are switched with the switch
option. Thefacet
attribute also provides metadata on the labels. Ittakes the values 'grid'
or 'wrap'
.
For compatibility with labeller()
, each labellerfunction must have the labeller
S3 class.