Skip to contents

Converts value to scaled format. For example, 10^6 returns "$1 MM" in millions format.

Usage

kDollarsFormat(x, scaleUnit = "MM", useDollarSign = TRUE, roundToDigit = 1)

Arguments

x

A numeric value or vector.

scaleUnit

Default 'MM', the desired scaling to calculate. Thousands = 'K', Millions = 'M' (or 'MM'), Billions = 'B', Trillions = 'T'.

useDollarSign

Default TRUE if you want to add a dollar sign to the suffix. False to omit '$'.

roundToDigit

Digits to round to after the value has been scaled.

Value

Formatted number

Examples

library(ggplot2)
library(styles)

kDollarsFormat(1000, scaleUnit = 'K')
#> [1] "$1 K"
kDollarsFormat(1000000, scaleUnit = 'M')
#> [1] "$1 MM"
kDollarsFormat(1000000, scaleUnit = 'MM')
#> [1] "$1 MM"
kDollarsFormat(1000000000, scaleUnit = 'B')
#> [1] "$1 B"
kDollarsFormat(1500000000000, scaleUnit = 'T')
#> [1] "$1.50 T"
kDollarsFormat(1000000, scaleUnit = 'M', useDollarSign = FALSE)
#> [1] "1 MM"

# Examples within ggplot
df <- mtcars
df$mpg <- df$mpg*1000000

# Create simple ggplot, no data shown by default
gg <- df |>
  ggplot(aes(y=mpg, x=wt)) +
  theme_dc()

# Text labels millions dollars
gg + geom_text(aes(label = kDollarsFormat(mpg)))


# y-axis format in millions of dollars
gg + scale_y_continuous(labels = kDollarsFormat)


# y-axis format in *thousands* of dollars
gg + scale_y_continuous(labels = ~kDollarsFormat(., scaleUnit = 'K'))


# y-axis format with no decimals
gg + scale_y_continuous(labels = ~kDollarsFormat(., roundToDigit = 0))


# y-axis format with no dollar sign
gg + scale_y_continuous(labels = ~kDollarsFormat(., useDollarSign = FALSE))