33
33
# ' @inheritParams geom_bar
34
34
# ' @param geom,stat Use to override the default connection between
35
35
# ' `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.
36
42
# ' @param outlier.colour,outlier.color,outlier.fill,outlier.shape,outlier.size,outlier.stroke,outlier.alpha
37
43
# ' Default aesthetics for outliers. Set to `NULL` to inherit from the
38
44
# ' aesthetics used for the box.
39
45
# '
40
46
# ' In the unlikely event you specify both US and UK spellings of colour, the
41
47
# ' US spelling will take precedence.
42
48
# '
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
- # '
49
49
# ' @param notch If `FALSE` (default) make a standard box plot. If
50
50
# ' `TRUE`, make a notched box plot. Notches are used to compare groups;
51
51
# ' if the notches of two boxes do not overlap, this suggests that the medians
109
109
geom_boxplot <- function (mapping = NULL , data = NULL ,
110
110
stat = " boxplot" , position = " dodge2" ,
111
111
... ,
112
+ outliers = TRUE ,
112
113
outlier.colour = NULL ,
113
114
outlier.color = NULL ,
114
115
outlier.fill = NULL ,
@@ -133,6 +134,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
133
134
position $ preserve <- " single"
134
135
}
135
136
}
137
+ check_bool(outliers )
136
138
137
139
layer(
138
140
data = data ,
@@ -143,6 +145,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
143
145
show.legend = show.legend ,
144
146
inherit.aes = inherit.aes ,
145
147
params = list2(
148
+ outliers = outliers ,
146
149
outlier.colour = outlier.color %|| % outlier.colour ,
147
150
outlier.fill = outlier.fill ,
148
151
outlier.shape = outlier.shape ,
@@ -167,7 +170,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
167
170
168
171
# need to declare `width` here in case this geom is used with a stat that
169
172
# 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 " ),
171
174
172
175
setup_params = function (data , params ) {
173
176
params $ flipped_aes <- has_flipped_aes(data , params )
@@ -180,6 +183,10 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
180
183
data $ width <- data $ width %|| %
181
184
params $ width %|| % (resolution(data $ x , FALSE ) * 0.9 )
182
185
186
+ if (isFALSE(params $ outliers )) {
187
+ data $ outliers <- NULL
188
+ }
189
+
183
190
if (! is.null(data $ outliers )) {
184
191
suppressWarnings({
185
192
out_min <- vapply(data $ outliers , min , numeric (1 ))
0 commit comments