This is a collection of small utilities for formatting numbers, especially for use in charts and tables. Many of them are simply wrappers around scales
functions.
Usage
round100(x, ...)
comma(x, ...)
percent100(x, ...)
percent_lt1(x, ...)
percent_txt(x, accuracy = 1, ...)
percent_eng(x, accuracy = 1, sentence_case = TRUE)
dollark(x, accuracy = 1, ...)
dollar1(x, ...)
Arguments
- x
A numeric vector
- ...
Additional arguments to pass to underlying
scales
functions, such asscales::label_percent
andscales::label_number
.- accuracy
A number to round to. Use (e.g.)
0.01
to show 2 decimal places of precision. IfNULL
, the default, uses a heuristic that should ensure breaks have the minimum number of digits needed to show the difference between adjacent values.Applied to rescaled data.
- sentence_case
Boolean: if
TRUE
(default), text will also be converted to sentence case, i.e. capitalizing the first letter
Details
round100
takes a decimal, such as a percentage, multiplies by 100, and rounds, e.g. 0.251
becomes "25"
, useful for then pasting with additional text (see examples).
percent100
adds on to round100
by appending a percent sign, e.g. 0.251
becomes "25%"
.
percent_lt1
takes an additional step, replacing any values below 1 percent with "<1%"
, useful for suppressing small numbers.
Examples
paste("The value is", percent_txt(0.25))
#> [1] "The value is 25 percent"
percent100(c(0.25, 0.251, 0.008))
#> [1] "25%" "25%" "1%"
percent_lt1(c(0.25, 0.251, 0.008))
#> [1] "25%" "25%" "<1%"
paste(percent_eng(0.1), "of adults say...")
#> [1] "Ten percent of adults say..."
dollar1(c(81.1, 105.22))
#> [1] "$81" "$105"
dollark(c(12345, 89100))
#> [1] "$12k" "$89k"
dollark(c(12345, 89100), accuracy = 0.1)
#> [1] "$12.3k" "$89.1k"