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

geom_histogram produces wrong number of bins in special cases #5882

Closed
qli84 opened this issue May 3, 2024 · 2 comments · Fixed by #5891
Closed

geom_histogram produces wrong number of bins in special cases #5882

qli84 opened this issue May 3, 2024 · 2 comments · Fixed by #5891

Comments

@qli84
Copy link

qli84 commented May 3, 2024

With the following example, only 2 bins were drawn instead of specified 3.

library(palmerpenguins)
library(ggplot2)

ggplot(penguins, aes(x= body_mass_g)) +
  geom_histogram(bins=3)

Rplot

However, by addtionally specifying the center, number of bins is correct.

ggplot(penguins, aes(x= body_mass_g)) +
  geom_histogram(bins= 3, center =  2700)

Rplot01

Without specification of the center, this mistake happens exactly if $min \times(2\times bins + 1) = 3\times max$, where $min$ and $max$ is the minimum and maximum of the variable, respectively. This equation is equivalent to the situation where shift = 1 in the internal function bin_breaks_width.

@teunbrand
Copy link
Collaborator

Thanks for the report! Is this possibly the same issue as #5036?

@qli84
Copy link
Author

qli84 commented May 4, 2024

Thanks for the report! Is this possibly the same issue as #5036?

I think they are related. I compared issue #5036 with mine, both mistakes start at calculating shift in the function bin_breaks_width. In our examples, shift should be 0 so that the origin starts from the right place, but it gives 1.

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 a pull request may close this issue.

2 participants