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

Scale parameters #6196

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Scale parameters #6196

wants to merge 10 commits into from

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Nov 22, 2024

This PR follows up on #4271 and #5444, aiming to fix #4269.

Like before, it aims to provide the convenience of setting some scale parameters, without comitting to a full scale yet.
Unlike #5444, this PR is closer to Claus' approach in #4271.

This PR the scale_params() function that populates a new ScalesList$params field. When a new scale is added, it will get updated with the parameters in that field. The update mechanism required the most attention. Every <Scale> class now has a Scale$new() method that takes on most of the work that continuous_scale(), discrete_scale(), binned_scale() constructors were doing before. The Scale$update() method is a thin wrapper around this and should resolve gnarly situations (e.g. limit transformation).

In the demo below we can note:

  • the y-limits are appropriately transformed by the sqrt transformation. If we were to use the transform first and then the limits, the result would be the same.
  • We can set parameters for multiple scales at once, like colour/shape.
devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
p <- ggplot(mpg, aes(displ, hwy, colour = drv, shape = factor(year))) +
  geom_point()

p +
  scale_params("x", breaks = c(2, 4, 7)) +
  scale_params("y", limits = c(0, 50)) +
  scale_params("y", transform = "sqrt") +
  scale_params(c("colour", "shape"), guide = guide_legend(ncol = 2))

The update mechanism should also recognise invalid updates:

p + scale_params("colour", foobar = "i dont exist")
#> Error in `geom_point()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `scale_colour_discrete()`:
#> ! Cannot update scale with the unknown argument:
#> • "foobar"

Created on 2024-11-22 with reprex v2.1.1

@teunbrand teunbrand changed the title Scale experiment Scale parameters Nov 22, 2024
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.

Set common scale parameters outside of the scale function
1 participant