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_log() generated function outputs expr instead of a character vector as documented #445

Open
ppreshant opened this issue Jul 31, 2024 · 0 comments

Comments

@ppreshant
Copy link

ppreshant commented Jul 31, 2024

Hello! I am trying to use the label_ functions for plotting formatted numbers in geom_text() instead of the intended use in ggplot2 scales. But the output of label_log() is different from that of label_scientific() so I thought this could be improved upon to make consistent?

I had to use the deparse() function with a map_chr() call to make scale_log() work eventually..

Here's the outputs of the label_..() functions -

# returns expression (not string)
scales::label_log(digits = 1)(c(1, 10, 100))
#> expression(10^0, 10^1, 10^2)

# returns string vector as documented
scales::label_scientific(digits = 1)(c(1, 10, 100))
#> [1] "1e+00" "1e+01" "1e+02"

# showing the functional output
scales::label_log()
#> function (x) 
#> {
#>     if (length(x) == 0) {
#>         return(expression())
#>     }
#>     exponent <- format(log(x, base = base), digits = digits)
#>     text <- paste0(base, "^", exponent)
#>     ret <- parse_safe(text)
#>     ret[is.na(x)] <- NA
#>     ret
#> }
#> <bytecode: 0x0000021649f9b4b8>
#> <environment: 0x000002164d3b7290>

Created on 2024-07-31 with reprex v2.1.0

Here's an example code to make the geom_text() which only works with label_scientific()

library(tidyverse)

# make simple test data 3 columns, 5 rows
b <- tibble::tibble(b1 = 1:5, b2 = 10^b1)

# with label_scientific
ggplot2::ggplot(b, 
       aes(x = b1, y = b2)) + 
  geom_point() + 
  geom_text(aes(label = scales::label_scientific(digits = 1)(b2)), vjust = -2)

# with label_log()  
ggplot(b, 
       aes(x = b1, y = b2)) + 
  geom_point() + 
  geom_text(aes(label = scales::label_log()(b2)), vjust = -2)
#> Don't know how to automatically pick scale for object of type <expression>.
#> Defaulting to continuous.
#> Error in `geom_text()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 2nd layer.
#> Caused by error in `compute_aesthetics()`:
#> ! Aesthetics are not valid data columns.
#> ✖ The following aesthetics are invalid:
#> ✖ `label = (scales::label_log())(b2)`
#> ℹ Did you mistype the name of a data column or forget to add `after_stat()`?

# I can fix it by mapping a deparse() function
ggplot(b, 
       aes(x = b1, y = b2)) + 
  geom_point() + 
  geom_text(aes(label = scales::label_log()(b2) %>% 
                  {map_chr(as.list(.), deparse)}
                  ), 
            vjust = -2)

Created on 2024-07-31 with reprex v2.1.0

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

No branches or pull requests

1 participant