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 draw 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:
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 RStudio.
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.
library(ggplot2) # Default way ggplot(data = diamonds, aes(x = price)) + geom_density()
Adjusting bandwidth
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
library(ggplot2) ggplot(data = diamonds, aes(x = price)) + geom_density(adjust = 1/5)
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.
library(ggplot2) ggplot(data = diamonds, aes(x = price)) + geom_density(adjust = 1/5, fill = "seagreen", color = "midnightblue")
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.
library(ggplot2) ggplot(data = diamonds, aes(x = price, fill = cut)) + geom_density(adjust = 1/5, color = "midnightblue")
Change Color 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 the colour attribute. You can try changing it to any other column.
library(ggplot2) ggplot(data = diamonds, aes(x = price, colour = cut)) + geom_density(adjust = 1/5)
Change Color Example 4
In this example, we are changing the fill attribute to clarity.
library(ggplot2) ggplot(data = diamonds, aes(x = price, fill = clarity)) + geom_density(adjust = 1/15, color = "midnightblue")
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.
library(ggplot2) ggplot(data = diamonds, aes(x = price, fill = cut, colour = cut)) + geom_density(adjust = 1/15)
Change Line Type of an R ggplot2 Density plot
Let us see how to change the border lines.
library(ggplot2) ggplot(data = diamonds, aes(x = price, fill = clarity)) + geom_density(adjust = 1/5, color = "midnightblue", linetype = 5)
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
By default, ggplot positions the legend on the right side. Let us change the legend position from right to top. Remember, You can use legend.position = “none” to completely remove the legend.
library(ggplot2) ggplot(data = diamonds, aes(x = price, fill = cut)) + geom_density(adjust = 1/5, color = "midnightblue") + theme(legend.position = "top")
Multiple R ggplot Density Plots
Let me show how to draw multiple density plots, by dividing the data based on a column value.
library(ggplot2) 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
How to use Facets in R ggplot2 Density plot
By default facet_wrap() assign the same y-axis to all. But you can change it (giving independent axis) to each one by adding one more attribute called scale.
library(ggplot2) ggplot(data = diamonds, aes(x = price, fill = cut)) + geom_density(adjust = 1/5, color = "midnightblue") + facet_wrap(~ cut, scale = "free")
Assigning names to R ggplot density plot
Let us assign names to the chart, X-Axis, and Y-Axis using labs function
library(ggplot2) 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")
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.
library(ggplot2) 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)
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().
library(ggplot2) 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")
Changing Theme of a ggplot2 Density plot
How to change the default theme?
- theme_dark(): We use this function to change the default theme to dark. Type theme_ then R Studio intelligence shows the list of available options. For example, theme_grey()
library(ggplot2) 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()