ggplot2
ggplot2 copied to clipboard
WIP: `n.breaks` are supplied to function-breaks.
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. https://github.com/tidyverse/ggplot2/issues/5972#issuecomment-2206917187. 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.
https://github.com/tidyverse/ggplot2/blob/2610840f7478af027294db0793df839199b8cb6b/R/scale-.R#L1287-L1291
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:
https://github.com/tidyverse/ggplot2/blob/2610840f7478af027294db0793df839199b8cb6b/R/scale-binned.R#L28
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.