How to Create a Histogram in R

Spread the love

One of the most fundamental graphical representations in statistics is a histogram. This article will guide you on how to create a histogram in R.

What is a Histogram?

A histogram is a graphical representation of the distribution of a dataset. It is an estimate of the probability distribution of a continuous variable. To construct a histogram, the first step is to “bin” the range of values—that is, divide the entire range of values into a series of intervals—and then count how many values fall into each interval. The bins are usually specified as consecutive, non-overlapping intervals of a variable. The plotted values represent the data count in each bin, and the area of each bar is equal to the frequency of observations in the corresponding bin.

Creating a Simple Histogram in R

The simplest way to create a histogram in R is by using the built-in hist() function. Let’s create a histogram from a standard normal distribution.

# Generate 1000 random numbers from a standard normal distribution
data <- rnorm(1000)

# Create a histogram of the data

This code generates 1000 random numbers from a standard normal distribution (mean = 0, standard deviation = 1) and plots a histogram. By default, R chooses the number of bins with the Sturges algorithm, although this can be changed.

Adjusting the Number of Bins

If you’d like to adjust the number of bins, you can do so with the breaks argument in the hist() function. The breaks argument allows you to define the way R splits the data into bins. There are a few predefined methods like “Sturges”, “Scott”, and “FD”, or you can specify an integer that represents the number of bins to use.

# Create a histogram of the data with 50 bins
hist(data, breaks = 50)

Adding Main Title and Axis Labels

To add a main title and axis labels to your histogram, you can use the main, xlab, and ylab parameters.

     main = "Histogram of Randomly Generated Data", 
     xlab = "Generated Data", 
     ylab = "Frequency")

Changing the Color of the Bars and Border

The color of the bars and their borders can be changed using the col and border parameters.

     col = "lightblue", 
     border = "black")

Adding a Density Curve

One common modification to a histogram is the addition of a density curve. This can be achieved by first calculating the density of the data using the density() function and then adding the density curve to the histogram with the lines() function.

# Create a histogram
     freq = FALSE, 
     main = "Histogram with Density Curve", 
     xlab = "Generated Data", 
     ylab = "Density")

# Add a density curve
      col = "red")

Here, freq = FALSE makes the histogram present densities instead of frequencies, which is necessary when overlaying a density plot.

Creating Histograms with ggplot2

While base R provides the hist() function for creating histograms, many R users prefer the ggplot2 package for its powerful graphics capabilities and its consistent syntax. Here is an example of how to create a histogram with ggplot2.

First, make sure to install and load the ggplot2 package. You can do it with the following code:

# Install ggplot2

# Load ggplot2

Once you’ve done that, you can create a histogram using the geom_histogram() function.

# Create a histogram
ggplot(data.frame(data), aes(x = data)) + 
  geom_histogram(binwidth = 0.5, 
                 color = "black", 
                 fill = "lightblue") +
  labs(title = "Histogram with ggplot2",
       x = "Generated Data",
       y = "Count")

In this example, the binwidth argument specifies the bin width. The color argument changes the color of the bin borders, and the fill argument changes the color of the bars. The labs() function is used to add a title and axis labels.


A histogram is a useful tool for visualizing the distribution of data. In this article, you learned how to create a histogram in R using both the base R hist() function and the geom_histogram() function from the ggplot2 package. With these tools, you can customize your histogram in many ways to best represent your data.

Posted in RTagged

Leave a Reply