bp.test: Box plot tests

View source: R/plot_extra.R

bp.testR Documentation

Box plot tests

Description

Add pairwise tests to box plots with three or more groups. Using cuzick.test under-the-hood, performs test for all pairs of groups and draws results on an existing plot.

Usage

bp.test(x, ...)

## S3 method for class 'formula'
bp.test(
  formula,
  data,
  which = NULL,
  at = NULL,
  line = NULL,
  length = NULL,
  space = NULL,
  test = wilcox.test,
  plot = TRUE,
  ...
)

## Default S3 method:
bp.test(
  x,
  which = NULL,
  at = NULL,
  line = NULL,
  length = NULL,
  space = NULL,
  test = wilcox.test,
  plot = TRUE,
  ...
)

Arguments

x

a vector of text to be drawn above each pair of groups

...

additional arguments or graphical parameters passed to segments (e.g., col.line, lty, lwd) or text (e.g., col.text, cex, font)

formula

a formula of the form response ~ group where response is a numeric variable and group is a factor-like variable with three or more unique values (groups)

data

an optional matrix or data frame (or similar: see model.frame) containing the variables in formula; by default, the variables are taken from environment(formula)

which

an index vector of the values to display

at

numeric vector giving the x-axis locations of each group having the same length as x or the number of pairs; alternatively, a list of pairs of x-axis locations for each x; see examples

line

lines at which to plot test results; if NULL, these will be calculated; if length 1, the calculated lines will be shifted by line

length, space

the length of segment legs and space between text in user units; if NULL (default), values are calculated based on the plotting region

test

the test to use for pairwise comparisons

plot

logical; if TRUE, an existing figure will be annotated with the tests; if FALSE, all tests will be returned but not plotted

Value

A list of user coordinates and text where each x is drawn.

See Also

cuzick.test; boxplot; tplot; rawr:::coords

Examples

## basic usage
boxplot(mpg ~ gear, mtcars)
x <- bp.test(mpg ~ gear, mtcars)

## select which tests to show
boxplot(mpg ~ gear, mtcars)
bp.test(x$text[c(1, 3)], at = list(1:2, 2:3), line = 0:1)

## re-order and adjust alignment of tests
boxplot(mpg ~ gear, mtcars, at = c(1, 3, 4), ylim = c(10, 55))
bp.test(mpg ~ gear, mtcars, at = c(1, 3, 4), which = c(1, 3, 2), line = -5)

## customize with graphical parameters passed to segments and/or text
op <- par(mar = par('mar') + c(0, 0, 5, 0))
boxplot(mpg ~ gear, mtcars)
bp.test(
  mpg ~ gear, mtcars, line = 0:2 * 3,
  ## passed to text
  col.text = c(2, 1, 1), cex = 1.5, font = c(4, 1, 1), pos = 3,
  ## passed to segments
  col.line = 'red', lwd = 2, lty = 2
)
par(op)


op <- par(mar = par('mar') + c(0, 0, 3, 0))
tplot(mpg ~ interaction(vs, am), mtcars, show.n = FALSE)
bp.test(mpg ~ interaction(vs, am), mtcars)

tplot(mpg ~ interaction(vs, am), mtcars, show.n = FALSE)
bp.test(mpg ~ interaction(vs, am), mtcars, which = c(1, 3, 5))

at <- bp.test(
  mpg ~ interaction(vs, am), mtcars, which = 6, line = 4,
  col = 'red', fg = 'red', lty = 2, font = 2, test = t.test
)
points(at[1], at[2], pch = 1, cex = 5, col = 'red', xpd = NA)
par(op)


## also works for barplots
bp <- barplot(with(mtcars, tapply(mpg, gear, mean)), ylim = c(0, 30))
bt <- bp.test(mpg ~ gear, mtcars, at = bp, line = -1, test = t.test)
bp.test(bt$text[2], at = bp[c(1, 3)], line = -3, col.text = 2, col.line = 2)


## use default method for more control
boxplot(mpg ~ am + vs, mtcars, axes = FALSE, ylim = c(10, 55))
axis(2, las = 1)
box(bty = 'l')
at <- bp.test(letters[1:6], at = 1:4, line = -7)
points(at, cex = 3, col = 1:6, xpd = NA)


## special cases
sp <- split(mtcars$mpg, interaction(mtcars$cyl, mtcars$am))
pv <- sapply(sp[-1], function(x) pvalr(t.test(sp[[1]], x)$p.value))
op <- par(mar = c(5, 5, 8, 2))
tplot(sp, show.n = FALSE)
bp.test(pv, 1:6)

pairs <- list(1:2, 3:4, 5:6)
pv <- sapply(pairs, function(ii)
  pvalr(t.test(sp[[ii[1]]], sp[[ii[2]]])$p.value))
tplot(sp, show.n = FALSE)
bp.test(pv, at = pairs, length = 1, space = 2, cex = 1.5)
par(op)


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.