0.4 C
London
Friday, March 14, 2025
HomeStatistics TutorialRHow to Create Kernel Density Plots in R (With Examples)

How to Create Kernel Density Plots in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

A kernel density plot is a type of plot that displays the distribution of values in a dataset using one continuous curve.

A kernel density plot is similar to a histogram, but it’s even better at displaying the shape of a distribution since it isn’t affected by the number of bins used in the histogram.

We can use the following methods to create a kernel density plot in R:

Method 1: Create One Kernel Density Plot

#define kernel density
kd #create kernel density plot
plot(kd)

Method 2: Create a Filled-In Kernel Density Plot

#define kernel density
kd #create kernel density plot
plot(kd)

#fill in kernel density plot with specific color
polygon(kd, col='blue', border='black')

Method 3: Create Multiple Kernel Density Plots

#plot first kernel density plot
kd1 blue')

#plot second kernel density plot
kd2 red')

#plot third kernel density plot
kd3 purple')

...

The following examples show how to use each method in practice.

Method 1: Create One Kernel Density Plot

The following code shows how to create a kernel density plot for one dataset in R:

#create data
data 

#define kernel density
kd #create kernel density plot
plot(kd, main='Kernel Density Plot of Data')

The x-axis shows the values of the dataset and the y-axis shows the relative frequency of each value. The highest points in the plot show where the values occur most often.

Method 2: Create a Filled-In Kernel Density Plot

The following code shows how to create a kernel density plot with a specific border color and filled-in color:

#create data
data 

#define kernel density
kd #create kernel density plot
plot(kd)

#add color
polygon(kd, col='steelblue', border='black')

Method 3: Create Multiple Kernel Density Plots

The following code shows how to create multiple kernel density plots in one plot in R:

#create datasets
data1 #plot first kernel density plot
kd1 blue', lwd=2)

#plot second kernel density plot
kd2 red', lwd=2)

Note that we can use similar syntax to create as many kernel density plots in one chart as we’d like.

Additional Resources

The following tutorials explain how to create other common plots in R:

How to Plot Multiple Boxplots in R
How to Plot Multiple Histograms in R
How to Plot Multiple Lines in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories