tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

geom_histogram produces wrong number of bins in special cases #5882

Closed qli84 closed 1 month ago

qli84 commented 2 months ago

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 commented 2 months ago

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

qli84 commented 2 months ago

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.