How to Specify Histogram Breaks in R

Spread the love

Histograms are one of the most common graphical methods for understanding the distribution of a dataset. In R, the primary function used to generate a histogram is the hist() function. While R’s default settings often produce useful visualizations, many tasks require more finely-tuned specifications. One critical parameter that can significantly influence the interpretability of a histogram is the “breaks” parameter, which determines the boundaries of the bins that form the histogram. In this article, we will dive deep into understanding how to specify histogram breaks in R and how it influences our data visualization.

Basics of Histograms

Before proceeding, let’s quickly recap the concept of histograms. A histogram is a graphical representation of a variable’s distribution. It’s created by partitioning the range of the data into bins or intervals and then counting how many data points fall into each bin. The bins are usually specified as consecutive and non-overlapping intervals. Each bin is plotted as a bar, where the height of the bar corresponds to the frequency of data points that fall within the bin’s range.

Understanding Breaks

“Breaks” in the context of histograms refer to the points in between the bins that separate the data points into different groups. They define the start and the end of each bin. The number of breaks is always one more than the number of bins. For example, if there are five bins, there will be six break points.

The way we choose to specify these breaks can dramatically influence the final visualization. If bins are too wide, we may oversimplify the data and miss important details. If bins are too narrow, the histogram might be too noisy, making it difficult to interpret. Therefore, it’s essential to know how to specify the breaks properly.

The hist() Function and Breaks

The hist() function in R is designed to create histograms. The breaks argument of the hist() function allows us to specify the break points for our histogram. There are several ways to define the breaks, and the hist() function is flexible in this regard. Let’s take a look at some of these methods.

Using the Default Method

If we do not specify the breaks, R will automatically choose the number of bins using the Sturges’ algorithm. This algorithm is an attempt to balance the complexity and simplicity of the data representation. It defines the number of bins as 1 + log2(n), where n is the number of observations.Here’s an example:

# Generate 100 random normal values
data <- rnorm(100)

# Create a histogram

In this example, R will automatically calculate the number of bins.

Specifying Number of Bins or Breaks

We can manually specify the number of breaks as an integer. This integer will determine the number of bins in the histogram.

# Generate 100 random normal values
data <- rnorm(100)

# Create a histogram with 20 breaks
hist(data, breaks = 20)

This code will create a histogram with 20 bins (or 19 breaks). The breaks will be evenly distributed across the range of the data.

Providing a Vector of Break Points

The breaks argument can also accept a vector of break points. This gives you total control over the bin ranges.

# Generate 100 random normal values
data <- rnorm(100)

# Create a histogram with specific breaks
hist(data, breaks = c(-3, -2, -1, 0, 1, 2, 3))

This code will create a histogram with bins that are defined by the vector of break points. There will be a bin for each interval (-3 to -2, -2 to -1, etc).

Using a Break Algorithm

Several algorithms can calculate a “good” number of breaks based on the data. The hist() function accepts strings to specify these. The options are “Sturges”, “Scott”, “FD”, or a function that takes a single argument and returns a break computation.

# Generate 100 random normal values
data <- rnorm(100)

# Create a histogram using Scott's method
hist(data, breaks = "Scott")

This code will compute breaks using Scott’s method. Each method makes different assumptions about the data, so some methods may be more appropriate for certain data distributions than others.

Factors Influencing the Choice of Breaks

Selecting the right number of breaks or bins is not an exact science. The choice often depends on the nature and distribution of the data, as well as the purpose of the analysis. Here are a few factors to consider:

  1. Data distribution: Different data distributions may require different numbers of bins to accurately represent the data. For skewed data, more bins may be needed on one side of the distribution than the other.
  2. Data size: Larger datasets often require more bins to adequately represent the data. The default method in R uses Sturges’ method, which takes the data size into account, but other methods may be more appropriate for very large datasets.
  3. Analysis purpose: The number of bins can also be influenced by the purpose of the analysis. If the goal is to identify overall patterns, fewer bins might suffice. If the goal is to identify outliers or nuances in the data, more bins might be needed.


Creating effective histograms is a critical skill in data analysis and visualization. Properly specifying histogram breaks in R can significantly impact the final visualization, potentially revealing more insights about the data. It’s essential to remember that the “right” number of bins or breaks depends on the data and the purpose of the analysis. Therefore, data scientists should carefully consider their choice of breaks when creating histograms.

Posted in RTagged

Leave a Reply