Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

label_dollar

Label currencies ($100, $2.50, etc)


Description

Format numbers as currency, rounding values to dollars or cents using a convenient heuristic.

Usage

label_dollar(
  accuracy = NULL,
  scale = 1,
  prefix = "$",
  suffix = "",
  big.mark = ",",
  decimal.mark = ".",
  trim = TRUE,
  largest_with_cents = 1e+05,
  negative_parens = FALSE,
  ...
)

dollar_format(
  accuracy = NULL,
  scale = 1,
  prefix = "$",
  suffix = "",
  big.mark = ",",
  decimal.mark = ".",
  trim = TRUE,
  largest_with_cents = 1e+05,
  negative_parens = FALSE,
  ...
)

dollar(
  x,
  accuracy = NULL,
  scale = 1,
  prefix = "$",
  suffix = "",
  big.mark = ",",
  decimal.mark = ".",
  trim = TRUE,
  largest_with_cents = 1e+05,
  negative_parens = FALSE,
  ...
)

Arguments

accuracy, largest_with_cents

Number to round to. If NULL, the default, values will be rounded to the nearest integer, unless any of the values has non-zero fractional component (e.g. cents) and the largest value is less than largest_with_cents which by default is 100,000.

scale

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

prefix, suffix

Symbols to display before and after value.

big.mark

Character used between every 3 digits to separate thousands.

decimal.mark

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

trim

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

negative_parens

Display negative using parentheses?

...

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

x

A numeric vector

Value

All label_() functions return a "labelling" function, i.e. a function that takes a vector x and returns a character vector of length(x) giving a label for each input value.

Labelling functions are designed to be used with the labels argument of ggplot2 scales. The examples demonstrate their use with x scales, but they work similarly for all scales, including those that generate legends rather than axes.

Old interface

dollar() and format_dollar() are retired; please use label_dollar() instead.

See Also

Examples

demo_continuous(c(0, 1), labels = label_dollar())
demo_continuous(c(1, 100), labels = label_dollar())

# Customise currency display with prefix and suffix
demo_continuous(c(1, 100), labels = label_dollar(prefix = "USD "))
euro <- dollar_format(
  prefix = "",
  suffix = "\u20ac",
  big.mark = ".",
  decimal.mark = ","
)
demo_continuous(c(1000, 1100), labels = euro)

# Use negative_parens = TRUE for finance style display
demo_continuous(c(-100, 100), labels = label_dollar(negative_parens = TRUE))

scales

Scale Functions for Visualization

v1.1.1
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre], Dana Seidel [aut], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.