Skip to content

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

@davidchall

Description

@davidchall

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementhelp wanted ❤️we'd love your help!

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions