Skip to content

Make bin guides compatible with discrete scales #3703

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 4 commits into from
Jan 8, 2020

Conversation

thomasp85
Copy link
Member

This PR makes the new bin guides work with discrete scales if the breaks are given in a cut() compliant format (e.g. (4, 19]).

This, most importantly, makes the bin guides work with geom_contour_filled() but also gives a venue for users to pre-bin their data and still benefit from the new guides.

Example:

library(ggplot2)

volcano_long <- data.frame(
  x = as.vector(col(volcano)),
  y = as.vector(row(volcano)),
  z = as.vector(volcano)
)

ggplot(volcano_long, aes(x, y, z = z)) + 
  geom_contour_filled(aes(fill = stat(level)), bins = 8) + 
  scale_fill_brewer()

ggplot(volcano_long, aes(x, y, z = z)) + 
  geom_contour_filled(aes(fill = stat(level)), bins = 8) + 
  scale_fill_brewer(guide = guide_colorsteps())

Created on 2020-01-08 by the reprex package (v0.3.0)

@thomasp85 thomasp85 requested a review from clauswilke January 8, 2020 10:50
@thomasp85 thomasp85 added this to the ggplot2 3.3.0 milestone Jan 8, 2020
@thomasp85
Copy link
Member Author

Failure is just standard travis random failure

Copy link
Member

@clauswilke clauswilke left a comment

Choose a reason for hiding this comment

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

The code looks good to me, but I think adding a few comments would be helpful. Also, should the feature of discrete breaks be mentioned somewhere in the docs?

limits <- scale$get_limits()
all_breaks <- c(limits[1], breaks, limits[2])
bin_at <- all_breaks[-1] - diff(all_breaks) / 2
} else {
Copy link
Member

Choose a reason for hiding this comment

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

The else branch is sufficiently complicated that a comments would be helpful. In particular, explain that when breaks is not numeric it's expected to be in the (<lower>, <upper>] format. I was very confused by the regex until I figured that out. Also, should this format be mentioned somewhere in the documentation?

limits <- scale$get_limits()
all_breaks <- c(limits[1], breaks, limits[2])
bin_at <- all_breaks[-1] - diff(all_breaks) / 2
} else {
Copy link
Member

Choose a reason for hiding this comment

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

See my previous comment. Add some explanation to the code here as well.

@thomasp85
Copy link
Member Author

You are probably right about everything. I'll add some comments and improve the docs

@thomasp85 thomasp85 merged commit 891ee88 into v3.3.0-rc Jan 8, 2020
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.

2 participants