Skip to content

Commit

Permalink
Adressing Hadley Wickham's suggestions to #927
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Sieger committed Mar 25, 2014
1 parent 1a7f86d commit 5dc188a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
4 changes: 4 additions & 0 deletions NEWS
@@ -1,6 +1,10 @@
ggplot2 0.9.3.1.99
----------------------------------------------------------------

* `geom_boxplot` gain new `varwidth` argument for controlling whether or not
the width of boxplots should be proportional to the size of the groups
(@tsieger, #927).

* Allow specifying only one of the limits in a scale and use the automatic
calculation of the other limit by passing NA to to the limit function,
`xlim()` or `ylim()` (@jimhester, #557).
Expand Down
27 changes: 12 additions & 15 deletions R/geom-boxplot.r
Expand Up @@ -35,7 +35,7 @@
#' @param varwidth if \code{FALSE} (default) make a standard box plot. If
#' \code{TRUE}, boxes are drawn with widths proportional to the
#' square-roots of the number of observations in the groups (possibly
#' weighted).
#' weighted, using the \code{weight} aesthetic).
#' @export
#'
#' @references McGill, R., Tukey, J. W. and Larsen, W. A. (1978) Variations of
Expand Down Expand Up @@ -126,23 +126,20 @@ GeomBoxplot <- proto(Geom, {
df$ymax_final <- pmax(out_max, df$ymax)
}

# if varwidth not requested or not available, don't use it
if(is.null(params) || is.null(params$varwidth) || !params$varwidth || is.null(df$relvarwidth)) {
if (is.null(df$relvarwidth)) {
transform(df,
xmin = x - width / 2, xmax = x + width / 2, width = NULL
)
} else {
transform(df,
xmin = x - width / 2, xmax = x + width / 2, width = NULL, relvarwidth = NULL
)
}
# if `varwidth` not requested or not available, don't use it
if (is.null(params) || is.null(params$varwidth) || !params$varwidth || is.null(df$relvarwidth)) {
df$xmin <- df$x - df$width / 2
df$xmax <- df$x + df$width / 2
} else {
transform(df,
xmin = x - relvarwidth * width / 2, xmax = x + relvarwidth * width / 2, width = NULL, relvarwidth = NULL
)
# make `relvarwidth` relative to the size of the largest group
df$relvarwidth <- df$relvarwidth / max(df$relvarwidth)
df$xmin <- df$x - df$relvarwidth * df$width / 2
df$xmax <- df$x + df$relvarwidth * df$width / 2
}
df$width <- NULL
if (!is.null(df$relvarwidth)) df$relvarwidth <- NULL

df
}

draw <- function(., data, ..., fatten = 2, outlier.colour = NULL, outlier.shape = NULL, outlier.size = 2,
Expand Down
3 changes: 2 additions & 1 deletion man/geom_boxplot.Rd
Expand Up @@ -27,7 +27,8 @@ geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot",
\item{varwidth}{if \code{FALSE} (default) make a standard
box plot. If \code{TRUE}, boxes are drawn with widths
proportional to the square-roots of the number of
observations in the groups (possibly weighted).}
observations in the groups (possibly weighted, using the
\code{weight} aesthetic).}

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

0 comments on commit 5dc188a

Please sign in to comment.