Skip to content

fix a bug in ScaleBinned$get_breaks() - #3575

Merged
thomasp85 merged 3 commits into
tidyverse:masterfrom
microly:microly-ScaleBinned-patch-1
Nov 14, 2019
Merged

thomasp85 merged 3 commits into
tidyverse:masterfrom
microly:microly-ScaleBinned-patch-1

Conversation

@microly

@microly microly commented Oct 18, 2019

Copy link
Copy Markdown
Contributor

this pull request try to fix the bug in issue #3574 .

I set n.breaks as the second parameter of breaks function.
if the n.breaks is not set by users, it will set to the default value 5, which is the same default as trans objects.

n.breaks can be set by end users.
So, after this fix, users can get meaningful breaks by set breaks as a function and get meaningful breaks using the values of limits and n.breaks.

@thomasp85

Copy link
Copy Markdown
Member

I think we'd need to check if the breaks function support two arguments before calling it with n.breaks, as the assumptions up until now has been that a breaks function only take a single argument

@microly

microly commented Oct 21, 2019

Copy link
Copy Markdown
Contributor Author

I think the breaks function with two arguments can work well.
The code block below is a simple example.

The brk function can be also used in other scales whose breaks function only need a single argument, because n.breaks has a default value.

df <- data.frame(
    x = runif(100),
    y = runif(100),
    z = rnorm(100)
)

brk <- function(limits, n.breaks = NULL) {
    n.breaks <- n.breaks %||% 5
    breaks <- seq(limits[1], limits[2], length.out = n.breaks + 2)
    breaks <- breaks[-c(1, length(breaks))]
}

library(ggplot2)

ggplot(df, aes(x, y)) +
    geom_point(aes(colour = z)) +
    scale_colour_stepsn(colours = terrain.colors(10), breaks = brk)

ggplot(df, aes(x, y)) + geom_point() + scale_x_continuous(breaks = brk)

@thomasp85

Copy link
Copy Markdown
Member

What I mean is that your change will mean that if you pass in a breaks function that only accepts a single argument it will fail. This runs counter to the expectations of the breaks function

@microly

microly commented Oct 21, 2019 •

Copy link
Copy Markdown
Contributor Author

oh, I know what you mean now~

maybe, we can handle it in some way like:

ggplot2/R/scale-.r

Lines 955 to 956 in 115c396

if (!is.null(self$n.breaks) && "n" %in% names(formals(self$trans$breaks))) {
breaks <- self$trans$breaks(limits, n = self$n.breaks)

@thomasp85

Copy link
Copy Markdown
Member

yeah, something like that

@microly

microly commented Oct 21, 2019

Copy link
Copy Markdown
Contributor Author

OK, thanks for your reply!
I learn very much from your codes and packages, thanks!

How can I finish the work? should I open a new pull request?

@clauswilke

Copy link
Copy Markdown
Member

@microly Just make additional changes to the code, commit, push, and the PR will update. Don't open a new PR.

@microly

microly commented Oct 21, 2019

Copy link
Copy Markdown
Contributor Author

@clauswilke
Thanks for your reply.
I am mot familiar with github, but I will try my best!

By the way, cowplot is one of the most important package in my daily work, thanks for your great
work!

update to check if the breaks function support two arguments before calling it with n.breaks.
@microly

microly commented Oct 24, 2019

Copy link
Copy Markdown
Contributor Author

@thomasp85
@clauswilke
I have made an update, please check it, thanks!

@thomasp85
thomasp85 merged commit 0c997f1 into tidyverse:master Nov 14, 2019
@thomasp85

Copy link
Copy Markdown
Member

Thanks!

@microly

microly commented Nov 16, 2019

Copy link
Copy Markdown
Contributor Author

thanks for your work!

@lock

lock Bot commented May 20, 2020

Copy link
Copy Markdown

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

1 similar comment
@lock

lock Bot commented May 20, 2020

Copy link
Copy Markdown

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock Bot locked and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants