Pretty print scaled numbers

pretty_num(
  x,
  units = c(k = 1000, M = 1e+06, B = 1e+09),
  decimal_digits = 1,
  no_dot_zero = FALSE
)

format_pretty_num(
  units = c(k = 1000, M = 1e+06, B = 1e+09),
  decimal_digits = 1,
  no_dot_zero = TRUE
)

Arguments

x

Vector of numbers

units

Named list with lower bounds for units. Names are units or unit abbreviations.

decimal_digits

Number of digits to show after the decimal point in the selected unit.

no_dot_zero

Should trailing .0 be removed regardless of the decimal digits requested?

Functions

  • format_pretty_num: A label formatter for ggplot2

Examples

pretty_num(1234)
#> [1] "1.2k"
pretty_num(12345, decimal_digits = 3)
#> [1] "12.345k"
pretty_num(123456789)
#> [1] "123.5M"
pretty_num(12345678900)
#> [1] "12.3B"
pretty_num(c(1234, 1234567, 12345678900))
#> [1] "1.2k" "1.2M" "12.3B"
library(ggplot2) ggplot(mtcars) + aes(mpg, wt * 1000) + geom_point() + scale_y_continuous(labels = format_pretty_num())