number is a generic formatter for numeric values. en is a shortcut for English format (comma as separator for thousands, point for decimal), fr for French format (space for thousands, comma for decimal). percent a shortcut for English percentages (value are multiplied by 100 and a % symbol is added) and pourcent a shortcut for French percentages. comp_percent returns the complement of 1, i.e. percent(1 - x). comp_pourcent is the French version. *_format functions will return another functions, useful to be used for example with ggplot2. en0 to en5 are shortcuts for en with 0 to 5 digits after decimal point. Similarly, fr0 to fr5 are shortcuts of fr, percent0 to percent5 of percent, etc.

number(
  x,
  accuracy = NULL,
  scale = 1,
  prefix = "",
  suffix = "",
  big.mark = " ",
  decimal.mark = ".",
  style_positive = c("none", "plus"),
  style_negative = c("hyphen", "minus", "parens"),
  scale_cut = NULL,
  trim = TRUE,
  ...
)

label_number(
  accuracy = NULL,
  scale = 1,
  prefix = "",
  suffix = "",
  big.mark = " ",
  decimal.mark = ".",
  style_positive = c("none", "plus"),
  style_negative = c("hyphen", "minus", "parens"),
  scale_cut = NULL,
  trim = TRUE,
  ...
)

label_en(
  accuracy = 1,
  scale = 1,
  prefix = "",
  suffix = "",
  big.mark = ",",
  decimal.mark = ".",
  trim = TRUE,
  ...
)

en(
  x,
  accuracy = 1,
  scale = 1,
  prefix = "",
  suffix = "",
  big.mark = ",",
  decimal.mark = ".",
  trim = TRUE,
  ...
)

label_fr(
  accuracy = 1,
  scale = 1,
  prefix = "",
  suffix = "",
  big.mark = " ",
  decimal.mark = ",",
  trim = TRUE,
  ...
)

fr(
  x,
  accuracy = 1,
  scale = 1,
  prefix = "",
  suffix = "",
  big.mark = " ",
  decimal.mark = ",",
  trim = TRUE,
  ...
)

label_percent(
  accuracy = NULL,
  scale = 100,
  prefix = "",
  suffix = "%",
  big.mark = " ",
  decimal.mark = ".",
  trim = TRUE,
  ...
)

percent(
  x,
  accuracy = NULL,
  scale = 100,
  prefix = "",
  suffix = "%",
  big.mark = " ",
  decimal.mark = ".",
  trim = TRUE,
  ...
)

label_pourcent(
  accuracy = 1,
  scale = 100,
  prefix = "",
  suffix = " %",
  big.mark = " ",
  decimal.mark = ",",
  trim = TRUE,
  ...
)

pourcent(
  x,
  accuracy = 1,
  scale = 100,
  prefix = "",
  suffix = " %",
  big.mark = " ",
  decimal.mark = ",",
  trim = TRUE,
  ...
)

comp_label_percent(
  accuracy = 1,
  scale = 100,
  prefix = "",
  suffix = "%",
  big.mark = " ",
  decimal.mark = ".",
  trim = TRUE,
  ...
)

comp_percent(
  x,
  accuracy = 1,
  scale = 100,
  prefix = "",
  suffix = "%",
  big.mark = " ",
  decimal.mark = ".",
  trim = TRUE,
  ...
)

comp_label_pourcent(
  accuracy = 1,
  scale = 100,
  prefix = "",
  suffix = " %",
  big.mark = " ",
  decimal.mark = ",",
  trim = TRUE,
  ...
)

comp_pourcent(
  x,
  accuracy = 1,
  scale = 100,
  prefix = "",
  suffix = " %",
  big.mark = " ",
  decimal.mark = ",",
  trim = TRUE,
  ...
)

en0(x)

en1(x)

en2(x)

en3(x)

en4(x)

en5(x)

fr0(x)

fr1(x)

fr2(x)

fr3(x)

fr4(x)

fr5(x)

percent0(x)

percent1(x)

percent2(x)

percent3(x)

percent4(x)

percent5(x)

pourcent0(x)

pourcent1(x)

pourcent2(x)

pourcent3(x)

pourcent4(x)

pourcent5(x)

comp_percent0(x)

comp_percent1(x)

