Calculate per-point Hotelling statistic for use in ggplot
Usage
stat_outliers(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
type = c("t2data", "c2data"),
level = 0.95,
outlier_only = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)Arguments
- mapping
Set of aesthetic mappings created by
aes(). If specified andinherit.aes = TRUE(the default), it is combined with the default mapping at the top level of the plot. You must supplymappingif there is no plot mapping.- data
The data to be displayed in this layer. There are three options:
If
NULL, the default, the data is inherited from the plot data as specified in the call toggplot().A
data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. Seefortify()for which variables will be created.A
functionwill be called with a single argument, the plot data. The return value must be adata.frame, and will be used as the layer data. Afunctioncan be created from aformula(e.g.~ head(.x, 10)).- geom
The geometric object to use to display the data for this layer. When using a
stat_*()function to construct a layer, thegeomargument can be used to override the default coupling between stats and geoms. Thegeomargument accepts the following:A
Geomggproto subclass, for exampleGeomPoint.A string naming the geom. To give the geom as a string, strip the function name of the
geom_prefix. For example, to usegeom_point(), give the geom as"point".For more information and other ways to specify the geom, see the layer geom documentation.
- position
A position adjustment to use on the data for this layer. This can be used in various ways, including to prevent overplotting and improving the display. The
positionargument accepts the following:The result of calling a position function, such as
position_jitter(). This method allows for passing extra arguments to the position.A string naming the position adjustment. To give the position as a string, strip the function name of the
position_prefix. For example, to useposition_jitter(), give the position as"jitter".For more information and other ways to specify the position, see the layer position documentation.
- ...
Additional parameters passed to underlying
ggplot2::geom_polygon()or toggplot2::layer().- type
t2data - Hotelling T2 data ellipse; t2mean - Hotelling confidence interval for the mean; c2data - normal data ellipse (using chi squared distribution).
- level
Either coverage probability (for type = "t2data" or "c2data") or confidence level (for type = "t2mean").
- outlier_only
Only return the statistic for outliers
- na.rm
Logical. Should missing values be removed? Default is FALSE.
- show.legend
logical. Should this layer be included in the legends?
NA, the default, includes if any aesthetics are mapped.FALSEnever includes, andTRUEalways includes. It can also be a named logical vector to finely select the aesthetics to display. To include legend keys for all levels, even when no data exists, useTRUE. IfNA, all levels are shown in legend, but unobserved levels are omitted.- inherit.aes
If
FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g.annotation_borders().
Details
This calculates the Hotelling statistic for each point in the plot,
group-wise. This allows to use the statistics is_outlier, d2 (the
squared Mahalanobis distance), c2crit (critical Chi-squared value for
the specified level) and t2crit (critical Hotelling T2 value
for the squared Mahalanobis distance) to be used as graphical
parameters, e.g. for coloring the points (see Examples below) using the
ggplot2::after_stat() function.
The is_outlier is simply either d2 > t2crit (if type="t2data" or
type="t2mean") or d2 > c2crit (if type="c2data").
The type argument chooses between the regular Hotelling statistic
(with type="t2data") or the Chi-squared statistic (with
type="c2data"). The Hotelling statistic for the mean is not allowed as
it makes no sense in the context of the outliers.
The function is a wrapper around the outliers() function, which does
the actual calculations, and the parameters passed (level, type, robust)
are passed on to that function. For example, to calculate robust
statistic, use stat_outliers(robust=TRUE).
Examples
pca <- prcomp(iris[, 1:4], scale.=TRUE)
df <- cbind(iris, pca$x)
library(ggplot2)
ggplot(df, aes(PC1, PC2, group=Species)) +
geom_hotelling(alpha=0.1, aes(fill = Species)) +
scale_color_manual(values=c("TRUE"="red", "FALSE"="grey")) +
stat_outliers(aes(color = after_stat(is_outlier)))
ggplot(df, aes(PC1, PC2, group=Species)) +
geom_hotelling(alpha=0.1, level = .75, aes(fill = Species)) +
stat_outliers(level = .75,
size=2,
aes(shape = Species,
color = after_stat(d2)))
# label the outliers
# note that you need to add the label aesthetics for the label geom to
# work
ggplot(df, aes(PC1, PC2, group=Species, label=rownames(df))) +
geom_hotelling(level = 0.75, alpha=0.1, aes(fill = Species)) +
geom_point(aes(color = Species)) +
stat_outliers(level = .75, geom="label",
outlier_only = TRUE)
