Learn R Programming

beeswarm (version 0.1.1)

beeswarm: Bee swarm plot

Description

Create a bee swarm plot. A bee swarm plot is a one-dimensional scatter plot similar to stripchart, but with a different set of methods to separate coincident points such that each point is visible. Also, beeswarm introduces additional features unavailable in stripchart, such as the ability to control the color and plotting character of each point.

Usage

## S3 method for class 'formula':
beeswarm(formula, data = NULL, subset, na.action = NULL, 
         pwpch = NULL, pwcol = NULL, pwbg = NULL, dlab, glab, ...)

## S3 method for class 'default': beeswarm(x, method = c("swarm", "center", "hex", "square"), vertical = TRUE, horizontal = !vertical, cex = 1, spacing = 1, breaks = NULL, labels, at = NULL, pch = par('pch'), col = par('col'), bg = NA, pwpch = NULL, pwcol = NULL, pwbg = NULL, do.plot = TRUE, add = FALSE, log = FALSE, xlim = NULL, ylim = NULL, dlim = NULL, glim = NULL, xlab = NULL, ylab = NULL, dlab = "", glab = "", ...)

Arguments

formula
A formula, such as y ~ grp, where y is a numeric vector of data values to be split into groups according to the grouping variable grp (usually a factor).
data
A data.frame (or list) from which the variables in formula should be taken.
subset
An optional vector specifying a subset of observations to be used.
na.action
A function which indicates what should happen when the data contain NAs. The default is to quietly ignore missing values in either the response or the group.
x
A numeric vector, or a data frame or list of numeric vectors, each of which is plotted as an individual swarm.
method
Method for arranging points (see Details).
vertical, horizontal
Orientation of the plot. horizontal takes precedence if both are specified.
cex
Size of points relative to the default given by par("cex"). Unlike other plotting functions, this must be a single value.
spacing
Relative spacing between points.
breaks
Breakpoints (optional). If NULL, breakpoints are chosen automatically. If NA, bins are not used (similar to stripchart with method = "stack").
labels
Labels for each group. By default, these are inferred from the data.
at
Numeric vector giving the locations where the swarms should be drawn; defaults to 1:n where n is the number of groups.
pch, col, bg
Plotting characters and colors, specified by group (see Details).
pwpch, pwcol, pwbg
Point-wise plotting characters and colors, specified for each data point (see Details).
do.plot
Draw a plot?
add
Add to an existing plot?
log
Use a logarithmic scale on the data axis?
xlim, ylim
Limits of the plot.
dlim, glim
An alternative way to specify limits (see Details).
xlab, ylab
Axis labels.
dlab, glab
An alternative way to specify axis labels (see Details).
...
Further arguments passed to plot.

Value

  • A data frame with plotting information, invisibly.

Details

Several methods for placing the points are available; each method uses a different algorithm to avoid overlapping points.

The default method, swarm, places points in increasing order. If a point would overlap an existing point, it is shifted sideways (along the group axis) by a minimal amount sufficient to avoid overlap. breaks is ignored.

The other three methods first discretize the values along the data axis, in order to create more efficient packing: square places the points on a square grid, whereas hex uses a hexagonal grid. center uses a square grid to produce a symmetric swarm. By default, the number of breakpoints for discretization is determined by a combination of the available plotting area and the plotting character size. The discretization of the data can be explicitly controlled using breaks. If breaks is set to NA, the data will not be grouped into intervals; this may be a sensible option if the data is already discrete.

In contrast to most other plotting functions, changing the size of the graphics device will often change the position of the points.

The plotting characters and colors can be controlled in two ways. First, the arguments pch, col and bg can specify plotting characters and colors in the same way as stripchart and boxplot: in short, the arguments apply to each group as a whole (and are recycled if necessary).

Alternatively, the characteristics of each individual data point can be controlled using pwpch, pwcol, and pwbg, which override pch, col and bg if these are also specified. These arguments can be specified as a list or vector. If supplied using the formula method, the arguments can be specified as part of the formula interface; i.e. they are affected by data and subset.

The dlab and glab labels may be used instead of xlab and ylab if those are not specified. dlab applies to the continuous data axis (the Y axis unless horizontal is TRUE); glab to the group axis. Likewise, dlim and glim can be used to specify limits of the axes instead of xlim or ylim.

This function is intended to be mostly compatible with calls to stripchart or boxplot. Thus, code that works with these functions should work with beeswarm with minimal modification.

See Also

stripchart, boxplot

Examples

Run this code
## One of the examples from 'stripchart'
  beeswarm(decrease ~ treatment,
    data = OrchardSprays, log = TRUE, 
    pch = 16, col = rainbow(8))

  ## One of the examples from 'boxplot', with a beeswarm overlay
   boxplot(len ~ dose, data = ToothGrowth,
              main = "Guinea Pigs' Tooth Growth",
              xlab = "Vitamin C dose mg",
              ylab = "Tooth length")  
   beeswarm(len ~ dose, data = ToothGrowth, col = 2, add = TRUE)

  ## Compare the 4 methods
  op <- par(mfrow = c(2,2))
  for (m in c("swarm", "center", "hex", "square")) {
    beeswarm(len ~ dose, data = ToothGrowth, method = m, main = m)
  }
  par(op)

  ## Demonstrate the use of 'pwcol'
  data(breast)
  beeswarm(time_survival ~ event_survival, data = breast,
    method = 'swarm',
    pch = 16, pwcol = as.numeric(ER),
    xlab = '', ylab = 'Follow-up time (months)',
    labels = c('Censored', 'Metastasis'))
  legend('topright', legend = levels(breast$ER),
    title = 'ER', pch = 16, col = 1:2)

  ## The list interface
  distributions <- list(runif = runif(100, min = -3, max = 3), 
                        rnorm = rnorm(100),
                        rlnorm = rlnorm(100, sdlog = 0.5))
  beeswarm(distributions, col = 2:4)

  ## Demonstrate 'pwcol' with the list interface 
  myCol <- lapply(distributions, function(x) cut(x, breaks = quantile(x), labels = FALSE))
  beeswarm(distributions, pch = 16, pwcol = myCol)
  legend('bottomright', legend = 1:4, pch = 16, col = 1:4, title = 'Quartile')

Run the code above in your browser using DataLab