The R Project SVN R

Rev

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