| 42333 |
ripley |
1 |
% File src/library/graphics/man/boxplot.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 89794 |
maechler |
3 |
% Copyright 1995-2026 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 27442 |
ripley |
6 |
\name{boxplot}
|
| 56186 |
murdoch |
7 |
\alias{boxplot}
|
| 27442 |
ripley |
8 |
\alias{boxplot.default}
|
|
|
9 |
\alias{boxplot.formula}
|
|
|
10 |
\title{Box Plots}
|
|
|
11 |
\description{
|
|
|
12 |
Produce box-and-whisker plot(s) of the given (grouped) values.
|
|
|
13 |
}
|
|
|
14 |
\usage{
|
|
|
15 |
boxplot(x, \dots)
|
|
|
16 |
|
| 70587 |
maechler |
17 |
\method{boxplot}{formula}(formula, data = NULL, \dots, subset, na.action = NULL,
|
| 76503 |
maechler |
18 |
xlab = mklab(y_var = horizontal),
|
|
|
19 |
ylab = mklab(y_var =!horizontal),
|
|
|
20 |
add = FALSE, ann = !add, horizontal = FALSE,
|
| 70587 |
maechler |
21 |
drop = FALSE, sep = ".", lex.order = FALSE)
|
| 27442 |
ripley |
22 |
|
|
|
23 |
\method{boxplot}{default}(x, \dots, range = 1.5, width = NULL, varwidth = FALSE,
|
| 89794 |
maechler |
24 |
notch = FALSE, warnN = TRUE, outline = TRUE, names, plot = TRUE,
|
| 77849 |
murrell |
25 |
border = par("fg"), col = "lightgray", log = "",
|
| 29414 |
maechler |
26 |
pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5),
|
| 75560 |
ripley |
27 |
ann = !add, horizontal = FALSE, add = FALSE, at = NULL)
|
| 27442 |
ripley |
28 |
}
|
|
|
29 |
\arguments{
|
| 29246 |
ripley |
30 |
\item{formula}{a formula, such as \code{y ~ grp}, where \code{y} is a
|
|
|
31 |
numeric vector of data values to be split into groups according to
|
| 75488 |
maechler |
32 |
the grouping variable \code{grp} (usually a factor). Note that
|
|
|
33 |
\code{~ g1 + g2} is equivalent to \code{g1:g2}.}
|
| 27442 |
ripley |
34 |
\item{data}{a data.frame (or list) from which the variables in
|
|
|
35 |
\code{formula} should be taken.}
|
|
|
36 |
\item{subset}{an optional vector specifying a subset of observations
|
|
|
37 |
to be used for plotting.}
|
| 29246 |
ripley |
38 |
\item{na.action}{a function which indicates what should happen
|
|
|
39 |
when the data contain \code{NA}s. The default is to ignore missing
|
|
|
40 |
values in either the response or the group.}
|
| 75488 |
maechler |
41 |
\item{xlab, ylab}{x- and y-axis annotation, since \R 3.6.0 with a
|
|
|
42 |
non-empty default. Can be suppressed by \code{ann=FALSE}.}
|
|
|
43 |
\item{ann}{\code{\link{logical}} indicating if axes should be annotated (by
|
|
|
44 |
\code{xlab} and \code{ylab}).}
|
| 70587 |
maechler |
45 |
\item{drop, sep, lex.order}{passed to \code{\link{split.default}}, see there.}
|
| 27442 |
ripley |
46 |
\item{x}{for specifying data from which the boxplots are to be
|
| 29246 |
ripley |
47 |
produced. Either a numeric vector, or a single list containing such
|
|
|
48 |
vectors. Additional unnamed arguments specify further data
|
|
|
49 |
as separate vectors (each corresponding to a component boxplot).
|
|
|
50 |
\code{\link{NA}}s are allowed in the data.}
|
| 37119 |
ripley |
51 |
\item{\dots}{For the \code{formula} method, named arguments to be passed to
|
|
|
52 |
the default method.
|
| 27442 |
ripley |
53 |
|
|
|
54 |
For the default method, unnamed arguments are additional data
|
| 56113 |
ripley |
55 |
vectors (unless \code{x} is a list when they are ignored), and named
|
|
|
56 |
arguments are arguments and \link{graphical parameters} to be passed
|
|
|
57 |
to \code{\link{bxp}} in addition to the ones given by argument
|
|
|
58 |
\code{pars} (and override those in \code{pars}). Note that
|
|
|
59 |
\code{bxp} may or may not make use of graphical parameters it is
|
|
|
60 |
passed: see its documentation.
|
| 27442 |
ripley |
61 |
}
|
|
|
62 |
\item{range}{this determines how far the plot whiskers extend out
|
|
|
63 |
from the box. If \code{range} is positive, the whiskers extend
|
|
|
64 |
to the most extreme data point which is no more than
|
|
|
65 |
\code{range} times the interquartile range from the box. A value
|
|
|
66 |
of zero causes the whiskers to extend to the data extremes.}
|
|
|
67 |
\item{width}{a vector giving the relative widths of the boxes making
|
|
|
68 |
up the plot.}
|
|
|
69 |
\item{varwidth}{if \code{varwidth} is \code{TRUE}, the boxes are
|
|
|
70 |
drawn with widths proportional to the square-roots of the number
|
|
|
71 |
of observations in the groups.}
|
|
|
72 |
\item{notch}{if \code{notch} is \code{TRUE}, a notch is drawn in
|
|
|
73 |
each side of the boxes. If the notches of two plots do not
|
| 28474 |
ripley |
74 |
overlap this is \sQuote{strong evidence} that the two medians differ
|
| 90082 |
hornik |
75 |
\bibcitep{|R:Chambers+Cleveland+Kleiner:1983|p.\sspace{}62}.
|
|
|
76 |
See \code{\link{boxplot.stats}} for the calculations used.}
|
| 89794 |
maechler |
77 |
\item{warnN}{logical. When \code{notch = TRUE}, indicating if a
|
|
|
78 |
\code{\link{warning}} should be signalled when some notch gets
|
|
|
79 |
outside the hinges, i.e., the box.}
|
| 29187 |
ripley |
80 |
\item{outline}{if \code{outline} is not true, the outliers are
|
|
|
81 |
not drawn (as points whereas S+ uses lines).}% the argument name is most ugly but S+ compatible
|
| 43860 |
ripley |
82 |
\item{names}{group labels which will be printed under each boxplot.
|
|
|
83 |
Can be a character vector or an \link{expression} (see
|
|
|
84 |
\link{plotmath}).}
|
| 27442 |
ripley |
85 |
\item{boxwex}{a scale factor to be applied to all boxes. When there
|
|
|
86 |
are only a few groups, the appearance of the plot can be improved
|
|
|
87 |
by making the boxes narrower.}
|
| 29414 |
maechler |
88 |
\item{staplewex}{staple line width expansion, proportional to box
|
|
|
89 |
width.}
|
|
|
90 |
\item{outwex}{outlier line width expansion, proportional to box
|
|
|
91 |
width.}
|
| 27442 |
ripley |
92 |
\item{plot}{if \code{TRUE} (the default) then a boxplot is
|
|
|
93 |
produced. If not, the summaries which the boxplots are based on
|
|
|
94 |
are returned.}
|
|
|
95 |
\item{border}{an optional vector of colors for the outlines of the
|
|
|
96 |
boxplots. The values in \code{border} are recycled if the
|
|
|
97 |
length of \code{border} is less than the number of plots.}
|
|
|
98 |
\item{col}{if \code{col} is non-null it is assumed to contain colors
|
| 36139 |
ripley |
99 |
to be used to colour the bodies of the box plots. By default they
|
|
|
100 |
are in the background colour.}
|
| 27442 |
ripley |
101 |
\item{log}{character indicating if x or y or both coordinates should
|
|
|
102 |
be plotted in log scale.}
|
| 29414 |
maechler |
103 |
\item{pars}{a list of (potentially many) more graphical parameters,
|
|
|
104 |
e.g., \code{boxwex} or \code{outpch}; these are passed to
|
|
|
105 |
\code{\link{bxp}} (if \code{plot} is true); for details, see there.}
|
| 27442 |
ripley |
106 |
\item{horizontal}{logical indicating if the boxplots should be
|
|
|
107 |
horizontal; default \code{FALSE} means vertical boxes.}
|
|
|
108 |
\item{add}{logical, if true \emph{add} boxplot to current plot.}
|
|
|
109 |
\item{at}{numeric vector giving the locations where the boxplots should
|
|
|
110 |
be drawn, particularly when \code{add = TRUE};
|
|
|
111 |
defaults to \code{1:n} where \code{n} is the number of boxes.}
|
|
|
112 |
}
|
|
|
113 |
\details{
|
|
|
114 |
The generic function \code{boxplot} currently has a default method
|
| 29254 |
ripley |
115 |
(\code{boxplot.default}) and a formula interface (\code{boxplot.formula}).
|
| 29246 |
ripley |
116 |
|
| 32316 |
ripley |
117 |
If multiple groups are supplied either as multiple arguments or via a
|
|
|
118 |
formula, parallel boxplots will be plotted, in the order of the
|
|
|
119 |
arguments or the order of the levels of the factor (see
|
|
|
120 |
\code{\link{factor}}).
|
| 61433 |
ripley |
121 |
|
| 29246 |
ripley |
122 |
Missing values are ignored when forming boxplots.
|
| 27442 |
ripley |
123 |
}
|
|
|
124 |
\value{
|
|
|
125 |
List with the following components:
|
|
|
126 |
\item{stats}{a matrix, each column contains the extreme of the lower
|
|
|
127 |
whisker, the lower hinge, the median, the upper hinge and the
|
| 35869 |
ripley |
128 |
extreme of the upper whisker for one group/plot. If all the inputs
|
|
|
129 |
have the same class attribute, so will this component.}
|
| 77323 |
maechler |
130 |
\item{n}{a vector with the number of (non-\code{\link{NA}}) observations in each group.}
|
| 27442 |
ripley |
131 |
\item{conf}{a matrix where each column contains the lower and upper
|
|
|
132 |
extremes of the notch.}
|
|
|
133 |
\item{out}{the values of any data points which lie beyond the
|
|
|
134 |
extremes of the whiskers.}
|
|
|
135 |
\item{group}{a vector of the same length as \code{out} whose elements
|
| 36276 |
duncan |
136 |
indicate to which group the outlier belongs.}
|
|
|
137 |
\item{names}{a vector of names for the groups.}
|
| 27442 |
ripley |
138 |
}
|
|
|
139 |
\references{
|
| 88598 |
hornik |
140 |
\bibshow{R:Becker+Chambers+Wilks:1988,
|
|
|
141 |
R:Chambers+Cleveland+Kleiner:1983,
|
|
|
142 |
R:Murrell:2005}
|
| 27442 |
ripley |
143 |
|
|
|
144 |
See also \code{\link{boxplot.stats}}.
|
|
|
145 |
}
|
|
|
146 |
\seealso{
|
|
|
147 |
\code{\link{boxplot.stats}} which does the computation,
|
| 29414 |
maechler |
148 |
\code{\link{bxp}} for the plotting and more examples;
|
| 27442 |
ripley |
149 |
and \code{\link{stripchart}} for an alternative (with small data
|
|
|
150 |
sets).
|
|
|
151 |
}
|
|
|
152 |
\examples{
|
|
|
153 |
## boxplot on a formula:
|
|
|
154 |
boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
|
| 89794 |
maechler |
155 |
# *add* notches and do *not* warn about "notches .. outside hinges":
|
| 27442 |
ripley |
156 |
boxplot(count ~ spray, data = InsectSprays,
|
| 89794 |
maechler |
157 |
notch = TRUE, warnN = FALSE, add = TRUE, col = "blue")
|
| 27442 |
ripley |
158 |
|
| 76503 |
maechler |
159 |
boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque",
|
|
|
160 |
log = "y")
|
|
|
161 |
## horizontal=TRUE, switching y <--> x :
|
|
|
162 |
boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque",
|
|
|
163 |
log = "x", horizontal=TRUE)
|
| 27442 |
ripley |
164 |
|
| 61153 |
ripley |
165 |
rb <- boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque")
|
| 27442 |
ripley |
166 |
title("Comparing boxplot()s and non-robust mean +/- SD")
|
|
|
167 |
mn.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, mean)
|
|
|
168 |
sd.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, sd)
|
|
|
169 |
xi <- 0.3 + seq(rb$n)
|
|
|
170 |
points(xi, mn.t, col = "orange", pch = 18)
|
|
|
171 |
arrows(xi, mn.t - sd.t, xi, mn.t + sd.t,
|
|
|
172 |
code = 3, col = "pink", angle = 75, length = .1)
|
|
|
173 |
|
|
|
174 |
## boxplot on a matrix:
|
|
|
175 |
mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),
|
| 41227 |
ripley |
176 |
`5T` = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))
|
| 71718 |
maechler |
177 |
boxplot(mat) # directly, calling boxplot.matrix()
|
|
|
178 |
|
|
|
179 |
## boxplot on a data frame:
|
|
|
180 |
df. <- as.data.frame(mat)
|
| 61153 |
ripley |
181 |
par(las = 1) # all axis labels horizontal
|
| 71718 |
maechler |
182 |
boxplot(df., main = "boxplot(*, horizontal = TRUE)", horizontal = TRUE)
|
| 27442 |
ripley |
183 |
|
|
|
184 |
## Using 'at = ' and adding boxplots -- example idea by Roger Bivand :
|
|
|
185 |
boxplot(len ~ dose, data = ToothGrowth,
|
|
|
186 |
boxwex = 0.25, at = 1:3 - 0.2,
|
| 32316 |
ripley |
187 |
subset = supp == "VC", col = "yellow",
|
|
|
188 |
main = "Guinea Pigs' Tooth Growth",
|
|
|
189 |
xlab = "Vitamin C dose mg",
|
| 42104 |
ripley |
190 |
ylab = "tooth length",
|
|
|
191 |
xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")
|
| 27442 |
ripley |
192 |
boxplot(len ~ dose, data = ToothGrowth, add = TRUE,
|
|
|
193 |
boxwex = 0.25, at = 1:3 + 0.2,
|
| 32316 |
ripley |
194 |
subset = supp == "OJ", col = "orange")
|
| 27442 |
ripley |
195 |
legend(2, 9, c("Ascorbic acid", "Orange juice"),
|
|
|
196 |
fill = c("yellow", "orange"))
|
| 29414 |
maechler |
197 |
|
| 70587 |
maechler |
198 |
## With less effort (slightly different) using factor *interaction*:
|
|
|
199 |
boxplot(len ~ dose:supp, data = ToothGrowth,
|
|
|
200 |
boxwex = 0.5, col = c("orange", "yellow"),
|
|
|
201 |
main = "Guinea Pigs' Tooth Growth",
|
|
|
202 |
xlab = "Vitamin C dose mg", ylab = "tooth length",
|
|
|
203 |
sep = ":", lex.order = TRUE, ylim = c(0, 35), yaxs = "i")
|
|
|
204 |
|
| 29414 |
maechler |
205 |
## more examples in help(bxp)
|
| 27442 |
ripley |
206 |
}
|
|
|
207 |
\keyword{hplot}
|