The R Project SVN R

Rev

Rev 76503 | Rev 85977 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/graphics/man/boxplot.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
76503 maechler 3
% Copyright 1995-2019 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,
29414 maechler 24
        notch = FALSE, outline = TRUE, names, plot = TRUE,
25
        border = par("fg"), col = NULL, log = "",
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
65208 hornik 75
    (Chambers \emph{et al}, 1983, p.\sspace{}62).  See \code{\link{boxplot.stats}}
28474 ripley 76
    for the calculations used.}
29187 ripley 77
  \item{outline}{if \code{outline} is not true, the outliers are
78
    not drawn (as points whereas S+ uses lines).}% the argument name is most ugly but S+ compatible
43860 ripley 79
  \item{names}{group labels which will be printed under each boxplot.
80
    Can be a character vector or an \link{expression} (see
81
    \link{plotmath}).}
27442 ripley 82
  \item{boxwex}{a scale factor to be applied to all boxes.  When there
83
    are only a few groups, the appearance of the plot can be improved
84
    by making the boxes narrower.}
29414 maechler 85
  \item{staplewex}{staple line width expansion, proportional to box
86
    width.}
87
  \item{outwex}{outlier line width expansion, proportional to box
88
    width.}
27442 ripley 89
  \item{plot}{if \code{TRUE} (the default) then a boxplot is
90
    produced.  If not, the summaries which the boxplots are based on
91
    are returned.}
92
  \item{border}{an optional vector of colors for the outlines of the
93
    boxplots.  The values in \code{border} are recycled if the
94
    length of \code{border} is less than the number of plots.}
95
  \item{col}{if \code{col} is non-null it is assumed to contain colors
36139 ripley 96
    to be used to colour the bodies of the box plots. By default they
97
    are in the background colour.}
27442 ripley 98
  \item{log}{character indicating if x or y or both coordinates should
99
    be plotted in log scale.}
29414 maechler 100
  \item{pars}{a list of (potentially many) more graphical parameters,
101
    e.g., \code{boxwex} or \code{outpch}; these are passed to
102
    \code{\link{bxp}} (if \code{plot} is true); for details, see there.}
27442 ripley 103
  \item{horizontal}{logical indicating if the boxplots should be
104
    horizontal; default \code{FALSE} means vertical boxes.}
105
  \item{add}{logical, if true \emph{add} boxplot to current plot.}
106
  \item{at}{numeric vector giving the locations where the boxplots should
107
    be drawn, particularly when \code{add = TRUE};
108
    defaults to \code{1:n} where \code{n} is the number of boxes.}
109
}
110
\details{
111
  The generic function \code{boxplot} currently has a default method
29254 ripley 112
  (\code{boxplot.default}) and a formula interface (\code{boxplot.formula}).
29246 ripley 113
 
32316 ripley 114
  If multiple groups are supplied either as multiple arguments or via a
115
  formula, parallel boxplots will be plotted, in the order of the
116
  arguments or the order of the levels of the factor (see
117
  \code{\link{factor}}).
61433 ripley 118
 
29246 ripley 119
  Missing values are ignored when forming boxplots.
27442 ripley 120
}
121
\value{
122
  List with the following components:
123
  \item{stats}{a matrix, each column contains the extreme of the lower
124
    whisker, the lower hinge, the median, the upper hinge and the
35869 ripley 125
    extreme of the upper whisker for one group/plot.  If all the inputs
126
    have the same class attribute, so will this component.}
77323 maechler 127
  \item{n}{a vector with the number of (non-\code{\link{NA}}) observations in each group.}
27442 ripley 128
  \item{conf}{a matrix where each column contains the lower and upper
129
    extremes of the notch.}
130
  \item{out}{the values of any data points which lie beyond the
131
    extremes of the whiskers.}
132
  \item{group}{a vector of the same length as \code{out} whose elements
36276 duncan 133
    indicate to which group the outlier belongs.}
134
  \item{names}{a vector of names for the groups.}
27442 ripley 135
}
136
\references{
74265 hornik 137
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988).
27442 ripley 138
  \emph{The New S Language}.
47262 ripley 139
  Wadsworth & Brooks/Cole.
27442 ripley 140
 
74265 hornik 141
  Chambers, J. M., Cleveland, W. S., Kleiner, B. and Tukey, P. A. (1983).
142
  \emph{Graphical Methods for Data Analysis}.
143
  Wadsworth & Brooks/Cole.
28474 ripley 144
 
74265 hornik 145
  Murrell, P. (2005).
146
  \emph{R Graphics}.
147
  Chapman & Hall/CRC Press.
36222 ripley 148
 
27442 ripley 149
  See also \code{\link{boxplot.stats}}.
150
}
151
\seealso{
152
  \code{\link{boxplot.stats}} which does the computation,
29414 maechler 153
  \code{\link{bxp}} for the plotting and more examples;
27442 ripley 154
  and \code{\link{stripchart}} for an alternative (with small data
155
  sets).
156
}
157
\examples{
158
## boxplot on a formula:
159
boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
76503 maechler 160
# *add* notches (somewhat funny here <--> warning "notches .. outside hinges"):
27442 ripley 161
boxplot(count ~ spray, data = InsectSprays,
162
        notch = TRUE, add = TRUE, col = "blue")
163
 
76503 maechler 164
boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque",
165
        log = "y")
166
## horizontal=TRUE, switching  y <--> x :
167
boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque",
168
        log = "x", horizontal=TRUE)
27442 ripley 169
 
61153 ripley 170
rb <- boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque")
27442 ripley 171
title("Comparing boxplot()s and non-robust mean +/- SD")
172
mn.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, mean)
173
sd.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, sd)
174
xi <- 0.3 + seq(rb$n)
175
points(xi, mn.t, col = "orange", pch = 18)
176
arrows(xi, mn.t - sd.t, xi, mn.t + sd.t,
177
       code = 3, col = "pink", angle = 75, length = .1)
178
 
179
## boxplot on a matrix:
180
mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),
41227 ripley 181
             `5T` = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))
71718 maechler 182
boxplot(mat) # directly, calling boxplot.matrix()
183
 
184
## boxplot on a data frame:
185
df. <- as.data.frame(mat)
61153 ripley 186
par(las = 1) # all axis labels horizontal
71718 maechler 187
boxplot(df., main = "boxplot(*, horizontal = TRUE)", horizontal = TRUE)
27442 ripley 188
 
189
## Using 'at = ' and adding boxplots -- example idea by Roger Bivand :
190
boxplot(len ~ dose, data = ToothGrowth,
191
        boxwex = 0.25, at = 1:3 - 0.2,
32316 ripley 192
        subset = supp == "VC", col = "yellow",
193
        main = "Guinea Pigs' Tooth Growth",
194
        xlab = "Vitamin C dose mg",
42104 ripley 195
        ylab = "tooth length",
196
        xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")
27442 ripley 197
boxplot(len ~ dose, data = ToothGrowth, add = TRUE,
198
        boxwex = 0.25, at = 1:3 + 0.2,
32316 ripley 199
        subset = supp == "OJ", col = "orange")
27442 ripley 200
legend(2, 9, c("Ascorbic acid", "Orange juice"),
201
       fill = c("yellow", "orange"))
29414 maechler 202
 
70587 maechler 203
## With less effort (slightly different) using factor *interaction*:
204
boxplot(len ~ dose:supp, data = ToothGrowth,
205
        boxwex = 0.5, col = c("orange", "yellow"),
206
        main = "Guinea Pigs' Tooth Growth",
207
        xlab = "Vitamin C dose mg", ylab = "tooth length",
208
        sep = ":", lex.order = TRUE, ylim = c(0, 35), yaxs = "i")
209
 
29414 maechler 210
## more examples in  help(bxp)
27442 ripley 211
}
212
\keyword{hplot}