6 Violin plots
6.1 Description
Violin plots show data distribution and probability density. They are like box plots but have a rotated kernel density plot on each side. We can use them to compare data distribution across categories.
The violin plot is essentially a mirrored density plot, in which a smoothed distribution is plotted symmetrically across the x
or y
axis, creating a Rorschach-like inkblot area for the distribution:
6.2 Set up
PACKAGES:
Install packages.
show/hide
install.packages("palmerpenguins")
library(palmerpenguins)
library(ggplot2)
DATA:
The penguins
data.
show/hide
<- palmerpenguins::penguins
penguins glimpse(penguins)
#> Rows: 344
#> Columns: 8
#> $ species <fct> Adelie, Adelie, Adelie…
#> $ island <fct> Torgersen, Torgersen, …
#> $ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, …
#> $ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, …
#> $ flipper_length_mm <int> 181, 186, 195, NA, 193…
#> $ body_mass_g <int> 3750, 3800, 3250, NA, …
#> $ sex <fct> male, female, female, …
#> $ year <int> 2007, 2007, 2007, 2007…
6.3 Grammar
CODE:
Create labels with labs()
Initialize the graph with ggplot()
and provide data
- Place an empty string (
""
) in thex
argument
Map and empty string (""
) to the x
and flipper_length_mm
to the y
Add geom_violin()
show/hide
<- labs(
labs_violin title = "Adult foraging penguins",
subtitle = "Distribution of flipper length",
x = "",
y = "Flipper length (millimeters)")
<- ggplot(data = penguins,
ggp2_violin aes(x = '',
y = flipper_length_mm)) +
geom_violin()
+
ggp2_violin labs_violin
GRAPH: