View source: R/generalized_boxplot.R
| generalized_boxplot | R Documentation |
This function implements the generalized boxplot, a robust data visualization technique designed to effectively represent skewed and heavy-tailed distributions, as proposed by Bruffaerts et al. (2014).
generalized_boxplot(
x,
alpha = 2 * stats::pnorm(-4 * stats::qnorm(0.75)),
p = 0.9,
plot = TRUE,
id = NULL,
group = NULL,
scales = c("free_y", "fixed"),
points = c("outliers", "all", "none"),
label_outliers = FALSE,
show_n = TRUE,
show_mean = FALSE,
annotate = NULL,
horizontal = FALSE,
log = FALSE,
fill = "grey85",
xlab = NULL,
ylab = NULL,
title = NULL,
caption = TRUE,
base_size = 11,
point_size = NULL,
point_alpha = 0.75,
x_labels_angle = 0,
box_width = 0.5,
notch = FALSE,
notch_width = 0.5,
staple_width = 0.5,
xlabels.angle = deprecated(),
xlabels.vjust = deprecated(),
xlabels.hjust = deprecated(),
box.width = deprecated(),
notchwidth = deprecated(),
staplewidth = deprecated()
)
This method extends the adjusted boxplot method by leveraging the flexible Tukey's g-and-h parametric distribution to model the underlying data structure, particularly for asymmetric or long-tailed datasets, providing a more nuanced and informative summary of the data's central tendency, spread, and potential outliers.
The data x_i are first mapped, preserving their ranks, into (0, 1)
by r_i = (\tilde{x}_i - \min_j \tilde{x}_j + 0.1) /
(\max_j \tilde{x}_j - \min_j \tilde{x}_j + 0.2), with
\tilde{x}_i = (x_i - \text{median}) / \text{IQR}, and then onto the
real line by w_i = \Phi^{-1}(r_i). The w_i, standardized by
their median and their interquartile range divided by
z_{0.75} - z_{0.25} = 1.349, are fitted by a Tukey g-and-h
distribution, whose skewness g and tail heaviness h are
estimated from the quantiles of orders p and 1 - p. The fences
are the quantiles of orders \alpha/2 and 1 - \alpha/2 of the
fitted distribution, mapped back to the scale of the data: a proportion
\alpha of the observations of a clean distribution, of whatever
skewness and tails, is expected outside. The default \alpha =
2\,\Phi(-4 z_{0.75}) \approx 0.7\%, the rate of Tukey's boxplot for normal
data, is that of the authors' implementation (the Stata command robbox
of Jann, Verardi and Vermandele). The fences are not inside the box, and
the whiskers end at the most extreme observations within the fences.
The outlying observations are therefore atypical at the rate
\alpha: about \alpha n of them are expected in clean data, so
that with a large \alpha (such as 5%) many flagged observations are
not errors. As h may be negative (tails lighter than normal), where
the g-and-h transform turns back before the \alpha/2 quantile the
fence is its extreme value.
The layout, the points, the annotations and the options for publication
figures are those of adjusted_boxplot(): see its details.
If plot = TRUE, a ggplot2 object.
If plot = FALSE, a list of tibbles: stats, outliers and, with
group, tests, as in adjusted_boxplot(), with the estimated g
and h instead of the medcouple, and alpha * n expected outlying
values.
Christian L. Goueguel
Bruffaerts, C., Verardi, V., Vermandele, C. (2014). A generalized boxplot for skewed and heavy-tailed distributions. Statistics and Probability Letters 95(C):110–117
Verardi, V., Vermandele, C. (2016). Outlier identification for skewed and/or heavy-tailed unimodal multivariate distributions. Journal de la Société Française de Statistique, 157(2):90–114
adjusted_boxplot()
data(forageLIBS)
minerals <- forageLIBS[c("Measurement", "Ca", "Mg", "P", "K", "S")]
# mineral contents, each on its own axis and with its own fill color, the
# outlying samples named: a figure of a journal page width
p <- generalized_boxplot(minerals, id = Measurement, label_outliers = TRUE,
fill = hcl.colors(5, "Pastel 1"), ylab = "Content (%)",
base_size = 8)
p
# saved at that size (here in the temporary directory)
ggplot2::ggsave(file.path(tempdir(), "minerals.pdf"), p, width = 175, height = 70,
units = "mm")
# the statistics and the outlying samples
res <- generalized_boxplot(minerals, id = Measurement, plot = FALSE)
res$stats
res$outliers
# with a detection rate of 5%, about 18 of the 368 samples would be
# flagged per mineral even in clean data
generalized_boxplot(minerals, id = Measurement, alpha = 0.05, plot = FALSE)$stats
# potassium and phosphorus by calcium level, with notches, means and a
# darker blue for a higher level: a figure of a journal column (85 x 75 mm)
minerals$Ca_level <- cut(minerals$Ca, quantile(minerals$Ca, 0:3 / 3),
labels = c("Low Ca", "Mid Ca", "High Ca"),
include.lowest = TRUE)
p <- generalized_boxplot(minerals[c("K", "P", "Ca_level")], group = Ca_level,
notch = TRUE, show_mean = TRUE, ylab = "Content (%)",
fill = c("#deebf7", "#9ecae1", "#4292c6"),
title = "Potassium and phosphorus by calcium level",
base_size = 8)
p
# trace elements on a logarithmic axis, horizontal, with all the samples
# and one fill color for all the boxes
traces <- forageLIBS[c("Fe", "Mn", "Zn")]
generalized_boxplot(traces, log = TRUE, horizontal = TRUE, points = "all",
fill = "#fdd49e", ylab = "Content (mg/kg)")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.