Skip to content

Add parameter "varwidth" to geom_boxplot #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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
30 changes: 24 additions & 6 deletions R/geom-boxplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#' the medians differ.
#' @param notchwidth for a notched box plot, width of the notch relative to
#' the body (default 0.5)
#' @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, using the \code{weight} aesthetic).
#' @export
#'
#' @references McGill, R., Tukey, J. W. and Larsen, W. A. (1978) Variations of
Expand Down Expand Up @@ -92,13 +96,17 @@
#' b + geom_boxplot(stat = "identity")
#' b + geom_boxplot(stat = "identity") + coord_flip()
#' b + geom_boxplot(aes(fill = X1), stat = "identity")
#'
#' # Using varwidth
#' p + geom_boxplot(varwidth = TRUE)
#' qplot(factor(cyl), mpg, data = mtcars, geom = "boxplot", varwidth = TRUE)
#' }
geom_boxplot <- function (mapping = NULL, data = NULL, stat = "boxplot", position = "dodge",
outlier.colour = "black", outlier.shape = 16, outlier.size = 2,
notch = FALSE, notchwidth = .5, ...) {
notch = FALSE, notchwidth = .5, varwidth = FALSE, ...) {
GeomBoxplot$new(mapping = mapping, data = data, stat = stat,
position = position, outlier.colour = outlier.colour, outlier.shape = outlier.shape,
outlier.size = outlier.size, notch = notch, notchwidth = notchwidth, ...)
outlier.size = outlier.size, notch = notch, notchwidth = notchwidth, varwidth = varwidth, ...)
}

GeomBoxplot <- proto(Geom, {
Expand All @@ -118,14 +126,24 @@ GeomBoxplot <- proto(Geom, {
df$ymax_final <- pmax(out_max, df$ymax)
}

transform(df,
xmin = x - width / 2, xmax = x + width / 2, width = 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 {
# 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,
notch = FALSE, notchwidth = .5) {
notch = FALSE, notchwidth = .5, varwidth = FALSE) {
common <- data.frame(
colour = data$colour,
size = data$size,
Expand Down
3 changes: 2 additions & 1 deletion R/stat-boxplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ StatBoxplot <- proto(Stat, {

transform(df,
x = if (is.factor(x)) x[1] else mean(range(x)),
width = width
width = width,
relvarwidth = sqrt(n)
)
})
}
Expand Down
13 changes: 12 additions & 1 deletion man/geom_boxplot.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
\usage{
geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot",
position = "dodge", outlier.colour = "black", outlier.shape = 16,
outlier.size = 2, notch = FALSE, notchwidth = 0.5, ...)
outlier.size = 2, notch = FALSE, notchwidth = 0.5, varwidth = FALSE,
...)
}
\arguments{
\item{outlier.colour}{colour for outlying points}
Expand All @@ -23,6 +24,12 @@ geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot",
\item{notchwidth}{for a notched box plot, width of the
notch relative to the body (default 0.5)}

\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, using the
\code{weight} aesthetic).}

\item{mapping}{The aesthetic mapping, usually constructed
with \code{\link{aes}} or \code{\link{aes_string}}. Only
needs to be set at the layer level if you are overriding
Expand Down Expand Up @@ -119,6 +126,10 @@ b <- ggplot(abc, aes(x = X1, ymin = `0\%`, lower = `25\%`, middle = `50\%`, uppe
b + geom_boxplot(stat = "identity")
b + geom_boxplot(stat = "identity") + coord_flip()
b + geom_boxplot(aes(fill = X1), stat = "identity")

# Using varwidth
p + geom_boxplot(varwidth = TRUE)
qplot(factor(cyl), mpg, data = mtcars, geom = "boxplot", varwidth = TRUE)
}
}
\references{
Expand Down