Link Search Menu Expand Document


A density plot visualises the distribution of data over a continuous interval (or time period). Density Plots are not affected by the number of bins (each bar used in a typical histogram) used, thus, they are better at visualizing the shape of the distribution than a histogram unless the bins in the histogram have a theoretical meaning.

Keep in Mind

  • Notice that the variable on the x-axis should be continuous. Density plots are not designed for use with discrete variables.

Also Consider

= You might also want to know how to make a histogram or a line graph, click Histogram or Line graph for more information.



For this R demonstration, we are going to use ggplot2 package to create a density plot. Additionally, we will use the dataset diamonds that is natively available in R.

To begin with this R demonstration, make sure that we install and load all the useful packages that we need it. We use the function p_load() in pacman package to help us to install and load the packages at one time.

#install and load necessary packages
if (!require("pacman")) install.packages("pacman")

Next, in order to make a density plot, we are going to use the ggplot() and geom_density() functions. We will specify price as our x-axis.

ggplot(diamonds, aes(x=price))+

Basic density plot

We can always change the color of the density plot using the col argument and fill the color inside the density plot using fill argument. Furthermore, we can specify the degree of transparency density fill area using the argument alpha where alpha ranges from 0 to 1.

ggplot(diamonds, aes(x=price))+

Colored density plot We can also change the type of line of the density plot as well by adding linetype= inside geom_density().

ggplot(diamonds, aes(x=price))+

Density plot with linetype Furthermore, you can also combine both histogram and density plots together.

ggplot(diamonds, aes(x=price)) + 
 geom_histogram(aes(y=..density..), colour="black", fill="grey45")+

Density Plot Overlaid on Histogram

What happen if we want to make multiple densities?

For example, we want to make multiple densities plots for price based on the type of cut, all we need to do is adding fill=cut inside aes().

ggplot(data=diamonds, aes(x=price,fill=cut)) +

multiple :