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 7 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.

Copy link
Member

@thomasp85 thomasp85 left a 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?

R/scale-.R Outdated Show resolved Hide resolved
R/scale-.R Outdated
Comment on lines 735 to 751
if (identical(self$breaks, NA)) {
cli::cli_abort(
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.",
call = self$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.

We catch this case early in the scale constructor

Comment on lines +746 to -771
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 {
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.

Comment on lines -1008 to -1022
if (identical(self$breaks, NA)) {
cli::cli_abort(
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.",
call = self$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.

Also caught in constructor

Comment on lines -1232 to -1242
} else if (identical(self$breaks, NA)) {
cli::cli_abort(
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.",
call = self$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.

Also caught in constructor

Comment on lines +1389 to +1391
if (inherits(fun, "ggproto_method")) {
fun <- environment(fun)$f
}
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()

Comment on lines 173 to 178

# 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))
Copy link
Collaborator Author

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.

Comment on lines -436 to +446
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))
Copy link
Collaborator Author

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

@@ -717,6 +723,12 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
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.

@teunbrand
Copy link
Collaborator Author

I tried to elaborate somewhat more in the comments above. The TL;DR: we merge in the 'breaks are a function'-branch with 'breaks come from the transformation breaks function'-branch, simplifying the logic as we only need to deal with function-breaks once.

Merge branch 'main' into n_breaks_for_all

# Conflicts:
#	R/scale-.R
#	tests/testthat/test-scales-breaks-labels.R
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()
2 participants