panel.violinscatter: Combine violin plot and scatter plot

View source: R/panel_violinscatter.R

panel.violinscatterR Documentation

Combine violin plot and scatter plot

Description

Custom lattice panel function that combines violin and scatter plot to a violin-shaped scatter plot. By default, the underlying violin is plotted with the generic 'panel.violin' function. On top of that are the points drawn from which the violin was calculated. To achieve this, each point is jittered randomly on the X (Y) axis according to the density estimate of the Y (X) axis.

Usage

panel.violinscatter(
  x,
  y,
  groups = NULL,
  subscripts = NULL,
  box.ratio = 1,
  box.width = box.ratio/(1 + box.ratio),
  horizontal = TRUE,
  col = NULL,
  alpha = 0.3,
  border = "white",
  lty = NULL,
  lwd = NULL,
  varwidth = FALSE,
  bw = NULL,
  adjust = NULL,
  kernel = NULL,
  window = NULL,
  width = NULL,
  n = 128,
  from = NULL,
  to = NULL,
  cut = NULL,
  na.rm = TRUE,
  identifier = "violinscatter",
  group.number = NULL,
  ...
)

Arguments

x, y

(numeric, factor) X and Y variables passed to panel.xyplot().

groups

grouping variable passed down from xyplot (does not need to be specified).

subscripts

subscripts passed down from xyplot (does not need to be specified).

box.ratio

(numeric) ratio of the thickness of each violin and inter violin space.

box.width

(numeric) thickness of the violins in absolute units; overrides box.ratio. See 'panel.violin' for details.

horizontal

(logical) If FALSE, the plot is ‘transposed’ in the sense that the behaviours of x and y are switched. See documentation of 'bwplot' for a full explanation.

col, alpha, border, lty, lwd, varwidth

graphical parameters controlling the violin. Defaults are taken from the "plot.polygon" settings.

bw, adjust, kernel, window, width, n, from, to, cut, na.rm

arguments to 'density', passed on as appropriate.

identifier

A character string that is prepended to the names of grobs that are created by this panel function.

group.number

internal parameter only used when function is called as panel.groups argument from within panel.superpose. Does not need to be specified manually.

...

other arguments passed to the function

Examples

# use singer data from lattice
library(lattice)
data(singer)
singer$voice.part <- gsub(" [12]", "", as.character(singer$voice.part))
singer$voice.part <- factor(singer$voice.part, c("Soprano", "Alto", "Tenor", "Bass"))

# example with grouping
xyplot(height ~ voice.part, singer, groups = voice.part,
  horizontal = FALSE, pch = 19,
  panel = function(x, y, ...) {
    panel.violinscatter(x, y, ...)
})

# same plot but horizontal orientation
xyplot(voice.part ~ height, singer, groups = voice.part,
  horizontal = TRUE, pch = 19,
  panel = function(x, y, ...) {
    panel.violinscatter(x, y, ...)
})

# example with more and non-discrete data points
df <- data.frame(
  sample = factor(rep(c("A", "B", "C"), each = 300)),
  variable = c(rnorm(300, 0, 3), rnorm(300, 1, 2), rnorm(300, 3, 3))
)

xyplot(variable ~ sample, df,
  horizontal = FALSE, pch = 19, cex = 0.4,
  panel = function(x, y, ...) {
    panel.violinscatter(x, y, ...)
})

m-jahn/lattice-tools documentation built on Dec. 3, 2023, 11:14 p.m.