Skip to content

Make bin guides compatible with discrete scales - #3703

Merged
thomasp85 merged 4 commits into
v3.3.0-rcfrom
bin-guide-discrete
Jan 8, 2020
Merged

thomasp85 merged 4 commits into
v3.3.0-rcfrom
bin-guide-discrete

Conversation

@thomasp85

Copy link
Copy Markdown
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
Copy Markdown
Member Author

Failure is just standard travis random failure

@clauswilke clauswilke left a comment

Copy link
Copy Markdown
Member

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?

Comment thread R/guide-bins.R
limits <- scale$get_limits()
all_breaks <- c(limits[1], breaks, limits[2])
bin_at <- all_breaks[-1] - diff(all_breaks) / 2
} else {

Copy link
Copy Markdown
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?

Comment thread R/guide-colorsteps.R
limits <- scale$get_limits()
all_breaks <- c(limits[1], breaks, limits[2])
bin_at <- all_breaks[-1] - diff(all_breaks) / 2
} else {

Copy link
Copy Markdown
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
Copy Markdown
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
@thomasp85
thomasp85 deleted the bin-guide-discrete branch October 6, 2025 11:58
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