VISUALISE species AS x, bill_len AS y FROM ggsql:penguins
DRAW violinViolin plots
Violin plots display the distribution of a single continuous variable, much like density plots. They are displayed differently, with mirrored densities and separated groups. The densities are mirrored, and each group has its own center.
Code
Explanation
- The
VISUALISE ... FROM ggsql:penguinsloads the built-in penguins dataset. species AS xsets a categorical variable to separate different groups.bill_len AS ysets the numeric variable to use for density estimation.DRAW violingives instructions to draw the violin layer.
Variations
Dodging
You can refine groups beyond the axis categorical variable, and the violins will be displayed in a dodged way.
However, dodging might be unproductive or counterintuitive in some cases. For example if we double-encode groups, like species as both x and colour in the plot below, dodging looks bad.
We can disable the dodging by setting position => 'identity'.
Half-violins
A ridgeline plot is a plot where violins are placed horizontally without mirroring. To place violins horizontally, we just need to swap the x and y variables.
To get ridges, we can set side => 'top'.
To display variables split across two different groups, you can combine two halves to get an asymmetrical violin. Here we’re using the FILTER clause to draw separate layers for the ‘male’ and ‘female’ groups.
With individual datapoints
It might be tempting to combine the display of individual datapoints with a violin to accentuate the distribution. The datapoints can be jittered by setting position => 'jitter'.
VISUALISE species AS x, bill_len AS y FROM ggsql:penguins
DRAW point SETTING position => 'jitter'
DRAW violin SETTING opacity => 0.3This can be made even more clear by also using the distrion => 'density' setting.