3333# ' @inheritParams geom_bar
3434# ' @param geom,stat Use to override the default connection between
3535# ' `geom_boxplot()` and `stat_boxplot()`.
36+ # ' @param outliers Whether to display (`TRUE`) or discard (`FALSE`) outliers
37+ # ' from the plot. Hiding or discarding outliers can be useful when, for
38+ # ' example, raw data points need to be displayed on top of the boxplot.
39+ # ' By discarding outliers, the axis limits will adapt to the box and whiskers
40+ # ' only, not the full data range. If outliers need to be hidden and the axes
41+ # ' needs to show the full data range, please use `outlier.shape = NA` instead.
3642# ' @param outlier.colour,outlier.color,outlier.fill,outlier.shape,outlier.size,outlier.stroke,outlier.alpha
3743# ' Default aesthetics for outliers. Set to `NULL` to inherit from the
3844# ' aesthetics used for the box.
3945# '
4046# ' In the unlikely event you specify both US and UK spellings of colour, the
4147# ' US spelling will take precedence.
4248# '
43- # ' Sometimes it can be useful to hide the outliers, for example when overlaying
44- # ' the raw data points on top of the boxplot. Hiding the outliers can be achieved
45- # ' by setting `outlier.shape = NA`. Importantly, this does not remove the outliers,
46- # ' it only hides them, so the range calculated for the y-axis will be the
47- # ' same with outliers shown and outliers hidden.
48- # '
4949# ' @param notch If `FALSE` (default) make a standard box plot. If
5050# ' `TRUE`, make a notched box plot. Notches are used to compare groups;
5151# ' if the notches of two boxes do not overlap, this suggests that the medians
109109geom_boxplot <- function (mapping = NULL , data = NULL ,
110110 stat = " boxplot" , position = " dodge2" ,
111111 ... ,
112+ outliers = TRUE ,
112113 outlier.colour = NULL ,
113114 outlier.color = NULL ,
114115 outlier.fill = NULL ,
@@ -133,6 +134,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
133134 position $ preserve <- " single"
134135 }
135136 }
137+ check_bool(outliers )
136138
137139 layer(
138140 data = data ,
@@ -143,6 +145,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
143145 show.legend = show.legend ,
144146 inherit.aes = inherit.aes ,
145147 params = list2(
148+ outliers = outliers ,
146149 outlier.colour = outlier.color %|| % outlier.colour ,
147150 outlier.fill = outlier.fill ,
148151 outlier.shape = outlier.shape ,
@@ -167,7 +170,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
167170
168171 # need to declare `width` here in case this geom is used with a stat that
169172 # doesn't have a `width` parameter (e.g., `stat_identity`).
170- extra_params = c(" na.rm" , " width" , " orientation" ),
173+ extra_params = c(" na.rm" , " width" , " orientation" , " outliers " ),
171174
172175 setup_params = function (data , params ) {
173176 params $ flipped_aes <- has_flipped_aes(data , params )
@@ -180,6 +183,10 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
180183 data $ width <- data $ width %|| %
181184 params $ width %|| % (resolution(data $ x , FALSE ) * 0.9 )
182185
186+ if (isFALSE(params $ outliers )) {
187+ data $ outliers <- NULL
188+ }
189+
183190 if (! is.null(data $ outliers )) {
184191 suppressWarnings({
185192 out_min <- vapply(data $ outliers , min , numeric (1 ))
0 commit comments