Skip to content

let geom_boxplot use the default colour, shape and size of geom_point #946

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 1 commit into from
Apr 23, 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
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* The outliers of geom_boxplot() use the default colour, size and shape from
geom_point(). Changing the defaults of geom_point() with
update_geom_defaults() will apply the same changes to the outliers of
geom_boxplot(). Changing the defaults for the outliers was previously not
possible. (@ThierryO, #757)

ggplot2 0.9.3.1
----------------------------------------------------------------

Expand Down
23 changes: 19 additions & 4 deletions R/geom-boxplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#' continuous variable, \code{\link{geom_jitter}} for another way to look
#' at conditional distributions"
#' @inheritParams geom_point
#' @param outlier.colour colour for outlying points
#' @param outlier.shape shape of outlying points
#' @param outlier.size size of outlying points
#' @param outlier.colour colour for outlying points. Uses the default from geom_point().
#' @param outlier.shape shape of outlying points. Uses the default from geom_point().
#' @param outlier.size size of outlying points. Uses the default from geom_point().
#' @param notch if \code{FALSE} (default) make a standard box plot. If
#' \code{TRUE}, make a notched box plot. Notches are used to compare groups;
#' if the notches of two boxes do not overlap, this is strong evidence that
Expand Down Expand Up @@ -92,10 +92,25 @@
#' b + geom_boxplot(stat = "identity")
#' b + geom_boxplot(stat = "identity") + coord_flip()
#' b + geom_boxplot(aes(fill = X1), stat = "identity")
#'
#' # Update the defaults for the outliers by changing the defaults for geom_point
#'
#' p <- ggplot(mtcars, aes(factor(cyl), mpg))
#' p + geom_boxplot()
#'
#' update_geom_defaults("point", list(shape = 1, colour = "red", size = 5))
#' p + geom_boxplot()
#' }
geom_boxplot <- function (mapping = NULL, data = NULL, stat = "boxplot", position = "dodge",
outlier.colour = "black", outlier.shape = 16, outlier.size = 2,
outlier.colour = NULL, outlier.shape = NULL, outlier.size = NULL,
notch = FALSE, notchwidth = .5, ...) {

outlier_defaults <- Geom$find('point')$default_aes()

outlier.colour <- outlier.colour %||% outlier_defaults$colour
outlier.shape <- outlier.shape %||% outlier_defaults$shape
outlier.size <- outlier.size %||% outlier_defaults$size

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, ...)
Expand Down
109 changes: 54 additions & 55 deletions man/geom_boxplot.Rd
Original file line number Diff line number Diff line change
@@ -1,73 +1,65 @@
% Generated by roxygen2 (4.0.0): do not edit by hand
\name{geom_boxplot}
\alias{geom_boxplot}
\title{Box and whiskers plot.}
\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, ...)
geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot",
position = "dodge", outlier.colour = NULL, outlier.shape = NULL,
outlier.size = NULL, notch = FALSE, notchwidth = 0.5, ...)
}
\arguments{
\item{outlier.colour}{colour for outlying points}
\item{outlier.colour}{colour for outlying points. Uses the default from geom_point().}

\item{outlier.shape}{shape of outlying points}
\item{outlier.shape}{shape of outlying points. Uses the default from geom_point().}

\item{outlier.size}{size of outlying points}
\item{outlier.size}{size of outlying points. Uses the default from geom_point().}

\item{notch}{if \code{FALSE} (default) make a standard
box plot. If \code{TRUE}, make a notched box plot.
Notches are used to compare groups; if the notches of two
boxes do not overlap, this is strong evidence that the
medians differ.}
\item{notch}{if \code{FALSE} (default) make a standard box plot. If
\code{TRUE}, make a notched box plot. Notches are used to compare groups;
if the notches of two boxes do not overlap, this is strong evidence that
the medians differ.}

\item{notchwidth}{for a notched box plot, width of the
notch relative to the body (default 0.5)}
\item{notchwidth}{for a notched box plot, width of the notch relative to
the body (default 0.5)}

\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
the plot defaults.}
\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 the plot defaults.}

\item{data}{A layer specific dataset - only needed if you
want to override the plot defaults.}
\item{data}{A layer specific dataset - only needed if you want to override
the plot defaults.}

\item{stat}{The statistical transformation to use on the
data for this layer.}
\item{stat}{The statistical transformation to use on the data for this
layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
\item{position}{The position adjustment to use for overlapping points
on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
values you want to set, not map. See \code{\link{layer}}
for more details.}
\item{...}{other arguments passed on to \code{\link{layer}}. This can
include aesthetics whose values you want to set, not map. See
\code{\link{layer}} for more details.}
}
\description{
The upper and lower "hinges" correspond to the first and
third quartiles (the 25th and 75th percentiles). This
differs slightly from the method used by the
\code{boxplot} function, and may be apparent with small
samples. See \code{\link{boxplot.stats}} for for more
information on how hinge positions are calculated for
\code{boxplot}.
The upper and lower "hinges" correspond to the first and third quartiles
(the 25th and 75th percentiles). This differs slightly from the method used
by the \code{boxplot} function, and may be apparent with small samples.
See \code{\link{boxplot.stats}} for for more information on how hinge
positions are calculated for \code{boxplot}.
}
\details{
The upper whisker extends from the hinge to the highest
value that is within 1.5 * IQR of the hinge, where IQR is
the inter-quartile range, or distance between the first
and third quartiles. The lower whisker extends from the
hinge to the lowest value within 1.5 * IQR of the hinge.
Data beyond the end of the whiskers are outliers and
plotted as points (as specified by Tukey).

In a notched box plot, the notches extend \code{1.58 *
IQR / sqrt(n)}. This gives a roughly 95% confidence
interval for comparing medians. See McGill et al. (1978)
for more details.
The upper whisker extends from the hinge to the highest value that is within
1.5 * IQR of the hinge, where IQR is the inter-quartile range, or distance
between the first and third quartiles. The lower whisker extends from the
hinge to the lowest value within 1.5 * IQR of the hinge. Data beyond the
end of the whiskers are outliers and plotted as points (as specified by Tukey).

In a notched box plot, the notches extend \code{1.58 * IQR / sqrt(n)}.
This gives a roughly 95% confidence interval for comparing medians.
See McGill et al. (1978) for more details.
}
\section{Aesthetics}{
\Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom",
"boxplot")}

\Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "boxplot")}
}
\examples{
\donttest{
Expand Down Expand Up @@ -124,16 +116,23 @@ 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")

# Update the defaults for the outliers by changing the defaults for geom_point

p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot()

update_geom_defaults("point", list(shape = 1, colour = "red", size = 5))
p + geom_boxplot()
}
}
\references{
McGill, R., Tukey, J. W. and Larsen, W. A. (1978)
Variations of box plots. The American Statistician 32,
12-16.
McGill, R., Tukey, J. W. and Larsen, W. A. (1978) Variations of
box plots. The American Statistician 32, 12-16.
}
\seealso{
\code{\link{stat_quantile}} to view quantiles conditioned
on a continuous variable, \code{\link{geom_jitter}} for
another way to look at conditional distributions"
\code{\link{stat_quantile}} to view quantiles conditioned on a
continuous variable, \code{\link{geom_jitter}} for another way to look
at conditional distributions"
}