assay, rowData or colData.R/AllGenerics.R, R/plotBoxplot.R
plotBoxplot.RdThis methods visualizes abundances or variables from rowData or
colData.
plotBoxplot(object, ...)
# S4 method for class 'SummarizedExperiment'
plotBoxplot(
object,
assay.type = NULL,
row.var = NULL,
col.var = NULL,
x = NULL,
features = NULL,
group.by = NULL,
...
)a
SummarizedExperiment
object.
Additional parameters for plotting. They are grouped below by
the part of the plot they customise. Each layer has its own prefixed
arguments (box.*, point.*, line.*, outlier.*).
Arguments not listed here are passed on to
ggpubr::stat_pvalue_manual()
when p-values are shown (see "Significance" below). Anything else, such
as titles, scales and themes, can be changed by adding ggplot2
layers to the returned plot, e.g. + labs(y = "Shannon").
Mapping variables to aesthetics
colour.by: NULL or character scalar. Specifies a
variable from colData(x) or rowData(x) which is used to
colour observations. (Default: NULL)
fill.by: NULL or character scalar. Specifies a
variable from colData(x) or rowData(x) which is used to
fill boxes and points. (Default: NULL)
size.by: NULL or character scalar. Specifies a
variable from colData(x) or rowData(x) which is used to scale
observation points. (Default: NULL)
shape.by: NULL or character scalar. Specifies a
variable from colData(x) or rowData(x) which is used to shape
observation points. (Default: NULL)
facet.by: NULL or character scalar. Specifies a
variable from colData(x) or rowData(x) which is used to facet
or group observations. Use "rownames" to draw each of
features in its own facet. (Default: NULL)
pair.by: NULL or character scalar. Specifies a
variable from colData(x) which is used to pair observation points.
Paired points are joined with lines. (Default: NULL)
add.change: Logical scalar. Whether to colour paired
observations by the change between time points. (Default: FALSE)
Layers
add.box: Logical scalar. Whether to add a boxplot
layer. (Default: TRUE)
add.points: Logical scalar. Whether to add a point
layer. (Default: TRUE)
add.proportion: Logical scalar. Whether to add a
barplot layer denoting the proportion of observations above
threshold. (Default: FALSE)
add.threshold: Logical scalar. Whether to add
threshold as a horizontal line. The line is always added with
add.proportion = TRUE. (Default: FALSE)
add.significance: Logical scalar. Whether to test
and show the significance of differences between groups (see
"Significance" below). (Default: FALSE)
Box layer
box.alpha: Numeric scalar. Transparency of the boxes.
(Default: 0.5)
box.width: Numeric scalar. Width of boxes.
(Default: 0.75)
outlier.size, outlier.colour,
outlier.alpha, outlier.shape: Size, colour, transparency
and shape of the outlier points of the boxplot layer, passed to
geom_boxplot. Outliers are drawn
only when add.points = FALSE; otherwise all observations are
already shown as points. If NULL, ggplot2 defaults are
used, and the shape defaults to point.shape.
(Default: NULL)
Point layer
point.alpha: Numeric scalar. Transparency of points.
(Default: 0.65)
point.shape: Numeric scalar. Shape of points.
(Default: 21)
point.size: Numeric scalar. Size of points.
(Default: 2)
point.colour: Character scalar. Colour of points.
(Default: "grey70")
point.offset: Character scalar. Method for offsetting
points so that they do not overlap. The available options include:
"center", "compactswarm", "hex", "square",
"swarm"
(see beeswarm::beeswarm() for details),
"frowney", "maxout", "minout", "pseudorandom",
"quasirandom", "smiley", "tukey", "tukeyDense"
(see vipor::offsetSingleGroup()
for details), "jitter", and "none".
If "none", offsetting is not applied. (Default: "jitter")
jitter.width: Numeric scalar. Width of jitter.
(Default: 0.3)
jitter.height: Numeric scalar. Height of jitter.
(Default: 0)
beeswarm.corral: Character scalar. Beeswarm's "corral"
method. Fed to function beeswarm::beeswarm().
(Default: "none")
dodge.width: Numeric scalar. Width of dodge. How far
apart the groups are plotted? Applies to boxes, points and bars.
(Default: 0)
Line layer (paired observations, pair.by)
line.alpha: Numeric scalar. Transparency of lines.
(Default: 0.5)
line.colour: Character scalar. Colour of lines.
(Default: "grey70")
linetype: Numeric scalar. Type of lines.
(Default: 1)
linewidth: Numeric scalar. Width of lines.
(Default: 1)
Proportion bars (add.proportion = TRUE)
threshold: Numeric scalar. Specifies threshold for the
barplots. (Default: 0)
bar.width: Numeric scalar. Width of proportion bars.
By default, it is calculated so that the width matches with the
width of boxes.
Facets
scales: Character scalar. Adjust scales of facets.
(Default: "fixed")
nrow, ncol: NULL or integer scalar.
Number of rows and columns of facets. (Default: NULL)
Significance (add.significance = TRUE or p.value)
p.value: NULL or data.frame. Pre-calculated
p-values to show instead of testing with add.significance. It must
have columns group1 and group2, one of p,
p.adj or p.adj.signif, and the facet and x axis variables
that are not compared, e.g. the output of rstatix::t_test()
(see examples). (Default: NULL)
significance.method: Character scalar. Test used
with add.significance: "wilcox.test" (or
"wilcoxon") or "t.test" (or "t-test").
(Default: "wilcox.test")
paired: Logical scalar. Whether to use a paired
test. (Default: TRUE if pair.by is specified)
p.adjust.method: Character scalar. Method for
adjusting p-values for multiple testing, see
p.adjust. (Default: "fdr")
mark.significance: Logical scalar. Whether to show
significance levels as asterisks instead of p-values.
(Default: FALSE)
digits: Integer scalar. Number of digits in the
shown p-values. (Default: 3)
step.increase: Numeric scalar. Distance of
significance brackets from the highest group they span, and between
stacked brackets, as a fraction of the y axis range. (Default: 0.12)
hide.ns: Logical scalar or "p" or
"p.adj". Whether to hide non-significant comparisons, as in
ggpubr::stat_pvalue_manual. Hidden comparisons do not take space,
so the shown brackets are stacked without gaps. (Default: FALSE)
Other arguments are passed to
ggpubr::stat_pvalue_manual(),
for example label, label.size, bracket.size,
tip.length and color.
The American spellings color.by, point.color,
line.color and outlier.color are also accepted.
NULL or character scalar. Specifies the
abundace table to plot. (Default: NULL)
NULL or character scalar. Specifies a variable
from rowData(x) to visualize. (Default: NULL)
NULL or character scalar Specifies a variable
from colData(x) to visualize. (Default: NULL)
NULL or character vector. Specifies a variable
from colData(x) or rowData(x) to visualize in x axis.
(Default: NULL)
NULL or character vector. If assay.type
is specified, this specifies rows to visualize in different facets. If
NULL, whole data is visualized as a whole. (Default: NULL)
NULL or character vector. Specifies a variable
from colData(x) or rowData(x) to group observations.
(Default: NULL)
A ggplot2 object.
A box plot is standard visualization technique to compare numeric values,
such as abundance, between categorical values, such as sample groups.
plotBoxplot() streamlines creation of box plots, and it offers
multiple options for visualization.
data("Tito2024QMP")
tse <- Tito2024QMP
tse <- transformAssay(tse, method = "relabundance")
tse <- addAlpha(tse, index = "shannon")
# Visualize alpha diversity
plotBoxplot(tse, col.var = "shannon", x = "diagnosis")
# Visualize relative abundance of top features
tse <- tse[getTop(tse, 6), ]
plotBoxplot(
tse, assay.type = "relabundance",
x = "diagnosis", fill.by = "diagnosis",
features = rownames(tse), facet.by = "rownames",
ncol = 2
)
# Add proportion bar
plotBoxplot(
tse, assay.type = "relabundance",
x = "diagnosis", fill.by = "diagnosis",
features = rownames(tse), facet.by = "rownames",
add.proportion = TRUE, threshold = 0.1
)
# Visualize only with beeswarm
plotBoxplot(
tse, assay.type = "relabundance",
x = "diagnosis", group.by = "diagnosis",
colour.by = "colonoscopy",
features = rownames(tse), facet.by = "rownames",
point.offset = "swarm", add.box = FALSE
)
#> Warning: Removed 750 rows containing missing values or values outside the scale range
#> (`geom_point()`).
# Do not add points
plotBoxplot(
tse, assay.type = "relabundance",
fill.by = "diagnosis",
features = rownames(tse), facet.by = "rownames",
add.points = FALSE
)
# Calculate statistical significance with Wilcoxon test
plotBoxplot(
tse,
col.var = "shannon",
fill.by = "diagnosis",
add.significance = TRUE
)
if (FALSE) { # \dontrun{
# Add pre-calculated p-values
# Calculate p-values
df <- meltSE(tse, add.col = TRUE)
pvals <- df |>
rstatix::t_test(shannon ~ diagnosis) |>
ungroup() |>
as.data.frame()
# Add them with p.value argument
plotBoxplot(
tse,
x = "diagnosis",
col.var = "shannon",
p.value = pvals
)
} # }
if (FALSE) { # \dontrun{
library(microbiomeDataSets)
mae <- microbiomeDataSets::peerj32()
tse <- getWithColData(mae, 1)
tse[["time_point"]] <- as.character(tse[["time"]])
# Create a plot showing the change between time points in abundance of
# Akkermansia
plotBoxplot(
tse, x = "time_point", assay.type = "counts", fill.by = "group",
features = "Akkermansia", pair.by = "subject",
add.change = TRUE, scales = "free"
)
} # }