bp.test | R Documentation |
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.
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,
...
)
x |
a vector of text to be drawn above each pair of groups |
... |
additional arguments or graphical parameters passed to
|
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
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 |
line |
lines at which to plot |
length , space |
the length of segment legs and space between text in
user units; if |
test |
the test to use for pairwise comparisons |
plot |
logical; if |
A list of user coordinates and text where each x
is drawn.
cuzick.test
; boxplot
; tplot
;
rawr:::coords
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.