How to set the number of bins for a histogram in ggplot2

In ggplot2, the number of bins for a histogram can be set by defining the argument breaks in the geom_histogram() function. This argument takes a vector of numbers or a single number, which specifies the number and size of the bins. The default is to use 30 bins. For example, breaks = 10 will create 10 bins with equal widths, while breaks = c(0, 10, 20) will create two bins of width 10.


You can use the bins argument to specify the number of bins to use in a histogram in :

library(ggplot2)

ggplot(df, aes(x=x)) +
  geom_histogram(bins=10)

The following examples show how to use this argument in practice.

Example: Set Number of Bins for Histogram in ggplot2

The following code shows how to create a dataset in R that contains 10,000 random values that follow a with a mean value of 2:

#make this example reproducible
set.seed(0)

#create data frame with 10,000 random values that follow Poisson distribution
df <- data.frame(values=rpois(n=10000, lambda=2))

#view first five rows of data frame
head(df)

  values
1      4
2      1
3      1
4      2
5      4
6      1

We can use the following code to create a histogram in ggplot2 to visualize the distribution of values in the data frame:

library(ggplot2)

ggplot(df, aes(x=values)) +
  geom_histogram(fill='steelblue', col='black')

By default, ggplot2 will automatically pick a certain number of bins to use in the histogram.

However, we can use the following syntax to specify that we want the histogram to use 10 bins:

library(ggplot2)

ggplot(df, aes(x=values)) +
  geom_histogram(fill='steelblue', col='black', bins=10)

Notice that the histogram now has exactly 10 bins.

Or we could use the following syntax to specify that we want the histogram to use 5 bins:

library(ggplot2)

ggplot(df, aes(x=values)) +
  geom_histogram(fill='steelblue', col='black', bins=5)

You’ll find that the fewer bins you use, the wider each bin will be.

In general, if you use too few bins then the true underlying distribution of values will be hidden.

However, if you use too many bins then you may just be visualizing the noise in the data.

One way to find the optimal number of bins to use in a histogram is by using Sturges’ Rule. Read more about that rule .

Note: You can find the complete documentation for the geom_histogram function .

The following tutorials explain how to create other common charts in R:

x