stat_multcomp {ggpmisc} | R Documentation |
Labels for pairwise multiple comparisons
Description
stat_multcomp
fits a linear model by default with stats::lm()
but alternatively using other model fit functions. The model is passed to
function glht()
from package 'multcomp' to fit Tukey, Dunnet or other
pairwise contrasts and generates labels based on adjusted
P-values.
Usage
stat_multcomp(
mapping = NULL,
data = NULL,
geom = NULL,
position = "identity",
...,
formula = NULL,
method = "lm",
method.args = list(),
contrasts = "Tukey",
p.adjust.method = NULL,
small.p = getOption("ggpmisc.small.p", default = FALSE),
adj.method.tag = 4,
p.digits = 3,
label.type = "bars",
fm.cutoff.p.value = 1,
mc.cutoff.p.value = 1,
mc.critical.p.value = 0.05,
label.y = NULL,
vstep = NULL,
output.type = NULL,
na.rm = FALSE,
orientation = "x",
parse = NULL,
show.legend = FALSE,
inherit.aes = TRUE
)
Arguments
mapping |
The aesthetic mapping, usually constructed with
|
data |
A layer specific dataset, only needed if you want to override the plot defaults. |
geom |
The geometric object to use to display the data. |
position |
The position adjustment to use for overlapping points on this layer. |
... |
other arguments passed on to |
formula |
a formula object. Using aesthetic names |
method |
function or character If character, "lm" (or its equivalent
"aov"), "rlm" or the name of a model fit function are accepted, possibly
followed by the fit function's |
method.args |
named list with additional arguments. |
contrasts |
character vector of length one or a numeric matrix. If
character, one of "Tukey" or "Dunnet". If a matrix, one column per level
of the factor mapped to |
p.adjust.method |
character As the argument for parameter |
small.p |
logical If true, use of lower case p instead of capital P as the symbol for P-value in labels. |
adj.method.tag |
numeric, character or function If |
p.digits |
integer Number of digits after the decimal point to
use for |
label.type |
character One of "bars", "letters" or "LETTERS", selects
how the results of the multiple comparisons are displayed. Only "bars" can
be used together with |
fm.cutoff.p.value |
numeric [0..1] The P-value for the main
effect of factor |
mc.cutoff.p.value |
numeric [0..1] The P-value for the individual contrasts above which no labelled bars are generated. Default is 1, labelling all pairwise contrasts tested. |
mc.critical.p.value |
numeric The critical P-value used for tests when encoded as letters. |
label.y |
numeric vector Values in native data units or if
|
vstep |
numeric in npc units, the vertical displacement step-size
used between labels for different contrasts when |
output.type |
character One of "expression", "LaTeX", "text", "markdown" or "numeric". |
na.rm |
a logical indicating whether NA values should be stripped before the computation proceeds. |
orientation |
character Either "x" or "y" controlling the default for
|
parse |
logical Passed to the geom. If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Details
This statistic can be used to automatically annotate a plot with
P-values for pairwise multiple comparison tests, based on
Tukey contrasts (all pairwise), Dunnet contrasts (other levels against the
first one) or a subset of all possible pairwise contrasts. See Meier (2022,
Chapter 3) for an accessible explanation of multiple comparisons and
contrasts with package 'multcomp', of which stat_multcomp()
is
mostly a wrapper.
The explanatory variable mapped to the x aesthetic must be a factor as this creates the required grouping. Currently, contrasts that involve more than two levels of a factor, such as the average of two treatment levels against a control level are not supported, mainly because they require a new geometry that I need to design, implement and add to package 'ggpp'.
Two ways of displaying the outcomes are implemented, and are selected by '"bars"', '"letters"' or '"LETTERS"' as argument to parameter 'label.type'. '"letters"' and '"LETTERS"' can be used only with Tukey contrasts, as otherwise the encoding is ambiguous. As too many bars clutter a plot, the maximum number of factor levels supported for '"bars"' together with Tukey contrasts is five, while together with Dunnet contrasts or contrasts defined by a numeric matrix, no limit is imposed.
stat_multcomp()
by default generates character labels ready to be
parsed as R expressions but LaTeX (use TikZ device), markdown (use package
'ggtext') and plain text are also supported, as well as numeric values for
user-generated text labels. The value of parse
is set automatically
based on output.type
, but if you assemble labels that need parsing
from numeric
output, the default needs to be overridden. This
statistic only generates annotation labels and segments connecting the
compared factor levels, or letter labels that discriminate significantly
different groups.
Value
A data frame with one row per comparison for label.type =
"bars"
, or a data frame with one row per factor x
level for
label.type = "letters"
and for label.type = "LETTERS"
.
Variables (= columns) as described under Computed variables.
Aesthetics
stat_multcomp()
understands x
and
y
, to be referenced in the formula
and weight
passed
as argument to parameter weights
. A factor must be mapped to
x
and numeric
variables to y
, and, if used, to
weight
. In addition, the aesthetics understood by the geom
("label_pairwise"
is the default for label.type = "bars"
,
"text"
is the default for label.type = "letters"
and for
label.type = "LETTERS"
) are understood and grouping
respected.
Computed variables
If output.type = "numeric"
and
label.type = "bars"
the returned tibble contains
columns listed below. In all cases if the model fit function used does not return a value,
the label is set to character(0L)
and the numeric value to NA
.
- x,x.left.tip,x.right.tip
x position, numeric.
- y
y position, numeric.
- coefficients
Delta estimate from pairwise contrasts, numeric.
- contrasts
Contrasts as two levels' ordinal "numbers" separated by a dash, character.
- tstat
t-statistic estimates for the pairwise contrasts, numeric.
- p.value
P-value for the pairwise contrasts.
- fm.method
Set according
method
used.- fm.class
Most derived class of the fitted model object.
- fm.formula
Formula extracted from the fitted model object if available, or the formula argument.
- fm.formula.chr
Formula extracted from the fitted model object if available, or the formula argument, formatted as character.
- mc.adjusted
The method used to adjust the P-values.
- mc.contrast
The type of contrast used for multiple comparisons.
- n
The total number of observations or rows in data.
- default.label
text label, always included, but possibly NA.
If output.type is not "numeric"
the returned data frame includes in
addition the following labels:
- stars.label
P-value for the pairwise contrasts encoded as "starts", character.
- p.value.label
P-value for the pairwise contrasts, character.
- delta.label
The coefficient or estimate for the difference between compared pairs of levels.
- t.value.label
t-statistic estimates for the pairwise contrasts, character.
If label.type = "letters"
or label.type = "LETTERS"
the returned tibble contains
columns listed below.
- x,x.left.tip,x.right.tip
x position, numeric.
- y
y position, numeric.
- critical.p.value
P-value used in pairwise tests, numeric.
- fm.method
Set according
method
used.- fm.class
Most derived class of the fitted model object.
- fm.formula
Formula extracted from the fitted model object if available, or the formula argument.
- fm.formula.chr
Formula extracted from the fitted model object if available, or the formula argument, formatted as character.
- mc.adjusted
The method used to adjust the P-values.
- mc.contrast
The type of contrast used for multiple comparisons.
- n
The total number of observations or rows in data.
- default.label
text label, always included, but possibly NA.
If output.type is not "numeric"
the returned data frame includes in
addition the following labels:
- letters.label
Letters that distinguish levels based on significance from multiple comparisons test.
Alternatives
stat_signif()
in package 'ggsignif' is
an earlier and independent implementation of pairwise tests.
Note
R option OutDec
is obeyed based on its value at the time the plot
is rendered, i.e., displayed or printed. Set options(OutDec = ",")
for languages like Spanish or French.
References
Meier, Lukas (2022) ANOVA and Mixed Models: A Short Introduction Using R. Chapter 3 Contrasts and Multiple Testing. The R Series. Boca Raton: Chapman and Hall/CRC. ISBN: 9780367704209, doi:10.1201/9781003146216.
See Also
This statistic uses the implementation of Tests of General Linear
Hypotheses in function glht
. See
summary.glht
and p.adjust
for the supported and tests and the references therein for the theory
behind them.
Examples
p1 <- ggplot(mpg, aes(factor(cyl), hwy)) +
geom_boxplot(width = 0.33)
## labeleld bars
p1 +
stat_multcomp()
p1 +
stat_multcomp(adj.method.tag = 0)
# test against a control, with first level being the control
# change order of factor levels in data to set the control group
p1 +
stat_multcomp(contrasts = "Dunnet")
# arbitrary pairwise contrasts, in arbitrary order
p1 +
stat_multcomp(contrasts = rbind(c(0, 0, -1, 1),
c(0, -1, 1, 0),
c(-1, 1, 0, 0)))
# different methods to adjust the contrasts
p1 +
stat_multcomp(p.adjust.method = "bonferroni")
p1 +
stat_multcomp(p.adjust.method = "holm")
p1 +
stat_multcomp(p.adjust.method = "fdr")
# no correction, useful only for comparison
p1 +
stat_multcomp(p.adjust.method = "none")
# sometimes we need to expand the plotting area
p1 +
stat_multcomp(geom = "text_pairwise") +
scale_y_continuous(expand = expansion(mult = c(0.05, 0.10)))
# position of contrasts' bars (based on scale limits)
p1 +
stat_multcomp(label.y = "bottom")
p1 +
stat_multcomp(label.y = 11)
# use different labels: difference and P-value from hypothesis tests
p1 +
stat_multcomp(use_label("Delta", "P"),
size = 2.75)
# control smallest P-value displayed and number of digits
p1 +
stat_multcomp(p.digits = 4)
# label only significant differences
# but test and correct for all pairwise contrasts!
p1 +
stat_multcomp(mc.cutoff.p.value = 0.01)
## letters as labels for test results
p1 +
stat_multcomp(label.type = "letters")
# use capital letters
p1 +
stat_multcomp(label.type = "LETTERS")
# location
p1 +
stat_multcomp(label.type = "letters",
label.y = "top")
p1 +
stat_multcomp(label.type = "letters",
label.y = 0)
# stricter critical p-value than default used for test
p1 +
stat_multcomp(label.type = "letters",
mc.critical.p.value = 0.01)
# Inspecting the returned data using geom_debug()
# This provides a quick way of finding out the names of the variables that
# are available for mapping to aesthetics with after_stat().
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed)
library(gginnards)
if (gginnards.installed)
p1 +
stat_multcomp(label.type = "bars",
geom = "debug")
if (gginnards.installed)
p1 +
stat_multcomp(label.type = "letters",
geom = "debug")
if (gginnards.installed)
p1 +
stat_multcomp(label.type = "bars",
output.type = "numeric",
geom = "debug")