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

label_number() argument to always print number with sign #262

Closed
davidchall opened this issue Mar 26, 2020 · 2 comments · Fixed by #334
Closed

label_number() argument to always print number with sign #262

davidchall opened this issue Mar 26, 2020 · 2 comments · Fixed by #334
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!
Milestone

Comments

@davidchall
Copy link
Contributor

davidchall commented Mar 26, 2020

Currently, label_number() only prints negative numbers with a sign. I'm requesting an argument to force a + symbol before positive numbers.

> scales::label_number()(seq(-1, 1, 0.5))
[1] "-1.00" "-0.50" "0.00"  "0.50"  "1.00"

The base R format() method doesn't provide an argument for this, so it's not currently possible to achieve this with dot args. Probably it would need to be added in post-processing between these lines (i.e. before adding a prefix):

scales/R/label-number.r

Lines 154 to 163 in 1e2c918

ret <- format(
scale * x,
big.mark = big.mark,
decimal.mark = decimal.mark,
trim = trim,
nsmall = nsmall,
scientific = FALSE,
...
)
ret <- paste0(prefix, ret, suffix)

One open question is whether zero should get +, - or nothing? The sprintf() function suggests that we should use +. This is also consistent with Python and C.

So I think I'm suggesting something like:

ret <- ifelse(x >= 0, paste0("+", ret), ret)
@hadley hadley added the feature a feature request or enhancement label Apr 3, 2020
@hadley hadley added the help wanted ❤️ we'd love your help! label May 7, 2020
@dirkschumacher
Copy link

dirkschumacher commented May 12, 2020

Happy to help here and send the PR. I see at least two options.

  1. Add a new parameter e.g. show_plus_sign = FALSE that controls the plus sign
scales::label_number(show_plus_sign = TRUE)(seq(-1, 1, 0.5))
  1. Allow suffix and prefix to be either a character or function taking the original vector values . Then one can build such a formatter outside the package. In addition scales could provide a prefix function that adds a + in front of a positive number.
plus_sign <- function(x) ifelse(x >= 0, "+", "")
scales::label_number(prefix = plus_sign)(seq(-1, 1, 0.5))

Both options would be backwards compatible. Any other ideas or preferences?

@davidchall
Copy link
Contributor Author

Hi @dirkschumacher - thanks for your offer of help!

I think these are both good options. I have a strong preference for (1), because showing a plus sign is quite a universal concept (i.e. independent of the prefix or suffix). But I'd definitely like to get feedback from the package maintainers before opening a PR.

As is often the case, naming this new argument is probably the most difficult bit. Here are some ideas:

  • show_plus_sign
  • always_sign (following documentation of sprintf(): "Always print number with sign: by default only negative numbers are printed with a sign.")
  • both_sign

dkahle added a commit to dkahle/scales that referenced this issue Jun 18, 2020
@hadley hadley added this to the v1.2.0 milestone Mar 19, 2022
hadley added a commit that referenced this issue Mar 21, 2022
hadley added a commit that referenced this issue Mar 24, 2022
* `style_positive = "plus` fixes #262..
* `style_negative = "minus" fixes #249.
* `style_negative = "parens"` deprecates `negative_parens` argument to `label_dollar()`
* `prefix` now come comes between `-` and `value` for negative values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants