How To Make Density Plots with ggplot2 in R?
Last Updated :
28 Apr, 2025
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:
Density Plots with ggplot2 in RThe 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:
Changed color line2. 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:
Changed background colorYou 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:
Changed KernalCombining 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:
Combined Histogram and Density Plots
Similar Reads
How to Make ECDF Plot with ggplot2 in R? Empirical Cumulative Distribution Function Plot (ECDF) helps us to visualize one or more distributions. ECDF plot is a great alternative for histograms and it has the ability to show the full range of data without the need for various parameters. In this article, we will discuss how to draw an ECDF
3 min read
How To Make Dumbbell Plot in R with ggplot2? The Dumbbell Plot shows the change between two points in our dataset. It is named so because of its Dumbbell shape. It helps us to understand the span of data categorically. To make Dumbbell Plot in R using ggplot2, Â we use the geom_dumbbell() function. Syntax: geom_dumbbell(data, aes(y, x, xend), s
2 min read
How To Make Violin Plots with ggplot2 in R? Violin plots help us to visualize numerical variables from one or more categories. They are similar to box plots in the way they show a numerical distribution using five summary-level statistics. But violin plots also have the density information of the numerical variables. It allows visualizing the
5 min read
How To Make Lollipop Plot in R with ggplot2? A lollipop plot is the combination of a line and a dot. It shows the relationship between a numeric and a categorical variable just like a barplot. A lollipop plot can be used at places where a barplot and scatter plot both are required. This single plot helps us visualize the problem better and tak
3 min read
How To Make World Map with ggplot2 in R? In this article, we will discuss how to create a world map and plot data on it using the R Programming Language. To create a world map using it we will use the geom_map() function of the ggplot2 package of the R Language. This function returns a ggplot object so all the functions that work on other
4 min read
How To Join Multiple ggplot2 Plots with cowplot? In this article, we are going to see how to join multiple ggplot2 plots with cowplot. To join multiple ggplot2 plots, we use the plot_grid() function of the cowplot package of R Language. Syntax: plot_grid(plot1,plot2,label=<label-vector>, ncol, nrow) Parameters: plot1 and plot2 are plots that
3 min read