Skip to content

geom_histogram produces wrong number of bins in special cases #5882

@qli84

Description

@qli84

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions