-
Notifications
You must be signed in to change notification settings - Fork 2.1k
n.breaks are supplied to function-breaks.
#5974
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
Conversation
| if (identical(breaks, NA)) { | ||
| cli::cli_abort( | ||
| "Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.", | ||
| call = call | ||
| ) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an adjacent change that seemed like a good idea because we can now omit this check in the get_breaks() calculation. It is the reason why the unit tests had to be adapted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are deleting an awful lot of logic without adding in an equivalent amount... can you walk me through it?
| if (identical(self$breaks, NA)) { | ||
| cli::cli_abort( | ||
| "Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.", | ||
| call = self$call | ||
| ) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We catch this case early in the scale constructor
| if (is.function(breaks)) { | ||
| # Limits in transformed space need to be converted back to data space | ||
| limits <- transformation$inverse(limits) | ||
| if (!is.null(self$n.breaks) && support_nbreaks(breaks)) { | ||
| breaks <- breaks(limits, n = self$n.breaks) | ||
| } else { | ||
| breaks <- breaks(limits) | ||
| if (!is.null(self$n.breaks)) { | ||
| cli::cli_warn( | ||
| "Ignoring {.arg n.breaks}. Use a {.cls transform} object that supports setting number of breaks.", | ||
| "Ignoring {.arg n.breaks}. Use a {.cls transform} object or \\ | ||
| {.arg breaks} function that supports setting number of breaks", | ||
| call = self$call | ||
| ) | ||
| } | ||
| breaks <- transformation$breaks(limits) | ||
| } | ||
| } else if (is.function(self$breaks)) { | ||
| breaks <- self$breaks(limits) | ||
| } else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need separate logic for the default breaks (breaks function(!) from transformation) and function-breaks, as both now access the n.breaks logic.
| if (identical(self$breaks, NA)) { | ||
| cli::cli_abort( | ||
| "Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.", | ||
| call = self$call | ||
| ) | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also caught in constructor
| } else if (identical(self$breaks, NA)) { | ||
| cli::cli_abort( | ||
| "Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.", | ||
| call = self$call | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also caught in constructor
| if (inherits(fun, "ggproto_method")) { | ||
| fun <- environment(fun)$f | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might need to deal with ggproto methods as self$breaks can be a function that gets wrapped by ggproto()
|
|
||
| # X | ||
| sxc <- scale_x_continuous(breaks = NA) | ||
| sxc$train(1:3) | ||
| expect_error(sxc$get_breaks()) | ||
| expect_error(sxc$get_breaks_minor()) | ||
|
|
||
| # Y | ||
| syc <- scale_y_continuous(breaks = NA) | ||
| syc$train(1:3) | ||
| expect_error(syc$get_breaks()) | ||
| expect_error(syc$get_breaks_minor()) | ||
|
|
||
| # Alpha | ||
| sac <- scale_alpha_continuous(breaks = NA) | ||
| sac$train(1:3) | ||
| expect_error(sac$get_breaks()) | ||
|
|
||
| # Size | ||
| ssc <- scale_size_continuous(breaks = NA) | ||
| ssc$train(1:3) | ||
| expect_error(ssc$get_breaks()) | ||
|
|
||
| # Fill | ||
| sfc <- scale_fill_continuous(breaks = NA) | ||
| sfc$train(1:3) | ||
| expect_error(sfc$get_breaks()) | ||
|
|
||
| # Colour | ||
| scc <- scale_colour_continuous(breaks = NA) | ||
| scc$train(1:3) | ||
| expect_error(scc$get_breaks()) | ||
| expect_error(scale_x_continuous(breaks = NA)) | ||
| expect_error(scale_y_continuous(breaks = NA)) | ||
| expect_error(scale_alpha_continuous(breaks = NA)) | ||
| expect_error(scale_size_continuous(breaks = NA)) | ||
| expect_error(scale_fill_continuous(breaks = NA)) | ||
| expect_error(scale_colour_continuous(breaks = NA)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests can all be simplified as the constructor now calls out NA-breaks.
| expect_snapshot_error(ggplot_build(p)) | ||
| expect_snapshot_error(scale_x_continuous(breaks = NA)) | ||
| p <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + scale_x_continuous(minor_breaks = NA) | ||
| expect_snapshot_error(ggplot_build(p)) | ||
| p <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + scale_x_continuous(labels = NA) | ||
| expect_snapshot_error(ggplotGrob(p)) | ||
| p <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + scale_x_continuous(labels = function(x) 1:2) | ||
| expect_snapshot_error(ggplotGrob(p)) | ||
| p <- ggplot(mtcars) + geom_bar(aes(factor(gear))) + scale_x_discrete(breaks = NA) | ||
| expect_snapshot_error(ggplot_build(p)) | ||
| expect_snapshot_error(scale_x_discrete(breaks = NA)) | ||
| p <- ggplot(mtcars) + geom_bar(aes(factor(gear))) + scale_x_discrete(labels = NA) | ||
| expect_snapshot_error(ggplotGrob(p)) | ||
|
|
||
| p <- ggplot(mtcars) + geom_bar(aes(mpg)) + scale_x_binned(breaks = NA) | ||
| expect_snapshot_error(ggplot_build(p)) | ||
| expect_snapshot_error(scale_x_binned(breaks = NA)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again simplification due to breaks = NA being caught in the constructor
| return(numeric()) | ||
| } | ||
| transformation <- self$get_transformation() | ||
| breaks <- self$breaks %|W|% transformation$breaks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like to think of this line as the 'spirit of this PR'. We no longer need to treat function-breaks and default breaks any differently.
|
I tried to elaborate somewhat more in the comments above. The TL;DR: we merge in the ' |
Merge branch 'main' into n_breaks_for_all # Conflicts: # R/scale-.R # tests/testthat/test-scales-breaks-labels.R
Merge branch 'main' into n_breaks_for_all # Conflicts: # R/scale-.R
This is now solved by allowing both forms, which makes this PR ready for review. |
n.breaks are supplied to function-breaks.n.breaks are supplied to function-breaks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This PR aims to fix #5972.
Briefly, it ensures that function-breaks are called with the
n = n.breaksargument when supported, not just breaks derived from the transformation.Example:
Created on 2024-07-03 with reprex v2.1.0
I consider this PR a WIP because I'd also like to treat binned scales, e.g. #5972 (comment). There are a few things that complicate this.
First, this has sort-of already been done in #3575, except instead of
n, then.breaksargument is used.ggplot2/R/scale-.R
Lines 1287 to 1291 in 2610840
I'm not sure how widespread an
n.breaksargument is, but ideally we should only supportnorn.breaksand not both. However, for backwards compatibility reasons, we might have to support both.Secondly,
scale_x/y_binned()already have a defaultn.breaksargument:ggplot2/R/scale-binned.R
Line 28 in 2610840
This would interfere with the strategy, because it would override
nwhen providingbreaks = scales::extended_breaks(n = 20)so that the internal function is called withn = 10. We could amend this by havingn.breaks = 10be an internal default fortransformation$breaks()only.In any case, I'd be happy to be advised on the case of binned scales.