Skip to content

Commit 89c6420

Browse files
unknownunknown
authored andcommitted
let geom_boxplot use the default colour, shape and size of geom_point
1 parent b316892 commit 89c6420

File tree

3 files changed

+79
-59
lines changed

3 files changed

+79
-59
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* The outliers of geom_boxplot() use the default colour, size and shape from
2+
geom_point(). Changing the defaults of geom_point() with
3+
update_geom_defaults() will apply the same changes to the outliers of
4+
geom_boxplot(). Changing the defaults for the outliers was previously not
5+
possible. (@ThierryO, #757)
6+
17
ggplot2 0.9.3.1
28
----------------------------------------------------------------
39

R/geom-boxplot.r

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#' continuous variable, \code{\link{geom_jitter}} for another way to look
2424
#' at conditional distributions"
2525
#' @inheritParams geom_point
26-
#' @param outlier.colour colour for outlying points
27-
#' @param outlier.shape shape of outlying points
28-
#' @param outlier.size size of outlying points
26+
#' @param outlier.colour colour for outlying points. Uses the default from geom_point().
27+
#' @param outlier.shape shape of outlying points. Uses the default from geom_point().
28+
#' @param outlier.size size of outlying points. Uses the default from geom_point().
2929
#' @param notch if \code{FALSE} (default) make a standard box plot. If
3030
#' \code{TRUE}, make a notched box plot. Notches are used to compare groups;
3131
#' if the notches of two boxes do not overlap, this is strong evidence that
@@ -92,10 +92,25 @@
9292
#' b + geom_boxplot(stat = "identity")
9393
#' b + geom_boxplot(stat = "identity") + coord_flip()
9494
#' b + geom_boxplot(aes(fill = X1), stat = "identity")
95+
#'
96+
#' # Update the defaults for the outliers by changing the defaults for geom_point
97+
#'
98+
#' p <- ggplot(mtcars, aes(factor(cyl), mpg))
99+
#' p + geom_boxplot()
100+
#'
101+
#' update_geom_defaults("point", list(shape = 1, colour = "red", size = 5))
102+
#' p + geom_boxplot()
95103
#' }
96104
geom_boxplot <- function (mapping = NULL, data = NULL, stat = "boxplot", position = "dodge",
97-
outlier.colour = "black", outlier.shape = 16, outlier.size = 2,
105+
outlier.colour = NULL, outlier.shape = NULL, outlier.size = NULL,
98106
notch = FALSE, notchwidth = .5, ...) {
107+
108+
outlier_defaults <- Geom$find('point')$default_aes()
109+
110+
outlier.colour <- outlier.colour %||% outlier_defaults$colour
111+
outlier.shape <- outlier.shape %||% outlier_defaults$shape
112+
outlier.size <- outlier.size %||% outlier_defaults$size
113+
99114
GeomBoxplot$new(mapping = mapping, data = data, stat = stat,
100115
position = position, outlier.colour = outlier.colour, outlier.shape = outlier.shape,
101116
outlier.size = outlier.size, notch = notch, notchwidth = notchwidth, ...)

man/geom_boxplot.Rd

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,65 @@
1+
% Generated by roxygen2 (4.0.0): do not edit by hand
12
\name{geom_boxplot}
23
\alias{geom_boxplot}
34
\title{Box and whiskers plot.}
45
\usage{
5-
geom_boxplot(mapping = NULL, data = NULL,
6-
stat = "boxplot", position = "dodge",
7-
outlier.colour = "black", outlier.shape = 16,
8-
outlier.size = 2, notch = FALSE, notchwidth = 0.5, ...)
6+
geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot",
7+
position = "dodge", outlier.colour = NULL, outlier.shape = NULL,
8+
outlier.size = NULL, notch = FALSE, notchwidth = 0.5, ...)
99
}
1010
\arguments{
11-
\item{outlier.colour}{colour for outlying points}
11+
\item{outlier.colour}{colour for outlying points. Uses the default from geom_point().}
1212

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

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

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

23-
\item{notchwidth}{for a notched box plot, width of the
24-
notch relative to the body (default 0.5)}
22+
\item{notchwidth}{for a notched box plot, width of the notch relative to
23+
the body (default 0.5)}
2524

26-
\item{mapping}{The aesthetic mapping, usually constructed
27-
with \code{\link{aes}} or \code{\link{aes_string}}. Only
28-
needs to be set at the layer level if you are overriding
29-
the plot defaults.}
25+
\item{mapping}{The aesthetic mapping, usually constructed with
26+
\code{\link{aes}} or \code{\link{aes_string}}. Only needs to be set
27+
at the layer level if you are overriding the plot defaults.}
3028

31-
\item{data}{A layer specific dataset - only needed if you
32-
want to override the plot defaults.}
29+
\item{data}{A layer specific dataset - only needed if you want to override
30+
the plot defaults.}
3331

34-
\item{stat}{The statistical transformation to use on the
35-
data for this layer.}
32+
\item{stat}{The statistical transformation to use on the data for this
33+
layer.}
3634

37-
\item{position}{The position adjustment to use for
38-
overlappling points on this layer}
35+
\item{position}{The position adjustment to use for overlapping points
36+
on this layer}
3937

40-
\item{...}{other arguments passed on to
41-
\code{\link{layer}}. This can include aesthetics whose
42-
values you want to set, not map. See \code{\link{layer}}
43-
for more details.}
38+
\item{...}{other arguments passed on to \code{\link{layer}}. This can
39+
include aesthetics whose values you want to set, not map. See
40+
\code{\link{layer}} for more details.}
4441
}
4542
\description{
46-
The upper and lower "hinges" correspond to the first and
47-
third quartiles (the 25th and 75th percentiles). This
48-
differs slightly from the method used by the
49-
\code{boxplot} function, and may be apparent with small
50-
samples. See \code{\link{boxplot.stats}} for for more
51-
information on how hinge positions are calculated for
52-
\code{boxplot}.
43+
The upper and lower "hinges" correspond to the first and third quartiles
44+
(the 25th and 75th percentiles). This differs slightly from the method used
45+
by the \code{boxplot} function, and may be apparent with small samples.
46+
See \code{\link{boxplot.stats}} for for more information on how hinge
47+
positions are calculated for \code{boxplot}.
5348
}
5449
\details{
55-
The upper whisker extends from the hinge to the highest
56-
value that is within 1.5 * IQR of the hinge, where IQR is
57-
the inter-quartile range, or distance between the first
58-
and third quartiles. The lower whisker extends from the
59-
hinge to the lowest value within 1.5 * IQR of the hinge.
60-
Data beyond the end of the whiskers are outliers and
61-
plotted as points (as specified by Tukey).
62-
63-
In a notched box plot, the notches extend \code{1.58 *
64-
IQR / sqrt(n)}. This gives a roughly 95% confidence
65-
interval for comparing medians. See McGill et al. (1978)
66-
for more details.
50+
The upper whisker extends from the hinge to the highest value that is within
51+
1.5 * IQR of the hinge, where IQR is the inter-quartile range, or distance
52+
between the first and third quartiles. The lower whisker extends from the
53+
hinge to the lowest value within 1.5 * IQR of the hinge. Data beyond the
54+
end of the whiskers are outliers and plotted as points (as specified by Tukey).
55+
56+
In a notched box plot, the notches extend \code{1.58 * IQR / sqrt(n)}.
57+
This gives a roughly 95% confidence interval for comparing medians.
58+
See McGill et al. (1978) for more details.
6759
}
6860
\section{Aesthetics}{
69-
\Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom",
70-
"boxplot")}
61+
62+
\Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "boxplot")}
7163
}
7264
\examples{
7365
\donttest{
@@ -124,16 +116,23 @@ b <- ggplot(abc, aes(x = X1, ymin = `0\%`, lower = `25\%`, middle = `50\%`, uppe
124116
b + geom_boxplot(stat = "identity")
125117
b + geom_boxplot(stat = "identity") + coord_flip()
126118
b + geom_boxplot(aes(fill = X1), stat = "identity")
119+
120+
# Update the defaults for the outliers by changing the defaults for geom_point
121+
122+
p <- ggplot(mtcars, aes(factor(cyl), mpg))
123+
p + geom_boxplot()
124+
125+
update_geom_defaults("point", list(shape = 1, colour = "red", size = 5))
126+
p + geom_boxplot()
127127
}
128128
}
129129
\references{
130-
McGill, R., Tukey, J. W. and Larsen, W. A. (1978)
131-
Variations of box plots. The American Statistician 32,
132-
12-16.
130+
McGill, R., Tukey, J. W. and Larsen, W. A. (1978) Variations of
131+
box plots. The American Statistician 32, 12-16.
133132
}
134133
\seealso{
135-
\code{\link{stat_quantile}} to view quantiles conditioned
136-
on a continuous variable, \code{\link{geom_jitter}} for
137-
another way to look at conditional distributions"
134+
\code{\link{stat_quantile}} to view quantiles conditioned on a
135+
continuous variable, \code{\link{geom_jitter}} for another way to look
136+
at conditional distributions"
138137
}
139138

0 commit comments

Comments
 (0)