Use "{l}" and "{r}" to show the left and right endpoints of the intervals.
Arguments
- label
A glue string passed to
glue::glue().- fmt
String, list or function. A format for break endpoints.
- single
Glue string: label for singleton intervals. See
lbl_glue()for details. IfNULL, singleton intervals will be labelled the same way as other intervals.- first
Glue string: override label for the first category. Write e.g.
first = "<{r}"to create a label like"<18". Seelbl_glue()for details.- last
String: override label for the last category. Write e.g.
last = ">{l}"to create a label like">65". Seelbl_glue()for details.- raw
. Use the
rawargument tochop()instead.- ...
Further arguments passed to
glue::glue().
Details
The following variables are available in the glue string:
lis a character vector of left endpoints of intervals.ris a character vector of right endpoints of intervals.l_closedis a logical vector. Elements areTRUEwhen the left endpoint is closed.r_closedis a logical vector,TRUEwhen the right endpoint is closed.
Endpoints will be formatted by fmt before being passed to glue().
Formatting endpoints
If fmt is not NULL then it is used to format the endpoints.
If
fmtis a string, then numeric endpoints will be formatted bysprintf(fmt, breaks); other endpoints, e.g. Date objects, will be formatted byformat(breaks, fmt).If
fmtis a list, then it will be used as arguments to format.If
fmtis a function, it should take a vector of numbers (or other objects that can be used as breaks) and return a character vector. It may be helpful to use functions from the{scales}package, e.g.scales::label_comma().
See also
Other labelling functions:
lbl_dash(),
lbl_discrete(),
lbl_endpoints(),
lbl_intervals(),
lbl_manual(),
lbl_midpoints(),
lbl_seq()
Examples
tab(1:10, c(1, 3, 3, 7),
labels = lbl_glue("{l} to {r}", single = "Exactly {l}"))
#> 1 to 3 Exactly 3 3 to 7 7 to 10
#> 2 1 3 4
tab(1:10 * 1000, c(1, 3, 5, 7) * 1000,
labels = lbl_glue("{l}-{r}",
fmt = function(x) prettyNum(x, big.mark=',')))
#> 1,000-3,000 3,000-5,000 5,000-7,000 7,000-10,000
#> 2 2 2 4
# reproducing lbl_intervals():
interval_left <- "{ifelse(l_closed, '[', '(')}"
interval_right <- "{ifelse(r_closed, ']', ')')}"
glue_string <- paste0(interval_left, "{l}", ", ", "{r}", interval_right)
tab(1:10, c(1, 3, 3, 7), labels = lbl_glue(glue_string, single = "{{{l}}}"))
#> [1, 3) {3} (3, 7) [7, 10]
#> 2 1 3 4
