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

scales::rescale* functions cannot use \(x) x notation #388

Closed
davidhodge931 opened this issue Jun 18, 2023 · 3 comments
Closed

scales::rescale* functions cannot use \(x) x notation #388

davidhodge931 opened this issue Jun 18, 2023 · 3 comments

Comments

@davidhodge931
Copy link

I assume this is an error with scales::rescale* functions and not ggplot2.

library(tidyverse)

#this works using ~ .x notation
ggplot(mpg, aes(displ, hwy, colour = cty - mean(cty))) +
  geom_point() +
  labs(
    x = "Engine displacement [L]",
    y = "Highway miles per gallon",
    colour = "Centered\nvalue"
  ) +
  scale_colour_stepsn(
    colours = c("blue", "white", "red"), 
    rescaler = ~scales::rescale_mid(.x, mid = 0), 
    breaks = scales::breaks_width(5),
  )

#this doesn't work using \(x) notation
ggplot(mpg, aes(displ, hwy, colour = cty - mean(cty))) +
  geom_point() +
  labs(
    x = "Engine displacement [L]",
    y = "Highway miles per gallon",
    colour = "Centered\nvalue"
  ) +
  scale_colour_stepsn(
    colours = c("blue", "white", "red"), 
    rescaler = \(x) scales::rescale_mid(x, mid = 0), 
    breaks = scales::breaks_width(5),
  )
#> Error in rescaler(...): unused argument (from = c(-7.85897435897436, 18.1410256410256))

Created on 2023-06-18 with reprex v2.0.2

@davidhodge931
Copy link
Author

davidhodge931 commented Jun 18, 2023

  library(tidyverse)
#this also doesn't work using function(x) notation
ggplot(mpg, aes(displ, hwy, colour = cty - mean(cty))) +
  geom_point() +
  labs(
    x = "Engine displacement [L]",
    y = "Highway miles per gallon",
    colour = "Centered\nvalue"
  ) +
  scale_colour_stepsn(
    colours = c("blue", "white", "red"), 
    rescaler = function(x) scales::rescale_mid(x, mid = 0), 
    breaks = scales::breaks_width(5),
  )
#> Error in rescaler(...): unused argument (from = c(-7.85897435897436, 18.1410256410256))

Created on 2023-06-18 with reprex v2.0.2

@teunbrand
Copy link
Contributor

The reason \(x) and function(x) anonymous functions don't work, is because there are multiple arguments passed to the rescaler function: the values and the limits (iirc).

@davidhodge931 davidhodge931 changed the title scales::rescale* functions cannot use \(x) x notation scales::rescale* functions cannot use \(x) x notation Jul 22, 2023
@hadley
Copy link
Member

hadley commented Oct 31, 2023

Yeah, this is nothing to do with anonymous function syntax and everything to do with expected arguments.

@hadley hadley closed this as completed Oct 31, 2023
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

3 participants