Skip to contents

One row per feature, one violin per group. A box plot hides bimodality, which in single-cell data is usually the whole story: a gene expressed in half a cluster and silent in the other half has the same median as one expressed weakly everywhere. The violin shows the shape, and stacking rows on a shared x lets a marker panel be read down the page.

Usage

violin(
  data,
  grid,
  density,
  grids = NULL,
  median = NULL,
  features = NULL,
  groups = NULL,
  group_colors = NULL,
  violin_width = 0.85,
  scale_per_violin = FALSE,
  show_median = TRUE,
  show_feature_labels = TRUE,
  show_value_axis = TRUE,
  theme = NULL,
  width = NULL,
  height = NULL,
  element_id = NULL,
  violin_aspect = 0.8
)

Arguments

data

A data frame with feature and group key columns, one row per violin, in the order to draw them.

grid

Numeric vector, the shared evaluation grid, ascending.

density

Numeric matrix, violins x grid, of density values.

grids

Optional numeric matrix, features x grid, giving each feature its own y range. Without it every row shares grid, which lets one highly expressed feature compress the rest into flat lines.

median

Optional numeric vector, one median per violin, drawn as a tick.

features, groups

Character vectors fixing the row and column order. Factor feature / group columns supply them from their levels.

group_colors

One hex colour per group. NULL uses the categorical palette.

violin_width

Fraction of a cell's width the widest violin fills, in (0, 1]. When rows are short next to the column width (many features, or a short or wide widget), violin_aspect can make it fill less.

scale_per_violin

Scale each violin to its own maximum rather than the row's. Per-row is the default so groups stay comparable within a feature.

show_median, show_feature_labels

Toggle the median tick and row labels.

show_value_axis

Draw each row's value axis on the right, with the highest value of its grid and, on rows tall enough for two labels, the lowest.

theme

Optional named list of theme overrides. Its padding entry is the space in px between the edge of the widget and the plot: one number for every side, or numbers named top, right, bottom and left in a list or vector, a side left out keeping the default of 10. Its categorical entry is an ordered palette: the colours go to the groups in order, and names on the colours are not matched to them. A colour given as a CSS custom property, such as background = "var(--bg)", takes the value the page gives it when the plot draws. After the page changes the property, what the plot drew on a canvas keeps the old colour until it draws again: resize the widget or render it again.

width, height

Widget dimensions (any valid CSS size).

element_id

Optional explicit DOM id.

violin_aspect

The largest width of the widest violin, as a multiple of its row height. A violin much wider than its row is tall reads as a flat band. The cap never makes a violin narrower than four times the label text size (44 px at the default font size), so with many features the violins stay visible; nor wider than violin_width of its cell. NULL turns the cap off.

Value

An htmlwidget object.

Details

The widget draws densities, it does not estimate them. Each violin arrives as a vector of density values on a shared grid, because kernel bandwidth choice changes what the figure claims and belongs with the data. violin_density() computes them from raw values with stats::density().

Examples

d <- violin_density(list(`CD3D|T` = rnorm(50, 2), `CD3D|B` = rnorm(50, 0)))
violin(d$data, grid = d$grid, density = d$density, median = d$median)