Violin

Layers are declared with the DRAW clause. Read the documentation for this clause for a thorough description of how to use it.

Violin plots display the distribution of a single continuous variable for multiple groups. The violins are mirrored kernel density estimates, similar to the density layer, but organised as distinct groups.

Aesthetics

The following aesthetics are recognised by the violin layer.

Required

  • x: Position on the x-axis (categorical).
  • y: Value on the y-axis for which to compute density.

Optional

  • stroke: The colour of the contour lines.
  • fill: The colour of the inner area.
  • colour: Shorthand for setting stroke and fill simultaneously.
  • opacity: The opacity of the colours.
  • linewidth: The width of the contour lines.
  • linetype The dash pattern of the contour line.

Settings

  • position: Determines the position adjustment to use for the layer (default is 'dodge')
  • bandwidth: A numerical value setting the smoothing bandwidth to use. If absent (default), the bandwidth will be computed using Silverman’s rule of thumb.
  • adjust: A numerical value as multiplier for the bandwidth setting, with 1 as default.
  • kernel: Determines the smoothing kernel shape. Can be one of the following:
    • 'gaussian' (default)
    • 'epanechnikov'
    • 'triangular'
    • 'rectangular' or 'uniform'
    • 'biweight' or 'quartic'
    • 'cosine'
  • width: Relative width of the violins. Defaults to 0.9.

Data transformation

A violin layer uses the same computation as a density layer. See the density data transformation section for details. The major difference between a violin layer and a density layer is just the matter of display.

Properties

  • weight: If mapped, it sets the relative contribution of an observation to the density estimate.

Calculated statistics

  • density: The estimated probability density per point on the grid. The total area of a single density curve adds up to 1.
  • intensity: Also termed ‘probability intensity estimation’, it is the precursor of the density variable. Specifically it is the same as the density without normalisation. You can use REMAPPING intensity AS offset if you want to reflect differences in group sizes.

Default remappings

  • density AS offset: By default the offsets around a centerline reflect the computed density.

Examples

A typical violin plot.

VISUALISE species AS x, bill_dep AS y FROM ggsql:penguins
  DRAW violin

The adjust setting controls the smoothing.

VISUALISE species AS x, bill_dep AS y FROM ggsql:penguins
  DRAW violin SETTING adjust => 0.1

To more clearly indicate differences in group sizes, you can use the intensity computed variable. Note that we have fewer (n=68) Chinstrap penguins than Adelie (n=152) or Gentoo (n=124) penguins.

VISUALISE species AS x, bill_dep AS y FROM ggsql:penguins
  DRAW violin REMAPPING intensity AS offset

You can combine groups to expand the categories.

VISUALISE species AS x, bill_dep AS y, island AS fill FROM ggsql:penguins
  DRAW violin