44# ' It visualises five summary statistics (the median, two hinges
55# ' and two whiskers), and all "outlying" points individually.
66# '
7+ # ' @eval rd_orientation()
8+ # '
79# ' @section Summary statistics:
810# ' The lower and upper hinges correspond to the first and third quartiles
911# ' (the 25th and 75th percentiles). This differs slightly from the method used
2830# ' [geom_violin()] for a richer display of the distribution, and
2931# ' [geom_jitter()] for a useful technique for small data.
3032# ' @inheritParams layer
31- # ' @inheritParams geom_point
33+ # ' @inheritParams geom_bar
3234# ' @param geom,stat Use to override the default connection between
3335# ' `geom_boxplot` and `stat_boxplot`.
3436# ' @param outlier.colour,outlier.color,outlier.fill,outlier.shape,outlier.size,outlier.stroke,outlier.alpha
6062# ' @examples
6163# ' p <- ggplot(mpg, aes(class, hwy))
6264# ' p + geom_boxplot()
63- # ' p + geom_boxplot() + coord_flip()
65+ # ' # Orientation follows the discrete axis
66+ # ' ggplot(mpg, aes(hwy, class)) + geom_boxplot()
6467# '
6568# ' p + geom_boxplot(notch = TRUE)
6669# ' p + geom_boxplot(varwidth = TRUE)
@@ -116,6 +119,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
116119 notchwidth = 0.5 ,
117120 varwidth = FALSE ,
118121 na.rm = FALSE ,
122+ orientation = NA ,
119123 show.legend = NA ,
120124 inherit.aes = TRUE ) {
121125
@@ -148,6 +152,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
148152 notchwidth = notchwidth ,
149153 varwidth = varwidth ,
150154 na.rm = na.rm ,
155+ orientation = orientation ,
151156 ...
152157 )
153158 )
@@ -161,9 +166,16 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
161166
162167 # need to declare `width` here in case this geom is used with a stat that
163168 # 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+ },
165175
166176 setup_data = function (data , params ) {
177+ data $ flipped_aes <- params $ flipped_aes
178+ data <- flip_data(data , params $ flipped_aes )
167179 data $ width <- data $ width %|| %
168180 params $ width %|| % (resolution(data $ x , FALSE ) * 0.9 )
169181
@@ -173,8 +185,8 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
173185 out_max <- vapply(data $ outliers , max , numeric (1 ))
174186 })
175187
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 )
178190 }
179191
180192 # if `varwidth` not requested or not available, don't use it
@@ -190,16 +202,16 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
190202 data $ width <- NULL
191203 if (! is.null(data $ relvarwidth )) data $ relvarwidth <- NULL
192204
193- data
205+ flip_data( data , params $ flipped_aes )
194206 },
195207
196208 draw_group = function (data , panel_params , coord , fatten = 2 ,
197209 outlier.colour = NULL , outlier.fill = NULL ,
198210 outlier.shape = 19 ,
199211 outlier.size = 1.5 , outlier.stroke = 0.5 ,
200212 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 )
203215 # this may occur when using geom_boxplot(stat = "identity")
204216 if (nrow(data ) != 1 ) {
205217 stop(
@@ -226,6 +238,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
226238 ),
227239 common
228240 ), n = 2 )
241+ whiskers <- flip_data(whiskers , flipped_aes )
229242
230243 box <- new_data_frame(c(
231244 list (
@@ -241,6 +254,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
241254 ),
242255 common
243256 ))
257+ box <- flip_data(box , flipped_aes )
244258
245259 if (! is.null(data $ outliers ) && length(data $ outliers [[1 ]] > = 1 )) {
246260 outliers <- new_data_frame(list (
@@ -254,6 +268,8 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
254268 fill = NA ,
255269 alpha = outlier.alpha %|| % data $ alpha [1 ]
256270 ), n = length(data $ outliers [[1 ]]))
271+ outliers <- flip_data(outliers , flipped_aes )
272+
257273 outliers_grob <- GeomPoint $ draw_panel(outliers , panel_params , coord )
258274 } else {
259275 outliers_grob <- NULL
@@ -262,7 +278,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
262278 ggname(" geom_boxplot" , grobTree(
263279 outliers_grob ,
264280 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 )
266282 ))
267283 },
268284
@@ -271,5 +287,5 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
271287 default_aes = aes(weight = 1 , colour = " grey20" , fill = " white" , size = 0.5 ,
272288 alpha = NA , shape = 19 , linetype = " solid" ),
273289
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 " )
275291)
0 commit comments