# How to Create Kernel Density Plots in R

In the realm of data visualization, kernel density plots are a powerful tool to represent the distribution of data. This type of plot creates a smooth curve that estimates the probability density function of a given variable. It’s a non-parametric way of estimating the probability density function of a random variable, helpful in understanding the underlying distribution of the data.

The primary use of kernel density plots is to visualize and better understand the underlying structure of the data. These plots can offer more insights than a simple histogram, allowing us to understand the distribution at a deeper level.

R programming language provides several packages that facilitate creating kernel density plots. In this article, we will explore how to create kernel density plots in R, using base R functions and the ggplot2 package.

## Creating Kernel Density Plots with Base R

In the base R environment, you can use the density() function to estimate kernel density, and the plot() function to plot it. Let’s first look at how to create a simple kernel density plot with base R.

# Create a normal distribution with 1000 data points
set.seed(123)
data <- rnorm(1000)

# Estimate the kernel density
dens <- density(data)

# Create a kernel density plot
plot(dens, main = "Kernel Density Plot", xlab = "Values")

Here, we first create a normal distribution of 1000 data points using rnorm(). Then we use the density() function to estimate the kernel density of this data. Finally, we create a kernel density plot using the plot() function.

## Customizing Kernel Density Plots in Base R

You can customize the appearance of the kernel density plot using various arguments in the plot() function. For example, you can change the line color and line type as follows:

plot(dens, main = "Kernel Density Plot", xlab = "Values", col = "blue", lty = "dotted")

You can also fill the area under the curve:

plot(dens, main = "Kernel Density Plot", xlab = "Values", col = "blue")
polygon(dens, col = "lightblue")

Here, we use the polygon() function to fill the area under the curve.

## Creating Kernel Density Plots with ggplot2

ggplot2 is a powerful and flexible R package for creating plots. You can use the geom_density() function to create kernel density plots. Let’s look at how to create a simple kernel density plot with ggplot2.

# Load the ggplot2 package
library(ggplot2)

# Create a kernel density plot
ggplot(data.frame(data), aes(x = data)) +
geom_density(fill = "lightblue") +
labs(title = "Kernel Density Plot", x = "Values")

In this case, we use the ggplot() function to initialize the ggplot object, geom_density() to add a layer for the kernel density plot, and labs() to add labels to the plot.

## Customizing Kernel Density Plots in ggplot2

You can customize the appearance of the kernel density plot using various arguments in the geom_density() function. For example, you can change the line color, fill color, and line type as follows:

ggplot(data.frame(data), aes(x = data)) +
geom_density(colour = "black", fill = "lightblue", linetype = "dashed") +
labs(title = "Kernel Density Plot", x = "Values")

You can also add a rug plot at the bottom, which displays the individual data points as tick marks:

ggplot(data.frame(data), aes(x = data)) +
geom_density(colour = "black", fill = "lightblue") +
geom_rug() +
labs(title = "Kernel Density Plot", x = "Values")

## Comparing Distributions with Kernel Density Plots

Kernel density plots are especially useful when comparing multiple distributions. In ggplot2, you can do this by mapping a categorical variable to the fill aesthetic. For example:

# Create two normal distributions with 1000 data points each
set.seed(123)
group_A <- rnorm(1000, mean = 0)
group_B <- rnorm(1000, mean = 1)

# Combine the data into a data frame
data <- data.frame(
Value = c(group_A, group_B),
Group = rep(c("A", "B"), each = 1000)
)

# Create a kernel density plot
ggplot(data, aes(x = Value, fill = Group)) +
geom_density(alpha = 0.5) +
labs(title = "Comparing Distributions with Kernel Density Plots", x = "Values")

In this example, we create two normal distributions with different means, combine them into a data frame, and create a kernel density plot that compares the two distributions.

## Conclusion

Kernel density plots are a useful tool for understanding the underlying distribution of data, and R provides flexible functions for creating these plots. While the base R functions can create basic kernel density plots, the ggplot2 package offers more flexibility and customization options.

Posted in RTagged