Open In App

How To Make Density Plots with ggplot2 in R?

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Density plots are a data visualization method used to estimate the probability density function (PDF) of a continuous variable. They provide smooth, continuous data distribution which makes them more informative than histograms in certain situations. The plot is produced by applying a kernel function usually a Gaussian (normal) kernel to the data points observed.

To create a density plot in R we use geom_density() function of ggplot2 package.

R
set.seed(1234)
df <- data.frame(value =round(c(rnorm(200,
                                      mean=100,
                                      sd=7))))
library(ggplot2)  

 ggplot(df, aes(x=value)) + geom_density() 

Output:

demsity_plot_in_r
Density Plots with ggplot2 in R

The plot displays a smooth curve representing the distribution of values in the dataset.

Syntax:

ggplot( aes(x)) + geom_density( fill, color, alpha)

Parameters:

  • fill: background color below the plot
  • color: the color of the plotline
  • alpha: transparency of graph

Customizing Color and Line Type

To change color we use the color property of geom_density() function.

1. Change Line Color and Type

R
set.seed(1234)
df <- data.frame(value =round(c(rnorm(900,
                                      mean=60, 
                                      sd=21))))
library(ggplot2)     

 ggplot(df, aes(x=value)) + geom_density(color="red",linetype="dashed") 

Output:

plot
Changed color line

2. Change Background Fill Color

To change the background color we use the fill property.

R
set.seed(1234)
df <- data.frame(value =round(c(rnorm(900,
                                      mean=60, 
                                      sd=21))))

library(ggplot2)       

 ggplot(df, aes(x=value)) + geom_density(fill="#77bd89",
                                         color="#1f6e34",
                                         alpha=0.8) 

Output:

Density_plot_with_kernel_selection
Changed background color

You can now see the plot with a customized line color and fill background.

Kernel selection

With kernel parameters the kernel that is being used can also be modified. There are other alternatives that can be selected including:

  • Gaussian (the default),
  • rectangular
  • triangular
  • epanechnikov
  • biweight
  • cosine
  • Optcosine

Below is a example of change kernel selection. Here we are using rectangular kernel.

R
set.seed(1234)
df <- data.frame(value = round(c(rnorm(900, mean = 60, sd = 21))))

library(ggplot2)

ggplot(df, aes(x = value, y = ..density..)) +
  geom_density(aes(fill = "Density"), alpha = 0.5,
               kernel = "rectangular") +
  scale_fill_manual(values = "lightgreen",
                    guide = guide_legend(override.aes = list(color = NA))) +
  labs(title = "Density Plot with Kernel Selection",
       x = "Value",
       y = "Density") +
  theme_minimal()

Output:

Density Plots with ggplot2 in RGeeksforgeeks
Changed Kernal

Combining Histogram and Density Plots

You can overlay a histogram with a density plot for comparing their empirical distribution of data with the estimated density. For this the geom_histogram() and geom_density() functions are combined.

R
set.seed(1234)
df <- data.frame(value = round(c(rnorm(900, mean = 60, sd = 21))))

library(ggplot2)

ggplot(df, aes(x = value)) +
  geom_histogram(aes(y = ..density..), 
                 bins = 30, 
                 fill = "lightblue", 
                 color = "black") +
  geom_density(alpha = 0.5, fill = "lightgreen") +
  labs(title = "Combined Histogram and Density Plot",
       x = "Value",
       y = "Density") +
  theme_minimal()

Output:

combine_histogram_and_density_plot
Combined Histogram and Density Plots

Next Article
Article Tags :

Similar Reads