How To Make Histogram In R

4 min read

Learning how to make histogram in R is an essential skill for anyone working with data, whether you are a student, researcher, or professional analyst. A histogram provides a quick visual summary of the distribution of a continuous variable, revealing patterns such as skewness, modality, and outliers that might be hidden in raw numbers. Which means this guide walks you through the entire process—from preparing your data to customizing the final plot—using both base R functions and the powerful ggplot2 package. By the end, you will be able to produce clear, publication‑ready histograms and interpret them with confidence Which is the point..

Short version: it depends. Long version — keep reading Easy to understand, harder to ignore..

Why Histograms Matter in Data Analysis

Histograms are more than just bar charts; they encode the frequency of observations within intervals, or bins, along the x‑axis. The height of each bar reflects how many data points fall into that range, making it easy to see where values concentrate. When you learn how to make histogram in R, you gain a tool for:

  • Detecting normality or deviation from it
  • Comparing groups side‑by‑side (by overlaying or faceting)
  • Informing decisions about data transformations or modeling choices
  • Communicating results to audiences with varying statistical backgrounds

Because the choice of bin width can dramatically alter the appearance of a histogram, mastering the parameters that control binning is a key part of the workflow.

Creating Histograms with Base R

R’s built‑in graphics system includes the hist() function, which is ideal for quick exploratory plots. Below we cover the core syntax, common customizations, and how to save your output.

Using the hist() Function

The simplest call requires only a numeric vector:

# Example data: heights (in cm) of 100 individuals
set.seed(123)
heights <- rnorm(100, mean = 170, sd = 10)

# Basic histogram
hist(heights)

This produces a default histogram with Sturges’ rule for bin selection, light gray bars, and axis labels derived from the variable name.

Customizing Bin Width and Colors

You can override the automatic binning by specifying breaks. Options include:

  • A single integer suggesting the approximate number of bins
  • A numeric vector defining the exact cut‑points
  • A character string naming an algorithm ("Sturges", "Freedman-Diaconis", "Scott")
# Manually set 20 bins
hist(heights, breaks = 20, col = "steelblue", border = "white",
     main = "Distribution of Heights",
     xlab = "Height (cm)", ylab = "Frequency")
  • col fills the bars; border controls the edge color.
  • main, xlab, and ylab add titles and axis labels.
  • Adding freq = FALSE converts the y‑axis to a density scale, useful for overlaying probability density functions.

Adding a Normal Curve

To assess normality, superimpose a theoretical normal density:

hist(heights, breaks = 15, prob = TRUE, col = "lightgreen",
     main = "Histogram with Normal Overlay",
     xlab = "Height (cm)")

curve(dnorm(x, mean = mean(heights), sd = sd(heights)),
      add = TRUE, col = "darkred", lwd = 2)

Setting prob = TRUE scales the histogram to a probability density, making the curve comparable.

Saving the Plot

Base R graphics can be saved directly to files:

png("height_histogram.png", width = 800, height = 600)
hist(heights, breaks = "FD", col = "tomato")
dev.off()

Replace png with pdf, jpeg, or tiff as needed Most people skip this — try not to..

Creating Histograms with ggplot2

While base R is sufficient for quick checks, the ggplot2 package offers greater flexibility, layered aesthetics, and seamless integration with the tidyverse workflow. Install and load it first:

install.packages("ggplot2")
library(ggplot2)

Basic ggplot2 Histogram

The core syntax maps a variable to the x‑axis and uses geom_histogram():

ggplot(data = data.frame(heights), aes(x = heights)) +
  geom_histogram(bins = 25, fill = "darkorange", color = "black") +
  labs(title = "Histogram of Heights (ggplot2)",
       x = "Height (cm)",
       y = "Count")
  • bins controls the number of intervals (alternatively, binwidth sets the exact width).
  • fill determines the interior color; color sets the outline.

Adjusting Aesthetics and Themes

ggplot2’s theming system lets you fine‑tune every element:

ggplot(data = data.frame(heights), aes(x = heights)) +
  geom_histogram(binwidth = 2, fill = "#69b3a2", color = "white", alpha = 0.8) +
  theme_minimal(base_size = 14) +
  theme(plot.title = element_text(face = "bold", hjust = 0.5),
        axis.title = element_text(face = "italic")) +
  labs(title = "Height Distribution",
       subtitle = "Bin width = 2 cm",
       caption = "Source: simulated data")
  • alpha adds transparency, useful when overlaying multiple histograms.
  • theme_minimal() provides a clean background; you can switch to theme_classic(), `
Just Finished

New Stories

These Connect Well

Related Corners of the Blog

Thank you for reading about How To Make Histogram In R. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home