Violin plot type

Description

Type function for violin plots, which are an alternative to box plots for visualizing continuous distributions (by group) in the form of mirrored densities.

Usage

type_violin(
  bw = "nrd0",
  joint.bw = c("mean", "full", "none"),
  adjust = 1,
  kernel = c("gaussian", "epanechnikov", "rectangular", "triangular", "biweight",
    "cosine", "optcosine"),
  n = 512,
  trim = FALSE,
  width = 0.9
)

Arguments

bw

the smoothing bandwidth to be used. The kernels are scaled such that this is the standard deviation of the smoothing kernel. (Note this differs from the reference books cited below.)

bw can also be a character string giving a rule to choose the bandwidth. See bw.nrd.
The default, “nrd0”, has remained the default for historical and compatibility reasons, rather than as a general recommendation, where e.g., “SJ” would rather fit, see also Venables and Ripley (2002).

The specified (or computed) value of bw is multiplied by adjust.

joint.bw character string indicating whether (and how) the smoothing bandwidth should be computed from the joint data distribution when there are multiple subgroups. The options are “mean” (the default), “full”, and “none”. Also accepts a logical argument, where TRUE maps to “mean” and FALSE maps to “none”. See the "Bandwidth selection" section below for a discussion of practical considerations.
adjust the bandwidth used is actually adjust*bw. This makes it easy to specify values like ‘half the default’ bandwidth.
kernel

a character string giving the smoothing kernel to be used. This must partially match one of “gaussian”, “rectangular”, “triangular”, “epanechnikov”, “biweight”, “cosine” or “optcosine”, with default “gaussian”, and may be abbreviated to a unique prefix (single letter).

“cosine” is smoother than “optcosine”, which is the usual ‘cosine’ kernel in the literature and almost MSE-efficient. However, “cosine” is the version used by S.
n

the number of equally spaced points at which the density is to be estimated. When n > 512, it is rounded up to a power of 2 during the calculations (as fft is used) and the final result is interpolated by approx. So it almost always makes sense to specify n as a power of two.

trim logical indicating whether the violins should be trimmed to the range of the data. Default is FALSE.
width numeric (ideally in the range [0, 1], although this isn’t enforced) giving the normalized width of the individual violins.

Details

See type_density for more details and considerations related to bandwidth selection and kernel types.

Examples

library("tinyplot")

# "violin" type convenience string
tinyplot(count ~ spray, data = InsectSprays, type = "violin")

# aside: to match the defaults of `ggplot2::geom_violin()`, use `trim = TRUE`
# and `joint.bw = FALSE`
tinyplot(count ~ spray, data = InsectSprays, type = "violin",
    trim = TRUE, joint.bw = FALSE)

# use flip = TRUE to reorient the axes
tinyplot(count ~ spray, data = InsectSprays, type = "violin", flip = TRUE)

# for flipped plots with long group labels, it's better to use a theme for
# dynamic plot resizing
tinytheme("clean")
tinyplot(weight ~ feed, data = chickwts, type = "violin", flip = TRUE)

# you can group by the x var to add colour (here with the original orientation)
tinyplot(weight ~ feed | feed, data = chickwts, type = "violin", legend = FALSE)

# dodged grouped violin plot example (different dataset)
tinyplot(len ~ dose | supp, data = ToothGrowth, type = "violin", fill = 0.2)

# note: above we relied on `...` argument passing alongside the "violin"
# type convenience string. But this won't work for `width`, since it will
# clash with the top-level `tinyplot(..., width = <width>)` arg. To ensure
# correct arg passing, it's safer to use the formal `type_violin()` option.
tinyplot(len ~ dose | supp, data = ToothGrowth, fill = 0.2,
    type = type_violin(width = 0.8))

# reset theme
tinytheme()