Skip to content

Commit 5dc188a

Browse files
author
Tomas Sieger
committed
Adressing Hadley Wickham's suggestions to #927
1 parent 1a7f86d commit 5dc188a

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
ggplot2 0.9.3.1.99
22
----------------------------------------------------------------
33

4+
* `geom_boxplot` gain new `varwidth` argument for controlling whether or not
5+
the width of boxplots should be proportional to the size of the groups
6+
(@tsieger, #927).
7+
48
* Allow specifying only one of the limits in a scale and use the automatic
59
calculation of the other limit by passing NA to to the limit function,
610
`xlim()` or `ylim()` (@jimhester, #557).

R/geom-boxplot.r

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#' @param varwidth if \code{FALSE} (default) make a standard box plot. If
3636
#' \code{TRUE}, boxes are drawn with widths proportional to the
3737
#' square-roots of the number of observations in the groups (possibly
38-
#' weighted).
38+
#' weighted, using the \code{weight} aesthetic).
3939
#' @export
4040
#'
4141
#' @references McGill, R., Tukey, J. W. and Larsen, W. A. (1978) Variations of
@@ -126,23 +126,20 @@ GeomBoxplot <- proto(Geom, {
126126
df$ymax_final <- pmax(out_max, df$ymax)
127127
}
128128

129-
# if varwidth not requested or not available, don't use it
130-
if(is.null(params) || is.null(params$varwidth) || !params$varwidth || is.null(df$relvarwidth)) {
131-
if (is.null(df$relvarwidth)) {
132-
transform(df,
133-
xmin = x - width / 2, xmax = x + width / 2, width = NULL
134-
)
135-
} else {
136-
transform(df,
137-
xmin = x - width / 2, xmax = x + width / 2, width = NULL, relvarwidth = NULL
138-
)
139-
}
129+
# if `varwidth` not requested or not available, don't use it
130+
if (is.null(params) || is.null(params$varwidth) || !params$varwidth || is.null(df$relvarwidth)) {
131+
df$xmin <- df$x - df$width / 2
132+
df$xmax <- df$x + df$width / 2
140133
} else {
141-
transform(df,
142-
xmin = x - relvarwidth * width / 2, xmax = x + relvarwidth * width / 2, width = NULL, relvarwidth = NULL
143-
)
134+
# make `relvarwidth` relative to the size of the largest group
135+
df$relvarwidth <- df$relvarwidth / max(df$relvarwidth)
136+
df$xmin <- df$x - df$relvarwidth * df$width / 2
137+
df$xmax <- df$x + df$relvarwidth * df$width / 2
144138
}
139+
df$width <- NULL
140+
if (!is.null(df$relvarwidth)) df$relvarwidth <- NULL
145141

142+
df
146143
}
147144

148145
draw <- function(., data, ..., fatten = 2, outlier.colour = NULL, outlier.shape = NULL, outlier.size = 2,

man/geom_boxplot.Rd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot",
2727
\item{varwidth}{if \code{FALSE} (default) make a standard
2828
box plot. If \code{TRUE}, boxes are drawn with widths
2929
proportional to the square-roots of the number of
30-
observations in the groups (possibly weighted).}
30+
observations in the groups (possibly weighted, using the
31+
\code{weight} aesthetic).}
3132

3233
\item{mapping}{The aesthetic mapping, usually constructed
3334
with \code{\link{aes}} or \code{\link{aes_string}}. Only

0 commit comments

Comments
 (0)