The true minus sign (Unicode 2212) -- neither an em dash, nor an en dash, nor the usual hyphen-minus -- is highly underrated. It makes everything look better!

signs(
  x,
  ...,
  format = getOption("signs.format", scales::number),
  add_plusses = getOption("signs.add.plusses", FALSE),
  trim_leading_zeros = getOption("signs.trim.leading.zeros", FALSE),
  label_at_zero = getOption("signs.label.at.zero", "none")
)

Arguments

x

Numeric vector.

...

Other arguments passed on to format.

format

Any function that takes a numeric vector and returns a character vector, such as scales::number, scales::comma, or scales::percent (all of which are documented at number_format).

add_plusses

Logical. Should positive values have plus signs?

trim_leading_zeros

Logical. Should signs trim leading zeros from values of x between -1 and 1?

label_at_zero

Character. What should be returned when x = 0? Options "none" (no change), "blank" (a zero-length string), or "symbol" (add a plus-minus symbol).

Value

A UTF-8 character vector

Details

add_plusses, trim_leading_zeros, and label_at_zero are offered for convenience.

The options signs.format, signs.add.plusses, signs.trim.leading.zeros, and signs.label.at.zero are set when the package is loaded to scales::number, FALSE, FALSE, and "none", respectively. If the package is not loaded and the these options are not otherwise set, signs will use those defaults.

label_at_zero is applied after format; that is, if it is "blank" and you've specified an accuracy of 0.1, -0.04 will show as blank.

Examples

x <- seq(-5, 5) scales::number(x)
#> [1] "-5.0" "-4.0" "-3.0" "-2.0" "-1.0" "0.0" "1.0" "2.0" "3.0" "4.0" #> [11] "5.0"
signs(x)
#> [1] "−5.0" "−4.0" "−3.0" "−2.0" "−1.0" "0.0" "1.0" "2.0" "3.0" "4.0" #> [11] "5.0"
signs(x, accuracy = 1, scale = 1, format = scales::percent)
#> [1] "−5%" "−4%" "−3%" "−2%" "−1%" "0%" "1%" "2%" "3%" "4%" "5%"
signs(x, add_plusses = TRUE)
#> [1] "−5.0" "−4.0" "−3.0" "−2.0" "−1.0" "0.0" "+1.0" "+2.0" "+3.0" "+4.0" #> [11] "+5.0"
signs(x, add_plusses = TRUE, label_at_zero = "blank")
#> [1] "−5.0" "−4.0" "−3.0" "−2.0" "−1.0" "" "+1.0" "+2.0" "+3.0" "+4.0" #> [11] "+5.0"
signs(x, add_plusses = TRUE, label_at_zero = "symbol")
#> [1] "−5.0" "−4.0" "−3.0" "−2.0" "−1.0" "±0.0" "+1.0" "+2.0" "+3.0" "+4.0" #> [11] "+5.0"
signs(x, accuracy = .1, scale = .1, trim_leading_zeros = TRUE)
#> [1] "−.5" "−.4" "−.3" "−.2" "−.1" ".0" ".1" ".2" ".3" ".4" ".5"