How to Create Histograms in R

Dipti MDipti M
4 min read

When starting analysis on any dataset, one of the first steps is to visualize its distribution. A histogram is one of the simplest yet most effective ways to do this.

Histograms provide an intuitive overview of how data values are spread. They make it easy to identify central tendencies like the median and mode, spot outliers, and detect gaps in the data. For example, the distribution of student grades in a class or employee ages in an organization is best understood through a histogram.

The best part? A histogram condenses large amounts of data into a single chart while conveying deep insights.

In this article, we’ll walk through how to create and customize histograms in R, from basic plots to advanced customizations.


Table of Contents

  1. Basics of Histograms

  2. Implementing Histograms in R

    • Example 1: AirPassengers Data

    • Example 2: Iris Dataset

  3. Customizing Histograms with Parameters

  4. Advanced Histogram Options

  5. Complete R Code


1. Basics of Histograms

A histogram is a bar chart that shows the frequency of values within specified ranges (called bins). It is usually created for a single numeric variable.

  • X-axis: The variable’s range, divided into intervals (bins).

  • Y-axis: The frequency (count) of observations falling into each bin.

In R, the hist() function is the standard way to generate histograms. Understanding this function is essential for univariate descriptive analytics and serves as the foundation for more advanced visualization.


2. Implementing Histograms in R

Example 1: AirPassengers Dataset

The built-in AirPassengers dataset contains monthly totals of international airline passengers (1949–1960).

# Plot the time series
plot(AirPassengers)

This shows a clear upward trend with seasonal variation. To examine the frequency distribution of passenger counts, we use hist():

# Histogram of AirPassengers data
hist(AirPassengers)

Here, most monthly passenger totals fall between 100–200, gradually spreading out as the trend increases. Since this is a time series with upward growth, the histogram is skewed toward lower values.


Example 2: Iris Dataset

The famous iris dataset provides measurements of flower attributes. Let’s analyze the distribution of Petal.Length.

# Plot the raw distribution
plot(iris$Petal.Length)

# Histogram of Petal.Length
hist(iris$Petal.Length)

Observations:

  • There are three visible clusters (~1–2, 3–5, 5–7).

  • Most values lie between 4–5.

  • Few values extend beyond 6.

Since histograms work only with numeric variables, attempting to plot a histogram of a categorical variable (like Species) will result in an error. Instead, use plot(iris$Species) to view category counts.


3. Customizing Histograms with Parameters

The hist() function in R is highly flexible. Some common parameters include:

  • main: Title of the histogram

  • xlab / ylab: Axis labels

  • col: Bar color

  • border: Border color of bars

  • breaks: Number of bins

  • xlim / ylim: Axis limits

  • las: Orientation of axis labels

  • freq / probability: Plot counts (default) or probability density

Example: Adding labels and colors

hist(iris$Petal.Length,
     main = "Histogram of Petal Length",
     xlab = "Petal length (cm)",
     ylab = "Count",
     col = "blue",
     border = "red")

Example: Changing bin size and axis orientation

hist(iris$Petal.Length,
     main = "Histogram with Custom Bins",
     xlab = "Petal length (cm)",
     ylab = "Count",
     col = "lightblue",
     breaks = 6,
     las = 1)   # Horizontal axis labels

4. Advanced Histogram Options

Probability Histogram

Instead of frequencies, plot relative probabilities by setting freq = FALSE:

hist(iris$Petal.Length,
     main = "Probability Histogram",
     xlab = "Petal length (cm)",
     col = "skyblue",
     freq = FALSE)

Shaded Density Bars

Use density and angle to add shading patterns:

hist(iris$Petal.Length,
     main = "Shaded Histogram",
     xlab = "Petal length (cm)",
     col = "blue",
     density = 50,
     angle = 60)

Display Exact Counts

Enable labels on top of bars:

hist(iris$Petal.Length,
     main = "Histogram with Labels",
     xlab = "Petal length (cm)",
     col = "lightgreen",
     labels = TRUE)

5. Complete R Code

Here’s the full set of R commands used in this article:

# AirPassengers data
plot(AirPassengers)
hist(AirPassengers)

# Iris dataset
plot(iris$Petal.Length)
hist(iris$Petal.Length)

# Species distribution (categorical variable)
plot(iris$Species)

# Custom histogram examples
hist(iris$Petal.Length, main="Histogram of Petal Length", xlab="Petal length (cm)", ylab="Count", col="blue", border="red")
hist(iris$Petal.Length, breaks=6, las=1, col="lightblue")
hist(iris$Petal.Length, freq=FALSE, col="skyblue")
hist(iris$Petal.Length, density=50, angle=60, col="blue")
hist(iris$Petal.Length, labels=TRUE, col="lightgreen")

Final Thoughts

Histograms in R are a powerful first step in data exploration. With just the hist() function, you can:

  • Understand distributions

  • Spot clusters, gaps, and outliers

  • Customize charts for better readability

They serve as the foundation for deeper statistical analysis and visualization.

This article was originally published at Perceptive Analytics

Our mission is to help organizations unlock the full potential of their data. Over the past two decades, we’ve partnered with Fortune 500 companies and mid-sized firms alike to address complex analytics challenges and deliver measurable results. Our expertise spans across Tableau consultants, Looker Consultant, and AI Consulting, empowering businesses to transform raw information into strategic insights that drive growth and efficiency.

0
Subscribe to my newsletter

Read articles from Dipti M directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dipti M
Dipti M