Skip to content

Commit

Permalink
Add figure space as style_positive option (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwiernik authored Nov 6, 2023
1 parent 4327756 commit 33d0e97
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
(@teunbrand, #403).
* The `scale_cut` argument in `number()` now works as advertised for values
below the lowest cut value (#346)
* Added a new option to the `style_positive` argument in `label_*()` functions.
Setting this to `"space"` will add a figure space in front of the number to
make it easier to align positive and negative values as figure space takes up
the same amount of space as `-` (#366)
* `label_dollar()` has been superseeded by `label_currency()` for clarity (#344)
* `sqrt_trans()` no longer returns an inverse for values outside of its domain
(#214)
Expand Down
10 changes: 8 additions & 2 deletions R/label-number.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#'
#' * `"none"` (the default): no change, e.g. `1`.
#' * `"plus"`: preceded by `+`, e.g. `+1`.
#' * `"space"`: preceded by a Unicode "figure space", i.e., a space equally
#' as wide as a number or `+`. Compared to `"none"`, adding a figure space
#' can ensure numbers remain properly aligned when they are left- or
#' right-justified.
#' @param style_negative A string that determines the style of negative numbers:
#'
#' * `"hyphen"` (the default): preceded by a standard hypen `-`, e.g. `-1`.
Expand Down Expand Up @@ -103,7 +107,7 @@
#' demo_continuous(c(0, 100), labels = label_number(suffix = "\u00b0C"))
label_number <- function(accuracy = NULL, scale = 1, prefix = "",
suffix = "", big.mark = " ", decimal.mark = ".",
style_positive = c("none", "plus"),
style_positive = c("none", "plus", "space"),
style_negative = c("hyphen", "minus", "parens"),
scale_cut = NULL,
trim = TRUE, ...) {
Expand Down Expand Up @@ -219,7 +223,7 @@ comma_format <- label_comma
#' @return A character vector of `length(x)`.
number <- function(x, accuracy = NULL, scale = 1, prefix = "",
suffix = "", big.mark = " ", decimal.mark = ".",
style_positive = c("none", "plus"),
style_positive = c("none", "plus", "space"),
style_negative = c("hyphen", "minus", "parens"),
scale_cut = NULL,
trim = TRUE, ...) {
Expand Down Expand Up @@ -280,6 +284,8 @@ number <- function(x, accuracy = NULL, scale = 1, prefix = "",
}
if (style_positive == "plus") {
ret[sign > 0] <- paste0("+", ret[sign > 0])
} else if (style_positive == "space") {
ret[sign > 0] <- paste0("\u2007", ret[sign > 0])
}

# restore NAs from input vector
Expand Down
6 changes: 5 additions & 1 deletion man/comma.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/label_number.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/number.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 33d0e97

Please sign in to comment.