comp_percent2(x)

comp_percent3(x)

comp_percent4(x)

comp_percent5(x)

comp_pourcent0(x)

comp_pourcent1(x)

comp_pourcent2(x)

comp_pourcent3(x)

comp_pourcent4(x)

comp_pourcent5(x)

Arguments

x

a numeric vector to format

accuracy

A number to round to. Use (e.g.) 0.01 to show 2 decimal places of precision. If NULL, 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.

scale

A scaling factor: x will be multiplied by scale before formatting. This is useful if the underlying data is very small or very large.

prefix

Additional text to display before the number. The suffix is applied to absolute value before style_positive and style_negative are processed so that prefix = "$" will yield (e.g.) -$1 and ($1).

suffix

Additional text to display after the number.

big.mark

Character used between every 3 digits to separate thousands.

decimal.mark

The character to be used to indicate the numeric decimal point.

style_positive

A string that determines the style of positive numbers:

  • "none" (the default): no change, e.g. 1.

  • "plus": preceded by +, e.g. +1.

style_negative

A string that determines the style of negative numbers:

  • "hyphen" (the default): preceded by a standard hypen -, e.g. -1.

  • "minus", uses a proper Unicode minus symbol. This is a typographical nicety that ensures - aligns with the horizontal bar of the the horizontal bar of +.

  • "parens", wrapped in parentheses, e.g. (1).

scale_cut

Named numeric vector that allows you to rescale large (or small) numbers and add a prefix. Built-in helpers include:

  • cut_short_scale(): [10^3, 10^6) = K, [10^6, 10^9) = M, [10^9, 10^12) = B, [10^12, Inf) = T.

  • cut_long_scale(): [10^3, 10^6) = K, [10^6, 10^12) = M, [10^12, 10^18) = B, [10^18, Inf) = T.

  • cut_si(unit): uses standard SI units.

If you supply a vector c(a = 100, b = 1000), absolute values in the range [0, 100) will not be rescaled, absolute values in the range [100, 1000) will be divided by 100 and given the suffix "a", and absolute values in the range [1000, Inf) will be divided by 1000 and given the suffix "b".

trim

Logical, if FALSE, values are right-justified to a common width (see base::format()).

...

Other arguments passed on to base::format().

Value

a formatted character vector or, for label_* functions, a function with single parameter x, a numeric vector, that returns a character vector

Examples

v <- c(12.3, 4, 12345.789, 0.0002)
number(v)
#> [1] "12.3"     "4.0"      "12 345.8" "0.0"     
en(v)
#> [1] "12"     "4"      "12,346" "0"     
fr(v)
#> [1] "12"     "4"      "12 346" "0"     
en2(v)
#> [1] "12.30"     "4.00"      "12,345.79" "0.00"     
en(v, accuracy = .001)
#> [1] "12.300"     "4.000"      "12,345.789" "0.000"     
en(v, accuracy = .5)
#> [1] "12.5"     "4.0"      "12,346.0" "0.0"     

p <- runif(10)
p
#>  [1] 0.10295904 0.70020340 0.34467833 0.79533091 0.15183486 0.06944454
#>  [7] 0.57258371 0.88031890 0.62055229 0.77745843
percent(p)
#>  [1] "10.3%" "70.0%" "34.5%" "79.5%" "15.2%" "6.9%"  "57.3%" "88.0%" "62.1%"
#> [10] "77.7%"
percent2(p)
#>  [1] "10.30%" "70.02%" "34.47%" "79.53%" "15.18%" "6.94%"  "57.26%" "88.03%"
#>  [9] "62.06%" "77.75%"
pourcent2(p)
#>  [1] "10,30 %" "70,02 %" "34,47 %" "79,53 %" "15,18 %" "6,94 %"  "57,26 %"
#>  [8] "88,03 %" "62,06 %" "77,75 %"
comp_percent(p)
#>  [1] "90%" "30%" "66%" "20%" "85%" "93%" "43%" "12%" "38%" "22%"

# Per mille
per_mille <- label_number(scale = 1000, suffix = "\u2030", accuracy = .1)
per_mille(v)
#> [1] "12 300.0‰"     "4 000.0‰"      "12 345 789.0‰" "0.2‰"