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

WIP: n.breaks are supplied to function-breaks. #5974

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

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5972.

Briefly, it ensures that function-breaks are called with the n = n.breaks argument when supported, not just breaks derived from the transformation.

Example:

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

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  scale_y_continuous(
    breaks = scales::breaks_extended(),
    n.breaks = 20
  )

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, the n.breaks argument is used.

ggplot2/R/scale-.R

Lines 1287 to 1291 in 2610840

} else if (is.function(self$breaks)) {
if ("n.breaks" %in% names(formals(environment(self$breaks)$f))) {
n.breaks <- self$n.breaks %||% 5 # same default as trans objects
breaks <- self$breaks(limits, n.breaks = n.breaks)
} else {

I'm not sure how widespread an n.breaks argument is, but ideally we should only support n or n.breaks and not both. However, for backwards compatibility reasons, we might have to support both.

Secondly, scale_x/y_binned() already have a default n.breaks argument:

scale_x_binned <- function(name = waiver(), n.breaks = 10, nice.breaks = TRUE,

This would interfere with the strategy, because it would override n when providing breaks = scales::extended_breaks(n = 20) so that the internal function is called with n = 10. We could amend this by having n.breaks = 10 be an internal default for transformation$breaks() only.

In any case, I'd be happy to be advised on the case of binned scales.

Comment on lines +610 to +615
if (identical(breaks, NA)) {
cli::cli_abort(
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.",
call = call
)
}
Copy link
Collaborator Author

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.

Comment on lines +742 to +748
if (zero_range(as.numeric(limits))) {
return(limits[1])
}

if (is.function(breaks)) {
# Limits in transformed space need to be converted back to data space
limits <- transformation$inverse(limits)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

realised we only needed the original limits for function-breaks, so zero-range can early-exit.

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.

n.breaks only supported when breaks = waiver()
1 participant