Histograms and Density Plots in R
Last Updated :
14 Jul, 2025
A histogram is a graphical representation that groups numeric data into ranges (bins) and helps visualize the distribution of values. It displays the count of observations within each bin using bars, making it ideal for understanding the frequency distribution.
Syntax:
hist(v,main,xlab,xlim,ylim,breaks,col,border)
Where:
- hist(): Creates a histogram to display the frequency or distribution of numeric data in R.
- v: A numeric vector used in the histogram.
- main: Title of the chart.
- xlab: Label for the x-axis.
- xlim: Range of values on the x-axis.
- ylim: Range of values on the y-axis.
- breaks: Width or number of bars.
- col: Fill color of the bars.
- border: Border color of the bars.
Example:
We create a histogram to visualize how the numeric values are distributed across defined ranges.
R
v <- c(5,9,13,2,50,20,59,36,23,2,8,27,72,14)
hist(v,xlab = "Weight",col = "red",border = "black")
Output:
OutputDensity Plot
A density plot is a smooth curve that shows the distribution of a numeric variable using kernel density estimation. It helps identify where values are more or less concentrated.
Syntax:
density(x)
Where:
- density(x): Computes a smooth estimate of the data distribution for a numeric vector using kernel density.
You can download the dataset from here.
Comparison Between Histogram and Density Plot
A histogram shows data using bars grouped into bins, while a density plot shows a smooth curve based on the same data. Histograms give frequency counts and density plots show probability distribution. Density plots are smoother and not affected by bin size.
Implementation of Histograms and Density Plot using ggplot2
We create a density plot to visualize the distribution of a numeric variable using a smooth kernel density curve. We read a dataset from Excel and uses ggplot2 to create a density plot for the Salary column.
- read_excel(): Reads data from an Excel file into R as a data frame.
- geom_density(): Adds a smooth density curve to represent the distribution.
- alpha: Adjusts the transparency of the filled color under the curve.
- labs(): Sets the plot’s title and axis labels.
R
library(readxl)
library(ggplot2)
Salary_Data <- read_excel("Salary_Data.xls")
den <- density(Salary_Data$YearsExperience)
library(ggplot2)
ggplot(Salary_Data, aes(x = Salary)) +
geom_density(fill = "skyblue", alpha = 0.7) +
labs(title = "Kernel Density Plot of Salary",
x = "Salary",
y = "Density")
Output:
Density Plots in R1. Customized Color and Line Type of Density Plots in R
We changed the border color using color, made the line dashed using linetype and adjusted transparency using alpha.
- color: Sets the border color of the density curve.
- linetype: Defines the style of the density line (e.g., "dashed", "solid").
R
library(ggplot2)
ggplot(Salary_Data, aes(x = Salary)) +
geom_density(fill = "purple", color = "black", linetype = "dashed", alpha = 0.5) +
labs(title = "Customized Density Plot of Salary",
x = "Salary",
y = "Density")
Output:
Density Plots in R2. Adjusted Bandwidth of Density Plots in R
We controlled the smoothness of the curve using bw and changed the fill color to blue.
- bw: Stands for bandwidth, which controls the smoothness of the density curve. A higher value results in a smoother curve, while a lower value makes it more sensitive to small changes in data.
R
library(ggplot2)
ggplot(Salary_Data, aes(x = Salary)) +
geom_density(fill = "blue", alpha = 0.7, bw = 2500) +
labs(title = "Density Plot of Salary with Adjusted Bandwidth",
x = "Salary",
y = "Density")
Output:
Density Plots in R3. Create a histogram and a density plot in the same frame
We combined a histogram and density curve using lines() and enabled probability scaling using prob = TRUE.
- lines(): Adds a line plot (in this case, the density curve) to an existing plot.
R
hist(beaver1$temp,
col="green",
border="black",
prob = TRUE,
xlab = "temp",
main = "GFG")
lines(density(beaver1$temp),
lwd = 2,
col = "chocolate3")
Output:
Output4. Customize the Histogram plots and Density Plot in R
We customized the histogram further by adding lty = "dashed" and used fill = "lightblue".
R
hist(beaver1$temp,
col = "green",
border = "black",
prob = TRUE,
xlab = "temp",
main = "GFG",
fill = "lightblue",
lty = "dashed"
)
lines(density(beaver1$temp),
lwd = 2,
col = "chocolate3"
)
Output:
Histogram and Density plot in R
Similar Reads
Histograms and Density Plots in Python Prerequisites: SeabornThe histogram is the graphical representation that organizes a group of data points into the specified range. Creating the histogram provides the Visual representation of data distribution. By using a histogram we can represent a large amount of data, and its frequency.Density
4 min read
Overlay Density Plots in Base R In this article, we will discuss how to overlay density plot using some base functions of the R Programming Language. Overlaying density plots means creating some density plots of the different data over a single plot. Functions UsedPlot() : This is a generic function for plotting R objects. Syntax:
2 min read
Difference between Histogram and Density Plot Histograms and density plots are two powerful visualization tools used to represent data distributions, but they serve different purposes and offer unique advantages. A histogram is a bar chart that groups data into bins, showing the frequency or count of values within each bin. In contrast, a densi
7 min read
Overlay Histogram with Fitted Density Curve in R In this article, we will be looking at the different approaches to overlay histogram with fitted density curve in R programming language. Method 1: Using line() and density() functions In this approach for overlaying histogram with the fitted density curve user need not install or import any library
3 min read
How to create Kernel Density Plot in R? In this article, we will discuss how to create kernel density plots in R programming language. For this, the user simply needs to call the density() function which is an in-build function in R language. Then the user has to pass the given data as the parameter to this function in order to create a d
5 min read
Plot Normal Distribution over Histogram in R In this article, we will discuss how to plot normal distribution over Histogram in the R Programming Language. In a random dataset, it is generally observed that the distribution of data is normal i.e. on its visualization using density plot with the value of the variable in the x-axis and y-axis we
3 min read