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

Adhere more strictly to bins argument in stat_bin() #5891

Merged
merged 6 commits into from
May 20, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5882, fix #5036 and fix #5890.

Briefly, the default boundary was sometimes imputed such that bins was not respected.
This PR fixes that by setting the default boundary half a bin below the minimum.

The reason why this value was chosen is utilitarian rather than having good theoretical justification.
It only affects the arm of the binning code that deals with unspecified binwidth or breaks arguments.
As #5890 had adjacent code, I decided to fix that here as well.

The relevant reprexes:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

# From #5882 (had 2 bins)
p <- ggplot(palmerpenguins::penguins, aes(x= body_mass_g)) +
  geom_histogram(bins = 3)
nrow(layer_data(p))
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_bin()`).
#> [1] 3

# From #5036 (had 10 bins)
p <- ggplot(ggplot2movies::movies, aes(rating)) + 
  geom_histogram(bins = 10)
nrow(layer_data(p))
#> [1] 10

# From #5036 (had 9 bins)
p <- ggplot(ggplot2movies::movies, aes(rating)) + 
  geom_histogram(bins = 10, boundary = 0)
nrow(layer_data(p))
#> [1] 10

# From #5890
ggplot(mpg, aes(displ)) +
  geom_histogram(bins = 1, center = 0)

Created on 2024-05-13 with reprex v2.1.0

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.

LGTM

@teunbrand teunbrand merged commit c9a2d6a into tidyverse:main May 20, 2024
11 checks passed
@teunbrand teunbrand deleted the bin_numbers branch May 20, 2024 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants