Lewati ke konten utama

R Histogram Tutorial: Create and Customize Plots with Base R

Learn to create and customize histograms in base R using the hist() function. Covers colors, labels, bins, density curves, and descriptive statistics.
Diperbarui 29 Mei 2026  · 10 mnt baca

In this tutorial, I'll walk you through creating and customizing histograms in the R programming language. We'll cover what a histogram is, how to set up your data, how to build a basic histogram, and how to adjust colors, labels, bins, and axis limits. 

We will be using the base R programming language with no additional packages. This approach is especially useful when additional packages cannot be used or when you are looking for quick exploratory analyses. In other cases, you might consider using ggplot2, as covered in our How to Make a ggplot2 Histogram in R tutorial.

To easily run all the example code in this tutorial yourself, you can create a DataLab workbook for free that has R pre-installed and contains all code samples. For more practice on how to make a histogram in R, check out this hands-on DataCamp exercise.

Working with histograms in other tools? See our guides on Histograms in Matplotlib for Python and How to Create a Histogram in Excel.

TL;DR

  • Use hist(x) to create a basic histogram in base R with no extra packages

  • Customize appearance with col (fill color), border (outline), main (title), xlab, and ylab

  • Control binning with the breaks argument: pass a number, a vector of breakpoints, or a method name ("Sturges", "Scott", "Freedman-Diaconis")

  • Overlay descriptive statistics using abline() for the mean and lines(density()) for a density curve

  • For grouped or publication-quality histograms, switch to ggplot2

What Is a Histogram?

A histogram is a graph that shows frequency distributions across continuous (numeric) variables. Histograms allow us to see the count of observations in data within ranges that the variable spans.

Histograms look similar to bar charts. A key difference between the two is that bar charts have a value associated with a specific category or discrete variable, while a histogram visualizes frequencies for continuous variables. 

Setting Up Data for Histograms

We will be using this housing dataset which includes details about different house listings, including the size of the house, the number of rooms, the price, and location information. We can read the data using the read.csv() function, either directly from the URL or by downloading the csv file into a directory and reading it from our local storage. We can also specify that we only want to store the columns we are interested in for this tutorial: price and condition.

home_data <- read.csv("https://raw.githubusercontent.com/rashida048/Datasets/master/home_data.csv")[ ,c('price', 'condition')]

Let’s look at the first few rows of data using the head() function.

head(home_data, 5)

First rows of the housing dataset showing price and condition columns in R

Creating Histograms with Base R

Next, we will create a histogram using the hist() function to look at the distribution of prices in our dataset.

hist(home_data$price)

Making a histogram of home prices in base R

Basic histogram of home prices. Image by Author.

Adding descriptive statistics

We can add descriptive statistics to the histogram using the abline() function. This adds a vertical line to the plot.

  • Set the v argument to the position on the x-axis for the vertical line. Here, we get the mean house price using mean().

  • The col argument sets the line color, in this case to red.

  • The lwd argument sets the line width. A value of 3 increases the thickness of the line to make it easier to see.

hist(home_data$price)

abline(v = mean(home_data$price), col='red', lwd = 3)

Plotting probability densities

To add a probability density line to the histogram, we first change the y-axis to be scaled to density. In the call to hist() , we set the probability argument to TRUE.

The probability density line is made with a combination of density(), which calculates the position of the probability density curve, and lines(), which adds the line to the existing plot.

hist(home_data$price, probability = TRUE)
abline(v = mean(home_data$price), col='red', lwd = 3)
lines(density(home_data$price), col = 'green', lwd = 3)

A base R histogram of home prices with the average highlighted
Histogram of home prices with the average highlighted. Image by Author.

Notice that the numbers on the y-axis have changed.

Customizing the color

The col parameter of hist() sets the fill color of the bars, and border sets the outline color. Here I'll set the fill to blue and the borders to white.

hist(home_data$price, col = 'blue', border = "white")

A base R histogram of home prices with color addedHistogram of home prices with color added. Image by Author.

Adding labels and titles

We can change the labels on the plot to make it more readable and presentable. This is useful if you share the plot with others.

  • xlab sets the x-axis label

  • ylab sets the y-axis label

  • main sets the plot title

hist(home_data$price, xlab = 'Price (USD)', ylab = 'Number of Listings', main = 'Distribution of House Prices')

A base R histogram with axis labels addedHistogram of home prices with axis labels. Image by Author.

Binning using breaks

With the default arguments, it is challenging to see the full distribution of the housing prices across the range of prices. We can see they are centralized in the first few bins, but they are not very descriptive. 

We can add more bins using the breaks parameter. With this argument, we can pass a vector of specific breakpoints to use, a function to compute the breakpoints, a number of breaks we would like, or a function to compute the number of cells. 

For this example, we will pass the number of bins we would like. This number is context-specific based on what you are trying to show in your graph.

hist(home_data$price, breaks = 100)

A base R histogram with the bin width changedHistogram of home prices with bin width changed. Image by Author.

