Graph info

Should I use this graph?


This graph requires:

✅ a numeric (continuous) variable

Description

Violin plots will display the distribution of a single numeric (continuous) variable. If you’re trying to view the shape of a continuous distribution across the groups or levels of a categorical variable, check out grouped violin plots.

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:

source: Rorschach test, Wikipedia

Getting set up

PACKAGES:

Install packages.

Code
install.packages("palmerpenguins")
library(palmerpenguins)
library(ggplot2)

DATA:

Artwork by @allison_horst

The penguins data.

Code
penguins <- palmerpenguins::penguins
glimpse(penguins)
Rows: 344
Columns: 8
$ species           <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island            <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm    <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm     <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g       <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex               <fct> male, female, female, NA, female, male, female, male…
$ year              <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…

The grammar

CODE:

Create labels with labs()

Initialize the graph with ggplot() and provide data

  • Place an empty string ("") in the x argument

Map and empty string ("") to the x and flipper_length_mm to the y

Add geom_violin()

Code
labs_violin <- labs(
  title = "Adult foraging penguins",
  subtitle = "Distribution of flipper length",
  x = "",
  y = "Flipper length (millimeters)")
ggp2_violin <- ggplot(data = penguins,
       aes(x = '', 
           y = flipper_length_mm)) +
       geom_violin() 
ggp2_violin + 
  labs_violin

GRAPH: