Open In App

Histograms and Density Plots in R

Last Updated : 14 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

histograph
Output

Density 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:

gh
Density Plots in R

1. 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:

gh
Density Plots in R

2. 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:

gh
Density Plots in R

3. 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:

histograph
Output

4. 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 RGeeksforgeeks
Histogram and Density plot in R

Similar Reads