Skip to content

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

Merged
merged 12 commits into from
May 19, 2025
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* In continuous scales, when `breaks` is a function and `n.breaks` is set, the
`n.breaks` will be passed to the `breaks` function. Previously, `n.breaks`
only applied to the default break calculation (@teunbrand, #5972)
* (internal) New `Facet$draw_panel_content()` method for delegating panel
assembly (@Yunuuuu, #6406).
* Facet gains a new method `setup_panel_params` to interact with the
Expand Down
85 changes: 40 additions & 45 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,12 @@
if (is.null(breaks) || is.null(labels)) {
return(invisible())
}
if (identical(breaks, NA)) {
cli::cli_abort(
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.",
call = call
)
}
Comment on lines +643 to +648
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.


bad_labels <- is.atomic(breaks) && is.atomic(labels) &&
length(breaks) != length(labels)
Expand Down Expand Up @@ -751,48 +757,38 @@
return(numeric())
}
transformation <- self$get_transformation()
breaks <- self$breaks %|W|% transformation$breaks
Copy link
Collaborator Author

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.


if (is.null(breaks)) {
return(NULL)
}

# Ensure limits don't exceed domain (#980)
domain <- suppressWarnings(transformation$transform(transformation$domain))
domain <- sort(domain)
# To avoid NaN causing issues. NaN are dropped by the sort()
if (length(domain) == 2 && !zero_range(domain)) {
limits <- oob_squish(limits, domain)
}

# Limits in transformed space need to be converted back to data space
limits <- transformation$inverse(limits)

if (is.null(self$breaks)) {
return(NULL)
}

if (identical(self$breaks, NA)) {
cli::cli_abort(
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.",
call = self$call
)
if (zero_range(as.numeric(limits))) {
return(limits[1])
}

# Compute `zero_range()` in transformed space in case `limits` in data space
# don't support conversion to numeric (#5304)
if (zero_range(as.numeric(transformation$transform(limits)))) {
breaks <- limits[1]
} else if (is.waiver(self$breaks)) {
if (!is.null(self$n.breaks) && trans_support_nbreaks(transformation)) {
breaks <- transformation$breaks(limits, self$n.breaks)
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 {
Comment on lines +777 to -794
Copy link
Collaborator Author

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.

breaks <- self$breaks
}

# Breaks in data space need to be converted back to transformed space
Expand Down Expand Up @@ -1046,13 +1042,6 @@
return(NULL)
}

if (identical(self$breaks, NA)) {
cli::cli_abort(
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.",
call = self$call
)
}

Comment on lines -1049 to -1055
Copy link
Collaborator Author

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 (is.waiver(self$breaks)) {
breaks <- limits
} else if (is.function(self$breaks)) {
Expand Down Expand Up @@ -1268,14 +1257,9 @@

if (is.null(self$breaks)) {
return(NULL)
} else if (identical(self$breaks, NA)) {
cli::cli_abort(
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.",
call = self$call
)
Comment on lines -1271 to -1275
Copy link
Collaborator Author

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 (is.waiver(self$breaks)) {
if (self$nice.breaks) {
if (!is.null(self$n.breaks) && trans_support_nbreaks(transformation)) {
if (!is.null(self$n.breaks) && support_nbreaks(transformation$breaks)) {
breaks <- transformation$breaks(limits, n = self$n.breaks)
} else {
if (!is.null(self$n.breaks)) {
Expand Down Expand Up @@ -1332,9 +1316,16 @@
}
}
} else if (is.function(self$breaks)) {
if ("n.breaks" %in% names(formals(environment(self$breaks)$f))) {
fmls <- names(formals(environment(self$breaks)$f))
if (any(c("n", "n.breaks") %in% fmls)) {
n.breaks <- self$n.breaks %||% 5 # same default as trans objects
breaks <- self$breaks(limits, n.breaks = n.breaks)
# TODO: we should only allow `n` argument and not `n.breaks` to be
# consistent with other scales. We should start deprecation at some point.
if ("n.breaks" %in% fmls) {
breaks <- self$breaks(limits, n.breaks = n.breaks)
} else {
breaks <- self$breaks(limits, n = n.breaks)
}
} else {
if (!is.null(self$n.breaks)) {
cli::cli_warn(
Expand Down Expand Up @@ -1439,6 +1430,14 @@
cli::cli_warn(msg, call = call)
}


support_nbreaks <- function(fun) {
if (inherits(fun, "ggproto_method")) {
fun <- environment(fun)$f

Check warning on line 1436 in R/scale-.R

View check run for this annotation

Codecov / codecov/patch

R/scale-.R#L1436

Added line #L1436 was not covered by tests
}
Comment on lines +1435 to +1437
Copy link
Collaborator Author

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()

"n" %in% fn_fmls_names(fun)
}

check_continuous_limits <- function(limits, ...,
arg = caller_arg(limits),
call = caller_env()) {
Expand All @@ -1449,10 +1448,6 @@
check_length(limits, 2L, arg = arg, call = call)
}

trans_support_nbreaks <- function(trans) {
"n" %in% names(formals(trans$breaks))
}

allow_lambda <- function(x) {
if (is_formula(x)) as_function(x) else x
}
30 changes: 18 additions & 12 deletions tests/testthat/_snaps/patterns/pattern-fills-no-alpha.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading