panel.beeswarm: Panel function for beeswarm plots

View source: R/panel_beeswarm.R

panel.beeswarmR Documentation

Panel function for beeswarm plots

Description

Panel function for beeswarm plots. This panel function works essentially like a stripplot, but instead of randomly scattering a variable produces a regular, grid-like pattern of points. Currently only the X variable is transformed. Continuously distributed data points can be optionally discretized, both for X and for Y variable.

Usage

panel.beeswarm(
  x,
  y,
  groups = NULL,
  subscripts = NULL,
  bin_x = FALSE,
  bin_y = FALSE,
  breaks_x = 10,
  breaks_y = 10,
  spread = 0.1,
  return_coords = FALSE,
  ...
)

Arguments

x, y

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

groups

Passed to panel.xyplot().

subscripts

Passed to panel.xyplot().

bin_x

(Logical) If X variable is to be binned or not (default FALSE).

bin_y

(Logical) If Y variable is to be binned or not (default FALSE).

breaks_x

(numeric) Number of breaks for x variable if bin_x = TRUE. Alternatively a numeric vector supplying the breaks for bins.

breaks_y

(numeric) Number of breaks for y variable if bin_y = TRUE. Alternatively a numeric vector supplying the breaks for bins.

spread

(numeric) Scalar indicating how much values should be spread.

return_coords

(logical) if TRUE returns a list with the calculated x- and y-coordinates for each data point

...

Further arguments passed to xyplot

Author(s)

modified after a function from Walmes Zeviani, walmes@ufpr.br, panel.beeswarm from package wzRfun. Idea based on the package beeswarm.

Examples


library(lattice)

# simple example
df <- data.frame(
  Y = sample(1:10, 60, replace = TRUE), 
  X = factor(rep(1:3, each = 20))
)

xyplot(Y ~ X, df, groups = X, panel = panel.beeswarm)

# but with continuous Y variable, it doesn't work as expected
df$Y <- rnorm(60)
xyplot(Y ~ X, df, groups = X, panel = panel.beeswarm)

# for this purpose we can bin the Y variable into groups
xyplot(Y ~ X, df, groups = X, 
  panel = function(x, y, ...) {
    panel.beeswarm(x, y, bin_y = TRUE, breaks_y = 10, ...)
})

# the breaks for Y bins are computed for each panel independently.
# we can also supply fixed bins via the 'breaks_y' argument
# to obtain the same binning for each panel
xyplot(Y ~ factor(rep(1, length(Y))) | X, df, groups = X,
  panel = function(x, y, ...) {
    panel.beeswarm(x, y, bin_y = TRUE, 
      breaks_y = seq(-4, 4, length.out = 20), ...)
})


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