Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add figure space as style_positive option #366

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading