How to Create a Density Plot with ggplot2


A density plot is a type of plot that uses a single smooth curve to help us visualize the distribution of values in a dataset.

This type of plot is similar to a histogram, which allows us to visualize the distribution of values in a dataset grouped into buckets.

The easiest way to create a density plot in R is by using the geom_density() function from the ggplot2 package.

You can use the following basic syntax to use this function to create a density plot:

library(ggplot2)

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

This particular example creates a density plot for the variable named my_variable within the data frame named df.

Note that you can also supply the following arguments to the geom_density() function to customize the appearance of the plot:

  • color: The outline color to use for the density curve
  • fill: The fill color to use inside the density curve
  • alpha: The transparency of the fill color (0 = invisible, 1 = fully visible)

The following example shows how to use the geom_density() function to create a density plot in ggplot2.

Example: How to Create a Density Plot with ggplot2

For this particular example, we will use the built-in mtcars dataset in R.

We can use the head() function to view the first six rows of this dataset:

#view first six rows of mtcars dataset
head(mtcars)

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

The dataset contains several variables that tell us different information about various cars.

Suppose that we would like to create a density plot of the mpg variable, which represents the miles per gallon of each car in the dataset.

We can use the following syntax to do so:

library(ggplot2)

#create density plot of mpg
ggplot(mtcars, aes(x=mpg)) +
  geom_density()

This produces the following density plot:

density plot in ggplot2

The x-axis displays the distribution of values for the mpg variable, which we can see ranges from about 10 mpg to 35 mpg.

The y-axis displays the density, or the relative frequencies of each value for the mpg variable.

The smooth curve inside the plot is the density curve, which gives us an idea of how common each value is for the mpg variable.

From the plot we can tell that most cars have mpg values between 15 and 20 while it is less common for cars to have mpg values as low as 10 or as high as 30 or more.

Note that we can also use the fill, color and alpha arguments in the geom_density() function to customize the appearance of the chart:

library(ggplot2)

#create density plot of mpg with custom colors
ggplot(mtcars, aes(x=mpg)) +
  geom_density(fill='coral2', color='grey', alpha=0.8)

This produces the following density plot:

density plot in ggplot2 with custom colors

Feel free to play around with the values to create a density plot with the exact appearance that you would like.

Additional Resources

The following tutorials explain how to perform other common tasks in ggplot2:

How to Adjust Space Between Bars in ggplot2
How to Remove NAs from Plot in ggplot2
How to Change Colors of Bars in Stacked Bart Chart in ggplot2

Leave a Reply

Your email address will not be published. Required fields are marked *