Density Plots
Histograms, while commonly used, aren't always the go-to choice for visualizing data distribution due to their sensitivity to bin selection. Enter the density plot—a more nuanced alternative for comprehending and comparing data distributions. Unlike histograms, density plots offer an estimated probability distribution, transforming discrete histogram counts into a smoother, continuous probability representation.
Behold the code below, a testament to the elegance of the density plot in ggplot2.
library(ggplot2)
library(ggthemr)
library(datasets)
head(iris, 3)
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
ggthemr('fresh')
density = ggplot( data = iris, aes( x = Sepal.Length, fill = Species )) +
geom_density() +
ggtitle("Density Plot of Speal Length by Species")
density