4
4
# ' It visualises five summary statistics (the median, two hinges
5
5
# ' and two whiskers), and all "outlying" points individually.
6
6
# '
7
+ # ' @eval rd_orientation()
8
+ # '
7
9
# ' @section Summary statistics:
8
10
# ' The lower and upper hinges correspond to the first and third quartiles
9
11
# ' (the 25th and 75th percentiles). This differs slightly from the method used
28
30
# ' [geom_violin()] for a richer display of the distribution, and
29
31
# ' [geom_jitter()] for a useful technique for small data.
30
32
# ' @inheritParams layer
31
- # ' @inheritParams geom_point
33
+ # ' @inheritParams geom_bar
32
34
# ' @param geom,stat Use to override the default connection between
33
35
# ' `geom_boxplot` and `stat_boxplot`.
34
36
# ' @param outlier.colour,outlier.color,outlier.fill,outlier.shape,outlier.size,outlier.stroke,outlier.alpha
60
62
# ' @examples
61
63
# ' p <- ggplot(mpg, aes(class, hwy))
62
64
# ' p + geom_boxplot()
63
- # ' p + geom_boxplot() + coord_flip()
65
+ # ' # Orientation follows the discrete axis
66
+ # ' ggplot(mpg, aes(hwy, class)) + geom_boxplot()
64
67
# '
65
68
# ' p + geom_boxplot(notch = TRUE)
66
69
# ' p + geom_boxplot(varwidth = TRUE)
@@ -116,6 +119,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
116
119
notchwidth = 0.5 ,
117
120
varwidth = FALSE ,
118
121
na.rm = FALSE ,
122
+ orientation = NA ,
119
123
show.legend = NA ,
120
124
inherit.aes = TRUE ) {
121
125
@@ -148,6 +152,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
148
152
notchwidth = notchwidth ,
149
153
varwidth = varwidth ,
150
154
na.rm = na.rm ,
155
+ orientation = orientation ,
151
156
...
152
157
)
153
158
)
@@ -161,9 +166,16 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
161
166
162
167
# need to declare `width` here in case this geom is used with a stat that
163
168
# doesn't have a `width` parameter (e.g., `stat_identity`).
164
- extra_params = c(" na.rm" , " width" ),
169
+ extra_params = c(" na.rm" , " width" , " orientation" ),
170
+
171
+ setup_params = function (data , params ) {
172
+ params $ flipped_aes <- has_flipped_aes(data , params )
173
+ params
174
+ },
165
175
166
176
setup_data = function (data , params ) {
177
+ data $ flipped_aes <- params $ flipped_aes
178
+ data <- flip_data(data , params $ flipped_aes )
167
179
data $ width <- data $ width %|| %
168
180
params $ width %|| % (resolution(data $ x , FALSE ) * 0.9 )
169
181
@@ -173,8 +185,8 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
173
185
out_max <- vapply(data $ outliers , max , numeric (1 ))
174
186
})
175
187
176
- data $ ymin_final <- pmin(out_min , data $ ymin )
177
- data $ ymax_final <- pmax(out_max , data $ ymax )
188
+ data $ ymin_final <- pmin(out_min , data $ ymin )
189
+ data $ ymax_final <- pmax(out_max , data $ ymax )
178
190
}
179
191
180
192
# if `varwidth` not requested or not available, don't use it
@@ -190,16 +202,16 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
190
202
data $ width <- NULL
191
203
if (! is.null(data $ relvarwidth )) data $ relvarwidth <- NULL
192
204
193
- data
205
+ flip_data( data , params $ flipped_aes )
194
206
},
195
207
196
208
draw_group = function (data , panel_params , coord , fatten = 2 ,
197
209
outlier.colour = NULL , outlier.fill = NULL ,
198
210
outlier.shape = 19 ,
199
211
outlier.size = 1.5 , outlier.stroke = 0.5 ,
200
212
outlier.alpha = NULL ,
201
- notch = FALSE , notchwidth = 0.5 , varwidth = FALSE ) {
202
-
213
+ notch = FALSE , notchwidth = 0.5 , varwidth = FALSE , flipped_aes = FALSE ) {
214
+ data <- flip_data( data , flipped_aes )
203
215
# this may occur when using geom_boxplot(stat = "identity")
204
216
if (nrow(data ) != 1 ) {
205
217
stop(
@@ -226,6 +238,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
226
238
),
227
239
common
228
240
), n = 2 )
241
+ whiskers <- flip_data(whiskers , flipped_aes )
229
242
230
243
box <- new_data_frame(c(
231
244
list (
@@ -241,6 +254,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
241
254
),
242
255
common
243
256
))
257
+ box <- flip_data(box , flipped_aes )
244
258
245
259
if (! is.null(data $ outliers ) && length(data $ outliers [[1 ]] > = 1 )) {
246
260
outliers <- new_data_frame(list (
@@ -254,6 +268,8 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
254
268
fill = NA ,
255
269
alpha = outlier.alpha %|| % data $ alpha [1 ]
256
270
), n = length(data $ outliers [[1 ]]))
271
+ outliers <- flip_data(outliers , flipped_aes )
272
+
257
273
outliers_grob <- GeomPoint $ draw_panel(outliers , panel_params , coord )
258
274
} else {
259
275
outliers_grob <- NULL
@@ -262,7 +278,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
262
278
ggname(" geom_boxplot" , grobTree(
263
279
outliers_grob ,
264
280
GeomSegment $ draw_panel(whiskers , panel_params , coord ),
265
- GeomCrossbar $ draw_panel(box , fatten = fatten , panel_params , coord )
281
+ GeomCrossbar $ draw_panel(box , fatten = fatten , panel_params , coord , flipped_aes = flipped_aes )
266
282
))
267
283
},
268
284
@@ -271,5 +287,5 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
271
287
default_aes = aes(weight = 1 , colour = " grey20" , fill = " white" , size = 0.5 ,
272
288
alpha = NA , shape = 19 , linetype = " solid" ),
273
289
274
- required_aes = c(" x" , " lower" , " upper" , " middle" , " ymin" , " ymax" )
290
+ required_aes = c(" x|y " , " lower|xlower " , " upper|xupper " , " middle|xmiddle " , " ymin|xmin " , " ymax|xmax " )
275
291
)
0 commit comments