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

breaks argument in stat_contour() accepts function. #2320

Closed
wants to merge 4 commits into from
Closed

breaks argument in stat_contour() accepts function. #2320

wants to merge 4 commits into from

Conversation

eliocamp
Copy link
Contributor

@eliocamp eliocamp commented Nov 1, 2017

Extended the breaks argument of stat_contour() to accept a function (similar to breaks in scale_x_continuous()).

Also, created breaks_default() which is the default function for creating breaks. The rational being to give the user easy access to the default behaviour so they can use it as the starting point for a new breaks function or small modifications. For example:

library(ggplot2)

v <- ggplot(faithfuld, aes(waiting, eruptions, z = density))
v + geom_contour(binwidth = 0.001)

my_breaks <- function(range) {
  b <- breaks_default(binwidth = 0.001, NULL)(range)
  b[b != 0.002]
}
v + geom_contour(breaks = my_breaks)

Also added documentation to bins and binwidth.

@eliocamp
Copy link
Contributor Author

eliocamp commented Nov 2, 2017

This is workable as is, but I'm still arguing with myself whether to pass only the range of the data or also bins and binwidth to the function in breaks.

@hadley
Copy link
Member

hadley commented Nov 2, 2017

I think it should pass binwidth. That would effectively make breaks = fullseq the default behaviour (but I don't think you should actually make that the default value)

@eliocamp
Copy link
Contributor Author

eliocamp commented Nov 2, 2017

Using fullseq as de default wouldn't break backwards compatibility in some cases? Right now the default when no parameter is passed is pretty(range, 10).

@hadley
Copy link
Member

hadley commented Nov 2, 2017

I'm not saying you should change it; I'm saying that it would match the existing internal API.

R/stat-contour.r Outdated
@@ -31,26 +43,23 @@ stat_contour <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
StatContour <- ggproto("StatContour", Stat,
required_aes = c("x", "y", "z"),
default_aes = aes(order = ..level..),
required_aes = c("x", "y", "z"),
Copy link
Member

Choose a reason for hiding this comment

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

Can you please preserve the existing indentation?

R/stat-contour.r Outdated
compute_group = function(data, scales, bins = NULL, binwidth = NULL,
breaks = breaks_default,
complete = FALSE, na.rm = FALSE) {
# Check is.null(breaks) for backwards compatibility
Copy link
Member

Choose a reason for hiding this comment

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

I think we've gone off the rails a bit here because I wasn't clear exactly what I was thinking. I'd prefer not to make such a big change and instead of breaks is a function, call it like fullseq with the range of the data and the binwidth.

@eliocamp
Copy link
Contributor Author

Sorry about the indent. 😅

I hope this is better. I still think the previous default behaviour (when all arguments are NULL) doesn't translate easily to breaks() only accepting range and binwidth. In this version, if no bins or binwidth is specified, I calculate binwidth from the result of pretty().

Are you open to removing pretty() completely and just set bins to 10 as default and calculate binwidth from diff(data$z)/bins? It's less convoluted and consistent. It also would fix the (arguably) bug that geom_contour(bins = 10) has a different result that geom_contour() even thought both have the same number of bins.

@thomasp85
Copy link
Member

@clauswilke whenever you have the time, can you give this PR a look and see if it should be put in line for the next feature release (since you "own" contouring around here😄)

@clauswilke clauswilke self-assigned this Mar 26, 2021
@clauswilke
Copy link
Member

Yes. I see this is a really old PR though, and the underlying code has changed a lot.

@eliocamp Are you willing to take this back up and make it workable with the current code base?

@jennybc jennybc deleted the branch tidyverse:master October 28, 2021 02:16
@jennybc jennybc closed this Oct 28, 2021
@thomasp85
Copy link
Member

Hi @eliocamp

This PR was closed as part of our default branch renaming efforts. Since the PR appeared to be stalled I'll keep it closed since it requires a bit of work to resurrect it.

If you feel like taking a second stab at this, please open a new PR (and make sure to update your ggplot2 fork to point to the new 'main' branch (using usethis::git_default_branch_rediscover())

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.

None yet

5 participants