The R Project SVN R

Rev

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

Rev Author Line No. Line
27442 ripley 1
\name{boxplot}
2
\alias{boxplot}
3
\alias{boxplot.default}
4
\alias{boxplot.formula}
5
\title{Box Plots}
6
\description{
7
  Produce box-and-whisker plot(s) of the given (grouped) values.
8
}
9
\usage{
10
boxplot(x, \dots)
11
 
12
\method{boxplot}{formula}(formula, data = NULL, \dots, subset)
13
 
14
\method{boxplot}{default}(x, \dots, range = 1.5, width = NULL, varwidth = FALSE,
15
        notch = FALSE, outline = TRUE, names, boxwex = 0.8, plot = TRUE,
16
        border = par("fg"), col = NULL, log = "", pars = NULL,
17
        horizontal = FALSE, add = FALSE, at = NULL)
18
}
19
\arguments{
20
  \item{formula}{a formula, such as \code{y ~ x}.}
21
  \item{data}{a data.frame (or list) from which the variables in
22
    \code{formula} should be taken.}
23
  \item{subset}{an optional vector specifying a subset of observations
24
    to be used for plotting.}
25
  \item{x}{for specifying data from which the boxplots are to be
26
    produced as well as for giving graphical parameters.  Additional
27
    unnamed  arguments specify further data, either
28
    as separate vectors (each corresponding to a component boxplot) or
29
    as a single list containing such vectors.  \code{\link{NA}}s are
30
    allowed in the data.}
31
  \item{\dots}{For the \code{formula} method, arguments to the
32
    default method and graphical parameters.
33
 
34
    For the default method, unnamed arguments are additional data
35
    vectors, and named arguments are graphical parameters in addition
36
    to the ones given by argument \code{pars}.
37
  }
38
  \item{range}{this determines how far the plot whiskers extend out
39
    from the box.  If \code{range} is positive, the whiskers extend
40
    to the most extreme data point which is no more than
41
    \code{range} times the interquartile range from the box. A value
42
    of zero causes the whiskers to extend to the data extremes.}
43
  \item{width}{a vector giving the relative widths of the boxes making
44
    up the plot.}
45
  \item{varwidth}{if \code{varwidth} is \code{TRUE}, the boxes are
46
    drawn with widths proportional to the square-roots of the number
47
    of observations in the groups.}
48
  \item{notch}{if \code{notch} is \code{TRUE}, a notch is drawn in
49
    each side of the boxes.  If the notches of two plots do not
50
    overlap then the medians are significantly different at the 5
51
    percent level.}
52
  \item{outline}{if \code{outline} is not true, the boxplot lines are
53
    not drawn.}% the argument name is most ugly but S+ compatible
54
  \item{names}{group labels which will be printed under each boxplot.}
55
  \item{boxwex}{a scale factor to be applied to all boxes.  When there
56
    are only a few groups, the appearance of the plot can be improved
57
    by making the boxes narrower.}
58
  \item{plot}{if \code{TRUE} (the default) then a boxplot is
59
    produced.  If not, the summaries which the boxplots are based on
60
    are returned.}
61
  \item{border}{an optional vector of colors for the outlines of the
62
    boxplots.  The values in \code{border} are recycled if the
63
    length of \code{border} is less than the number of plots.}
64
  \item{col}{if \code{col} is non-null it is assumed to contain colors
27625 ripley 65
    to be used to colour the bodies of the box plots.}
27442 ripley 66
  \item{log}{character indicating if x or y or both coordinates should
67
    be plotted in log scale.}
68
  \item{pars}{a list of graphical parameters; these are passed to
69
    \code{\link{bxp}} (if \code{plot} is true).}
70
  \item{horizontal}{logical indicating if the boxplots should be
71
    horizontal; default \code{FALSE} means vertical boxes.}
72
  \item{add}{logical, if true \emph{add} boxplot to current plot.}
73
  \item{at}{numeric vector giving the locations where the boxplots should
74
    be drawn, particularly when \code{add = TRUE};
75
    defaults to \code{1:n} where \code{n} is the number of boxes.}
76
}
77
\details{
78
  The generic function \code{boxplot} currently has a default method
79
  (\code{boxplot.default}) and a formula interface
80
  (\code{\link{boxplot.formula}}).
81
}
82
\value{
83
  List with the following components:
84
  \item{stats}{a matrix, each column contains the extreme of the lower
85
    whisker, the lower hinge, the median, the upper hinge and the
86
    extreme of the upper whisker for one group/plot.}
87
  \item{n}{a vector with the number of observations in each group.}
88
  \item{conf}{a matrix where each column contains the lower and upper
89
    extremes of the notch.}
90
  \item{out}{the values of any data points which lie beyond the
91
    extremes of the whiskers.}
92
  \item{group}{a vector of the same length as \code{out} whose elements
93
    indicate which group the outlier belongs to}
94
  \item{names}{a vector of names for the groups}
95
}
96
\references{
97
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
98
  \emph{The New S Language}.
99
  Wadsworth \& Brooks/Cole.
100
 
101
  See also \code{\link{boxplot.stats}}.
102
}
103
\seealso{
104
  \code{\link{boxplot.stats}} which does the computation,
105
  \code{\link{bxp}} for the plotting;
106
  and \code{\link{stripchart}} for an alternative (with small data
107
  sets).
108
}
109
\examples{
110
## boxplot on a formula:
111
data(InsectSprays)
112
boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
113
# *add* notches (somewhat funny here):
114
boxplot(count ~ spray, data = InsectSprays,
115
        notch = TRUE, add = TRUE, col = "blue")
116
 
117
data(OrchardSprays)
118
boxplot(decrease ~ treatment, data = OrchardSprays,
119
        log = "y", col="bisque")
120
 
121
rb <- boxplot(decrease ~ treatment, data = OrchardSprays, col="bisque")
122
title("Comparing boxplot()s and non-robust mean +/- SD")
123
 
124
mn.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, mean)
125
sd.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, sd)
126
xi <- 0.3 + seq(rb$n)
127
points(xi, mn.t, col = "orange", pch = 18)
128
arrows(xi, mn.t - sd.t, xi, mn.t + sd.t,
129
       code = 3, col = "pink", angle = 75, length = .1)
130
 
131
## boxplot on a matrix:
132
mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),
133
             T5 = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))
134
boxplot(data.frame(mat), main = "boxplot(data.frame(mat), main = ...)")
135
par(las=1)# all axis labels horizontal
136
boxplot(data.frame(mat), main = "boxplot(*, horizontal = TRUE)",
137
        horizontal = TRUE)
138
 
139
## Using 'at = ' and adding boxplots -- example idea by Roger Bivand :
140
 
141
data(ToothGrowth)
142
boxplot(len ~ dose, data = ToothGrowth,
143
        boxwex = 0.25, at = 1:3 - 0.2,
144
        subset= supp == "VC", col="yellow",
145
        main="Guinea Pigs' Tooth Growth",
146
        xlab="Vitamin C dose mg",
147
        ylab="tooth length", ylim=c(0,35))
148
boxplot(len ~ dose, data = ToothGrowth, add = TRUE,
149
        boxwex = 0.25, at = 1:3 + 0.2,
150
        subset= supp == "OJ", col="orange")
151
legend(2, 9, c("Ascorbic acid", "Orange juice"),
152
       fill = c("yellow", "orange"))
153
}
154
\keyword{hplot}