How to create a Density plot in R?

Filed Under: R Programming
Density Plot Using Ggplot2 In R

You can create a density plot in R using ggplot2. For plotting using ggplot2, you have to use the function geom_density(). Let’s see how it works in this tutorial.

What is a Density plot?

Density plots, also known as Kernel density plots, they’re are used to understand the distribution of data. It is considered as an effective way to present the variable distribution over the given time period. The density plot’s peak gives the data of concentrated values over the time period.

You can either create a density plot in basic R or by using ggplot as mentioned above.


Advantages of Density plots over Histograms

  • Density plots are better than histograms, as they can determine the distribution shape effectively.
  • Unlike histograms, density plots are not affected by bins.
  • It gives a clear visualization of the data distribution over a time period.

There are many types of density plots are available to plot in R. All of them are used based on various problems and requirements.

Let’s roll into the topics and we can plot all types of density plots in R


Basic density plot using ggplot2 in R

In this section we are creating a basic density plot using ggplot2 in R. For this purpose, we will import a pricing data file. After that, we will plot the density plot for the values present in that file.

You can download the sample files used in this tutorial from this link.
Sources: Iris dataset, Google play store apps dataset

Execute the below code to create a simple density plot in Rstudio.

library(ggplot2)               #imports ggplot2
library(dplyr)                 #imports dplyr

#loads the data from the .txt file with header true. 
data <- read.table("price.txt", header=TRUE)
 
#creating the density plot 
data %>%
+     filter( Dollars<400 ) %>%
+     ggplot( aes(x=Dollars)) +
+     geom_density(fill="#4D9DDA", color="#4D9DDA", alpha=0.8)

Density Plot in R
Density Plot in R

Now we will try to add a title to our density plot. Execute the below code to create a density plot with a suitable title.

library(ggplot2)               #imports ggplot2
library(dplyr)                 #imports dplyr
 
#loads the data from the .txt file with header true. 
data <- read.table("price.txt", header=TRUE)
 
#creating the density plot 
data %>%
+     filter( Dollars<400 ) %>%
+     ggplot( aes(x=Dollars)) +
+     geom_density(fill="#4D9DDA", color="#4D9DDA", alpha=0.8)+
+    ggtitle("State wise population distribution in the USA")+theme_ipsum()

density plot in r

hrbrtheme: hrbrthemes is an additional theme package in ggplot2 which mainly concentrates on typography in the plots.


The Mirror density plots in R using ggplot2

As you know that the density plots are the representation of the distribution of the values. The mirror density plots are used to compare the 2 different plots.

The exactly opposite or mirror plot of the values will make comparison very easy and efficient. For creating this mirror density plot using ggplot2, we use geom_density function.

To plot the mirror density plot, execute the below code in Rstudio.

library(ggplot2)       #importing library ggplot2
library(hrbrthemes)    #importing library hrbrthemes


#sample data taken for plotting
data <- data.frame(data1 = rnorm(1000),data2 = rnorm(1000, mean=2))

p <- ggplot(data, aes(x=x))+
 
#top portion plot 
geom_density( aes(x = data1, y = ..density..), fill="#D2CE12" ) +
geom_label( aes(x=6, y=0.20, label="data1"), color="#1EAEC2") +
 
#bottom portion plot
geom_density( aes(x = data2, y = -..density..), fill= "#66B32D") +
geom_label( aes(x=6, y=-0.20, label="data2"), color="#1EAEC2") +
theme_ipsum() +
xlab("x values")

mirror density plot in r

Multiple Density plots in R using ggplot2

Multiple density plots: These are the plots that use multiple variables and multiple fills to create a graph, which shows the distribution of values.

In this section, we are going to create multiple density plots using ggplot2. In this plot, we are using the google play store data which is available in Kaggle.

We are plotting the graph using the Content Rating and Numbers data.

Execute the below code to create the multiple density plot in R studio.

library(ggplot2)     #imports library ggplot2
library(hrbrthemes)  #imports library hrbrthemes
library(dplyr)       #imports the dplyr function
Library (tidyr)      #imports the tidyverse package
library(viridis)     #imports the library viridis
 
readfile<-read.csv("googleplaystore.csv")  #reads the data
 
x1 <- ggplot(googleplaystore, aes(x=Number, group=Content.Rating, fill=Content.Rating))+
geom_density(adjust=1)+         #plots the density graph
theme_ipsum()
x1
density plot in r

Creating Small Multiples using facet_wrap() function

This small multiples of the density plot will help us to understand the distribution of each variable. The individual plots will help us to compare the different variable distribution as they lie on the same axis.

For this purpose we are using facet_wrap() function.

To create the small multiples of the density plot, execute the below code in Rstudio.

readfile <- read.csv("googleplaystore.csv")   #reads the file
ggplot(data=readfile, aes(x=Number, group=Content.Rating, fill=Content.Rating)) +
     geom_density(adjust=2) +
     theme_ipsum() +
     facet_wrap(~Content.Rating) +    #creates the small multiples
     theme(
         legend.position="none",
         panel.spacing = unit(0.2, "lines"),
         axis.ticks.x=element_blank()
     )
multiple density plots in r

Stacked density plots in R using ggplot2

The stacking density plot is the plot which shows the most frequent data for the given value. But the disadvantage of the stacked plot is that it does not clearly show the distribution of the data.

Here we are creating a stacked density plot using the google play store data.

Execute the below code to create the stacked density plot in R studio.

readfile <- read.csv("googleplaystore.csv")   #reads the data
plt <- ggplot(data=readfile, aes(x=Number, group=Content.Rating, fill=Content.Rating)) +
     geom_density(adjust=1.5, position="fill") +
     theme_ipsum()
 
plt   #displays the plot
stacked density plot in R

2D Density plots in R using ggplot2

R offers the function geom_density2d() to plot the two dimensional density plots. 2D graphs are visually appealing in nature and can communiacte the insights in an effective manner .

For this purpose we are using the iris flower dataset which is available in the kaggle webiste. Lets plot the density plot for sepal length and with varibales.

Execute the below code to create a 2D density plot in R studio.

library(ggplot2)

#reads the iris flower data
readfile <- read.csv("Iris.csv")
View(readfile)

#marks the x and y axis values 
x <- ggplot(data=readfile, aes(x=SepalLength.Cm, y=SpealWidth.Cm))
View(x)

#generated the 2D density plot
x+stat_density2d()+geom_point()

#create an appealing 2D plot
values+stat_density2d(aes(fill=..density..), geom='raster',contour=FALSE)
values+stat_density2d(aes(fill=..density..), geom='tile',contour=FALSE)+geom_point(color='white')

#shows the density points in the plot
densitypoints <- values+stat_density2d(aes(fill=..density..), geom='tile',contour=FALSE)+geom_point(color='white')

#creates the x and y labels 
densitypoints+xlab('Sepal length')+ylab('Sepal width')



The iris flower data is shown below.

iris flower data

The 2-Dimensional density plot of the values present in iris flower data.

2d density plot in r

Conclusion

R is one of the best visualization heavy language. In this tutorial, we have gone through the density plots, its benefits and we have plotted the density plots using ggplot2.

There are many types of density plots in R. We have plotted basic density plots with x and y labels, mirror density plots, multi-density plots, stacked density plots and finally we have plotted the 2-dimensional density plot using the iris dataset.

R offers great libraries and functions to create visually appealing graphs for any purpose. That’s all for now. Learn more…Practice more and stay connected for more R tutorials. Keep learning!!!

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages