Density comparison chart in R

Sample data

Consider the following data from palmerpenguins package, where the continuous variable is the flipper length of the pinguins and the categorial the penguin species.

# install.packages("palmerpenguins")

x <- penguins$flipper_length_mm
g <- as.factor(penguins$species)

p <- data.frame(Flipper.length = x, species = g)
p <- na.omit(p)

Density comparison with lines

It is possible to estimate the kernel density for each penguin species with the tapply function. Then, you can add the three densities to the same plot. Note that you can select a different kernel and a smoothing parameter than de defaults.

# install.packages("car")

# Densities for each group
dens <- tapply(p$Flipper.length, p$species, density)

# Plot
plot(dens$Adelie, xlim = c(min(p$Flipper.length) - 10,
                           max(p$Flipper.length) + 25),
     main = "Density comparison")
lines(dens$Chinstrap, col = 2)
lines(dens$Gentoo, col = 3)

# Legend
legend("topright", legend = levels(p$species),
       lty = 1, col = 1:3)

Density comparison chart in R

The densityPlot function

densityPlot function from car package

An alternative is using densityPlot from car package. Note that this function uses an adaptive-kernel estimate by default instead of a fixed-bandwidth kernel estimate. If you want the same estimation of the previous section set method = "kernel" as argument.

# install.packages("car")

densityPlot(p$Flipper.length ~ p$species,
            legend = list(title = "Species"),
            xlab = "Flipper length",
            xlim = c(160, 280))

The function

Other option is using the function from sm library. If you set model = "equal" a permutation test of equality will be computed and boostrap bands will be displayed if the number of groups is two.

# install.packages("sm")
library(sm) = p$Flipper.length,
                   group = p$species,
                   model = "equal") function from sm package

Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon
Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon

See also