Most basic violin plot with ggplot2



A violin plot allows to compare the distribution of several groups by displaying their densities. See how to build it with R and ggplot2 below. Learn more about violin chart theory in data-to-viz.

Violin Section Violin theory

Basic violin plot


Note on input format


Ggplot2 expects input data to be in a long format: each row is dedicated to one observation. Your input needs 2 column:
- a categorical variable for the X axis: it needs to be have the class factor
- a numeric variable for the Y axis: it needs to have the class numeric

→ From wide format

In this case we need to reformat the input. This is possible thanks to the gather() function of the tidyr library that is part of the tidyverse.

Sepal.Length Sepal.Width Petal.Length Petal.Width
5.1 3.5 1.4 0.2
4.9 3.0 1.4 0.2
4.7 3.2 1.3 0.2
4.6 3.1 1.5 0.2

Related chart types


Violin
Density
Histogram
Boxplot
Ridgeline



Contact

This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message on Twitter, or send an email pasting yan.holtz.data with gmail.com.

Github Twitter