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 signed option for negative numbers in label_log() #422

Merged
merged 7 commits into from
Oct 21, 2024

Conversation

teunbrand
Copy link
Contributor

This PR aims to fix #421.

Briefly, is adds a signed argument controlling wether + or - prefixes should be used.
By default, this is applied when negative numbers or zeroes are present in the numbers to label.

Reprex from the issue:

devtools::load_all("~/packages/scales/")
#> ℹ Loading scales

demo_continuous(
  c(-100, 100),
  transform = "asinh",
  breaks = c(-100, -10, -1, 0, 1, 10, 100),
  labels = label_log()
)
#> scale_x_continuous(transform = "asinh", breaks = c(-100, -10, 
#>     -1, 0, 1, 10, 100), labels = label_log())

Created on 2024-04-08 with reprex v2.1.0

@teunbrand
Copy link
Contributor Author

I took out the formatter for purposes of fixing #445.
The formatter works like this:

devtools::load_all("~/packages/scales/")
#> ℹ Loading scales
format_log(c(-10, -1, -0.1, 0, 0.1, 1, 10))
#> [1] "-10^ 1" "-10^ 0" "-10^-1" "0"      "+10^-1" "+10^ 0" "+10^ 1"

Which allows you to format geom_text(parse = TRUE):

library(ggplot2)
df <- data.frame(a = 1:5, b = 10^(1:5))
ggplot(df, aes(a, b)) +
  geom_point() +
  geom_text(
    aes(label = format_log(b)),
    parse = TRUE, vjust = -0.5
  )

Created on 2024-10-04 with reprex v2.1.1

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thomasp85 thomasp85 merged commit 9bcb182 into r-lib:main Oct 21, 2024
14 checks passed
@teunbrand teunbrand deleted the negative_label_log branch October 21, 2024 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: label_log() but for negative numbers
2 participants