R ggplot2 Density Plot

The R ggplot2 Density Plot is useful to visualize the distribution of variables with an underlying smoothness. Let us see how to Create a ggplot density plot, Format its colour, alter the axis, change its labels, adding the histogram, and plot multiple density plots using R ggplot2 with an example.

R ggplot Density Plot syntax

The syntax to draw a ggplot Density Plot in R Programming is as shown below

geom_density(mapping = NULL, data = NULL, stat = "density",
             position = "identity", na.rm = FALSE, ..., show.legend = NA,
             inherit.aes = TRUE)

Before we get into the ggplot2 example, let us the see the data that we are going to use for this Density Plot example. For this demonstration, we are going to use the diamonds data set that is provided by the R, and the data inside this data set is:

R ggplot2 Density Plot 0

Create R ggplot2 Density Plot

In this example, we show you how to create a Density Plot using the ggplot2 package, and we are going to use the above-shown diamonds data set, provided by the R Studio.

TIP: ggplot2 package is not installed by default. Please refer Install R Packages article to understand the steps involved in installing a package in R Programming.

# Create R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Default way to Create a Density Plot
ggplot(data = diamonds, aes(x = price)) + 
  geom_density()
R ggplot2 Density Plot 1

Adjusting bandwidth of a R ggplot2 Density plot

Let us see how to change the bandwidth in a density plot using the adjust argument.

NOTE: If your requirement is to import data from external files then please refer R Read CSV article to understand the steps involved in CSV file import

# Adjusting bandwidth of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price)) + 
  geom_density(adjust = 1/5)
R ggplot2 Density Plot 2

Change Colors of R ggplot2 Density plot

In this example, we show how to change the color of a density plot drawn by the ggplot2.

  • color: Please specify the color you want to use for density curve borders. For example “red”, “blue”, “green” etc. In this example, we are assigning the “midnight blue” color to borders.
  • fill: You have to specify the color you want to fill / use. In this example, we are assigning the sea green color.
# Change Colors of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price)) + 
  geom_density(adjust = 1/5, fill = "seagreen",
               color = "midnightblue")
R ggplot2 Density Plot 3

Change Color of a R ggplot2 density plot example 2

Let us see how to fill the color of a ggplot2 density plot based on the column data. In this example, we are assigning the cut column as fill attribute. You can try changing it to any other column.

# Change Colors of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  geom_density(adjust = 1/5, color = "midnightblue")
R ggplot2 Density Plot 4

Change Color of an R ggplot2 Density plot example 3

Let us see how to change the color of a ggplot2 density plotlines based on the column data. In this example, we are assigning the cut column as Colour attribute. You can try changing it to any other column.

# Change Colors of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, colour = cut)) + 
  geom_density(adjust = 1/5)
R ggplot2 Density Plot 5

Change Color of a R ggplot2 Density plot example 4

In this example, we are changing the fill attribute to clarity.

# Change Colors of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = clarity)) + 
  geom_density(adjust = 1/15, color = "midnightblue")
R ggplot2 Density Plot 6

Change Color of a R ggplot2 Density plot example 5

In this example, we are changing the fill attribute to cut, and colour attribute to cut.

# Change Colors of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = cut, colour = cut)) + 
  geom_density(adjust = 1/15)
R ggplot2 Density Plot 7

Change Line Type of an R ggplot2 Density plot

Let us see how to change the border lines of a density plot

# Change Colors of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = clarity)) + 
  geom_density(adjust = 1/5, color = "midnightblue", linetype = 5)
R ggplot2 Density Plot 8

TIP: In R programming, 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash. So, you can use numbers or string as the linetype value

Alter Legend position of an R ggplot2 Density plot

By default, ggplot position the legend at the right side of a density plot. Let us change the legend position from right to top. Remember, You can use legend.position = “none” to completely remove the legend.

# Change Legend Position of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  geom_density(adjust = 1/5, color = "midnightblue") +
  theme(legend.position = "top")
R ggplot2 Density Plot 9

Multiple R ggplot Density Plots

Let me show how to draw multiple density plots, by dividing the data based on a column value.

# Multiple R ggplot Density Plots

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  geom_density(adjust = 1/5, color = "midnightblue") +
  facet_wrap(~ cut) # divide the Density plot, based on Cut
R ggplot2 Density Plot 10

How to use Facets in R ggplot2 Density plot

By default facet_wrap() assign the same y-axis to all the density plot. But you can change it (giving independent axis) to each density plot by adding one more attribute called scale.

# Multiple R ggplot Density Plots

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  geom_density(adjust = 1/5, color = "midnightblue") +
  facet_wrap(~ cut, scale = "free") 
R ggplot2 Density Plot 11

Assigning names to R ggplot density plot

Let us assign names to ggplot density plot, X-Axis, and Y-Axis using labs function

# Assigning Names to R ggplot Density Plots

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  geom_density(adjust = 1/5, color = "midnightblue") +
  facet_wrap(~ cut, scale = "free") +
  labs(title="GGPLOT Density Plot", x="Price in Dollars", y="Density") 
R ggplot2 Density Plot 14

Change Axis limits of an R density plot

Let us change the default axis values in a ggplot density plot

  • xlim: This argument helps to specify the limits for the X-Axis. Here, we are changing the default x-axis limit to (0, 20000)
  • ylim: Help you to specify the Y-Axis limits.
# Change X-AXIS of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  #facet_wrap(~ cut, scale = "free") +
  geom_density(adjust = 1/5, color = "midnightblue") +
  labs(title="GGPLOT Density Plot", x="Price in Dollars", y="Density") +
  xlim(0, 20000) 
R ggplot2 Density Plot 15

Create an R ggplot Density with Histogram

In real-time, we may interest in density based histograms because. Let us create a ggplot density plot against the histogram using geom_histogram().

# Histogram and R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  geom_density(color = "red") +
  geom_histogram(binwidth = 250, aes(y=..density..), fill = "midnightblue") +
  labs(title="GGPLOT Density Plot", x="Price in Dollars", y="Density")
R ggplot2 Density Plot 12

Changing Theme of a ggplot2 Density plot

How to change the default theme of a density plot?

  • theme_dark(): We use this function to change the density plot default theme to dark. Type theme_ then R Studio intelligence shows the list of available options. For example, theme_grey()
# Changing Theme of a R ggplot Density Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating a Density Plot
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  geom_density(adjust = 1/5, color = "midnightblue") +
  facet_wrap(~ cut, scale = "free") +
  labs(title="GGPLOT Density Plot", x="Price in Dollars", y="Density") +
  theme_dark()
R ggplot2 Density Plot 13