| type_violin | R Documentation |
Type functions for violin plots, which are an alternative to box
plots for visualizing continuous distributions (by group) in the form of
mirrored densities. type_violin() draws the smooth outline of each
density, while type_sina() scatters the underlying observations as points
within the same outline (similar to a beeswarm plot).
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,
lighten = TRUE,
singletons = c("warn", "drop", "none")
)
type_sina(
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,
method = c("quasirandom", "random"),
singletons = c("keep", "warn", "drop")
)
bw |
the smoothing |
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 |
adjust |
the bandwidth used is actually |
kernel |
a character string giving the smoothing kernel to be used. This
must partially match one of
|
n |
the number of equally spaced points at which the density is
to be estimated. When |
trim |
logical indicating whether the densities should be trimmed to
the range of the data. Default is |
width |
numeric (ideally in the range |
lighten |
logical. Should the fills use a lighter, opaque tint of the
series colour(s)? Default is |
singletons |
character string indicating what to do with singleton
groups, i.e. combinations of The remaining option differs by type, as does the default.
|
method |
character string giving how |
See type_density for more details and considerations related to
bandwidth selection and kernel types.
A sina plot (Sidiropoulos et al., 2018) is closely related to a beeswarm
plot, but is arguably the more principled of the two. Both spread a
group's observations sideways to expose its shape. A beeswarm does so by
packing points until they no longer collide, which makes its width an
artefact of the rendering: change the symbol size or the device and the
swarm changes shape. A sina instead displaces each point by the kernel
density at its own y value, so its width is a property of the data,
stable across devices and comparable between groups. The trade-off is
occlusion: a beeswarm guarantees that no point hides another, whereas a
sina accepts the occasional overlap. If you need collision-free packing,
use a dedicated package such as beeswarm.
Sidiropoulos, N., Sohi, S. H., Pedersen, T. L., Porse, B. T., Winther, O., Rapin, N., and Bagger, F. O. (2018). SinaPlot: An Enhanced Chart for Simple and Truthful Representation of Single Observations Over Multiple Classes. Journal of Computational and Graphical Statistics, 27(3), 673-676. Available: https://doi.org/10.1080/10618600.2017.1366914
type_boxplot, type_density and type_ridge for the other ways of displaying a distribution by group, and type_jitter for displacing points without reference to a density.
# "violin" type convenience string
tinyplot(weight ~ feed, data = chickwts, type = "violin")
# to match the defaults of `ggplot2::geom_violin()`, use `trim = TRUE` and
# `joint.bw = FALSE`
tinyplot(
weight ~ feed, data = chickwts,
# type = type_violin(trim = TRUE, joint.bw = FALSE) # same but see final ex.
type = "violin", trim = TRUE, joint.bw = FALSE
)
# For flipped violin plots, it's usually better to use a dynamic theme to
# accommodate (horizontal) y-axis labels
tinyplot(
weight ~ feed, data = chickwts, type = "violin", flip = TRUE,
theme = "dynamic" # or "clean(2)", "classic", "minimal", etc.
)
# 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")
# the "sina" type shows the observations themselves, rather than a smooth
# outline drawn around them
tinyplot(weight ~ feed, data = chickwts, type = "sina")
# layering a sina on top of a violin lines up exactly, since the points are
# displaced by the violin's own half-width
tinyplot(weight ~ feed, data = chickwts, type = "violin")
tinyplot_add(type = "sina", pch = 16, col = "black")
# unlike `type_violin()`, `type_sina()` supports a continuous `by` variable;
# it colours the points rather than splitting them into groups
tinyplot(
Sepal.Length ~ Species | Petal.Width, data = iris,
type = "sina", pch = 16
)
# note: above we relied on `...` argument passing alongside the type
# convenience strings. 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 functional type.
tinyplot(
len ~ dose | supp, data = ToothGrowth,
type = type_violin(width = 0.75)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.