With breaks set to 100, we have significantly more visibility into the distribution in the first few buckets. 

We can also specify the number of breaks using the names of common calculations for calculating optimal breaks in a histogram. By default, hist() uses the “Sturges” method. Here we specify the method explicitly.

hist(home_data$price, breaks = "Sturges")

A base R histogram using the Sturges methodHistogram of home prices using the Sturges method. Image by Author.

We can also pass “Scott” as an argument for the breaks attribute to use the Scott Method. 

hist(home_data$price, breaks = "Scott")

A base R histogram using the Scott method
Histogram of home prices using the Scott method. Image by Author.

Finally, we could also use the Freedman-Diaconis (FD) method. 

hist(home_data$price, breaks = "Freedman-Diaconis")

A base R histogram using the Freedman-Diaconis methodHistogram of home prices using the Freedman-Diaconis method. Image by Author.

Comparing break methods

The table below summarizes the three named break methods available in hist(). The default is Sturges.

Method How it works Best for
Sturges Bins = 1 + log₂(n). Assumes roughly normal data. Small to medium datasets with near-normal distributions
Scott Bin width = 3.49 × sd × n⁻¹ᐟ³. Minimizes integrated mean squared error. Normally distributed data where variance matters
Freedman-Diaconis Bin width = 2 × IQR × n⁻¹ᐟ³. Uses interquartile range instead of standard deviation. Skewed data or distributions with outliers

Setting axis limits

We can set the x-axis limits of our plot using the xlim  argument to zoom in on the data we are interested in. For example, it is sometimes helpful to focus on the central part of the distribution, rather than on the long tail we currently see when we view the whole plot. 

Changing the y-axis limits is also possible (using the ylim argument), but this is less useful for histograms since the automatically calculated values are almost always ideal.

We will zoom in on prices between $0 and $2M.

hist(home_data$price, breaks = 100, xlim = c(0, 2000000))

Base R histogram of home prices zoomed to the 0 to 2 million dollar rangeHistogram of home prices with the axis limits changed. Image by Author.

Next Steps in Histogram Visualization

As you get more comfortable with R, you can try dedicated visualization packages that offer more control over aesthetics, layout, and interactivity. A very popular and easy-to-use library for plotting in R is called ggplot2. Below we create an interesting view of the distributions of prices based on the number of bedrooms in the house.  


Faceted histogram of home prices by bedroom count using ggplot2 in RHistogram of home prices using ggplot2. Image by Author.

ggplot2 is the most widely used visualization package in R. You can learn to create histograms with it in our How to Make a ggplot2 Histogram in R tutorial, or explore the Graphics with ggplot2 tutorial for a broader introduction. Check out our Introduction to ggplot2 course and Intermediate ggplot2 course to go deeper. 

Final Thoughts

In this tutorial, we learned that histograms are great visualizations for looking at distributions of continuous variables. We learned how to make a histogram in R, how to plot summary statistics on top of our histogram, how to customize features of the plot like the axis titles, the color, how we bin the x-axis, and how to set limits on the axes. Finally, we demonstrated some of the power of the ggplot2 library.

For further DataCamp reading and resources, check out our interactive courses: 

Learn R Essentials

Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
Start Learning R for Free

Author
Kevin Babitz
LinkedIn

Data Science writer | Senior Technical Marketing Analyst at Wayfair | MSE in Data Science at University of Pennsylvania

Topik

Learn R with DataCamp

Kursus

Pengantar R

4 Hr
3M
Lihat DetailRight Arrow
Mulai Kursus
Lihat Lebih BanyakRight Arrow
Terkait

Tutorials

ggplot2 Histogram in R: Complete Tutorial with Examples

Learn to create and customize ggplot2 histograms in R, from basic plots to colored, faceted, and density-overlaid visualizations.

Kevin Babitz

Tutorials

Box Plot in R Tutorial

Learn about box plots in R, including what they are, when you should use them, how to implement them, and how they differ from histograms.
DataCamp Team's photo

DataCamp Team

Tutorials

R Formula Tutorial

Discover the R formula and how you can use it in modeling- and graphical functions of well-known packages such as stats, and ggplot2.
Karlijn Willems's photo

Karlijn Willems

Tutorials

How to Create a Histogram with Plotly

Learn how to implement histograms in Python using the Plotly data visualization library.
Kurtis Pykes 's photo

Kurtis Pykes

Tutorials

How to Create a Histogram in Excel: A Step-by-Step Guide

Master the art of visualizing data in Excel using histograms! This step-by-step guide will walk you through various methods, such as built-in charts, formulas, and the Analysis ToolPak, to help you analyze frequency distributions like a pro.
Jachimma Christian's photo

Jachimma Christian

Tutorials

Histograms in Matplotlib

Learn about histograms and how you can use them to gain insights from data with the help of matplotlib.
Aditya Sharma's photo

Aditya Sharma

Lihat Lebih BanyakLihat Lebih Banyak