Rev 47262 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/graphics/man/boxplot.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2008 R Core Development Team% Distributed under GPL 2 or later\name{boxplot}\alias{boxplot}\alias{boxplot.default}\alias{boxplot.formula}\title{Box Plots}\description{Produce box-and-whisker plot(s) of the given (grouped) values.}\usage{boxplot(x, \dots)\method{boxplot}{formula}(formula, data = NULL, \dots, subset, na.action = NULL)\method{boxplot}{default}(x, \dots, range = 1.5, width = NULL, varwidth = FALSE,notch = FALSE, outline = TRUE, names, plot = TRUE,border = par("fg"), col = NULL, log = "",pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5),horizontal = FALSE, add = FALSE, at = NULL)}\arguments{\item{formula}{a formula, such as \code{y ~ grp}, where \code{y} is anumeric vector of data values to be split into groups according tothe grouping variable \code{grp} (usually a factor).}\item{data}{a data.frame (or list) from which the variables in\code{formula} should be taken.}\item{subset}{an optional vector specifying a subset of observationsto be used for plotting.}\item{na.action}{a function which indicates what should happenwhen the data contain \code{NA}s. The default is to ignore missingvalues in either the response or the group.}\item{x}{for specifying data from which the boxplots are to beproduced. Either a numeric vector, or a single list containing suchvectors. Additional unnamed arguments specify further dataas separate vectors (each corresponding to a component boxplot).\code{\link{NA}}s are allowed in the data.}\item{\dots}{For the \code{formula} method, named arguments to be passed tothe default method.For the default method, unnamed arguments are additional datavectors (unless \code{x} is a list when they are ignored),and named arguments are arguments and graphical parameters to bepassed to \code{\link{bxp}} in addition to the onesgiven by argument \code{pars} (and override those in \code{pars}).}\item{range}{this determines how far the plot whiskers extend outfrom the box. If \code{range} is positive, the whiskers extendto the most extreme data point which is no more than\code{range} times the interquartile range from the box. A valueof zero causes the whiskers to extend to the data extremes.}\item{width}{a vector giving the relative widths of the boxes makingup the plot.}\item{varwidth}{if \code{varwidth} is \code{TRUE}, the boxes aredrawn with widths proportional to the square-roots of the numberof observations in the groups.}\item{notch}{if \code{notch} is \code{TRUE}, a notch is drawn ineach side of the boxes. If the notches of two plots do notoverlap this is \sQuote{strong evidence} that the two medians differ(Chambers \emph{et al.}, 1983, p. 62). See \code{\link{boxplot.stats}}for the calculations used.}\item{outline}{if \code{outline} is not true, the outliers arenot drawn (as points whereas S+ uses lines).}% the argument name is most ugly but S+ compatible\item{names}{group labels which will be printed under each boxplot.Can be a character vector or an \link{expression} (see\link{plotmath}).}\item{boxwex}{a scale factor to be applied to all boxes. When thereare only a few groups, the appearance of the plot can be improvedby making the boxes narrower.}\item{staplewex}{staple line width expansion, proportional to boxwidth.}\item{outwex}{outlier line width expansion, proportional to boxwidth.}\item{plot}{if \code{TRUE} (the default) then a boxplot isproduced. If not, the summaries which the boxplots are based onare returned.}\item{border}{an optional vector of colors for the outlines of theboxplots. The values in \code{border} are recycled if thelength of \code{border} is less than the number of plots.}\item{col}{if \code{col} is non-null it is assumed to contain colorsto be used to colour the bodies of the box plots. By default theyare in the background colour.}\item{log}{character indicating if x or y or both coordinates shouldbe plotted in log scale.}\item{pars}{a list of (potentially many) more graphical parameters,e.g., \code{boxwex} or \code{outpch}; these are passed to\code{\link{bxp}} (if \code{plot} is true); for details, see there.}\item{horizontal}{logical indicating if the boxplots should behorizontal; default \code{FALSE} means vertical boxes.}\item{add}{logical, if true \emph{add} boxplot to current plot.}\item{at}{numeric vector giving the locations where the boxplots shouldbe drawn, particularly when \code{add = TRUE};defaults to \code{1:n} where \code{n} is the number of boxes.}}\details{The generic function \code{boxplot} currently has a default method(\code{boxplot.default}) and a formula interface (\code{boxplot.formula}).If multiple groups are supplied either as multiple arguments or via aformula, parallel boxplots will be plotted, in the order of thearguments or the order of the levels of the factor (see\code{\link{factor}}).Missing values are ignored when forming boxplots.}\value{List with the following components:\item{stats}{a matrix, each column contains the extreme of the lowerwhisker, the lower hinge, the median, the upper hinge and theextreme of the upper whisker for one group/plot. If all the inputshave the same class attribute, so will this component.}\item{n}{a vector with the number of observations in each group.}\item{conf}{a matrix where each column contains the lower and upperextremes of the notch.}\item{out}{the values of any data points which lie beyond theextremes of the whiskers.}\item{group}{a vector of the same length as \code{out} whose elementsindicate to which group the outlier belongs.}\item{names}{a vector of names for the groups.}}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.Chambers, J. M., Cleveland, W. S., Kleiner, B. and Tukey, P. A. (1983)\emph{Graphical Methods for Data Analysis.} Wadsworth & Brooks/Cole.Murrell, P. (2005) \emph{R Graphics}. Chapman & Hall/CRC Press.See also \code{\link{boxplot.stats}}.}\seealso{\code{\link{boxplot.stats}} which does the computation,\code{\link{bxp}} for the plotting and more examples;and \code{\link{stripchart}} for an alternative (with small datasets).}\examples{## boxplot on a formula:boxplot(count ~ spray, data = InsectSprays, col = "lightgray")# *add* notches (somewhat funny here):boxplot(count ~ spray, data = InsectSprays,notch = TRUE, add = TRUE, col = "blue")boxplot(decrease ~ treatment, data = OrchardSprays,log = "y", col = "bisque")rb <- boxplot(decrease ~ treatment, data = OrchardSprays, col="bisque")title("Comparing boxplot()s and non-robust mean +/- SD")mn.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, mean)sd.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, sd)xi <- 0.3 + seq(rb$n)points(xi, mn.t, col = "orange", pch = 18)arrows(xi, mn.t - sd.t, xi, mn.t + sd.t,code = 3, col = "pink", angle = 75, length = .1)## boxplot on a matrix:mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),`5T` = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))boxplot(as.data.frame(mat),main = "boxplot(as.data.frame(mat), main = ...)")par(las=1)# all axis labels horizontalboxplot(as.data.frame(mat), main = "boxplot(*, horizontal = TRUE)",horizontal = TRUE)## Using 'at = ' and adding boxplots -- example idea by Roger Bivand :boxplot(len ~ dose, data = ToothGrowth,boxwex = 0.25, at = 1:3 - 0.2,subset = supp == "VC", col = "yellow",main = "Guinea Pigs' Tooth Growth",xlab = "Vitamin C dose mg",ylab = "tooth length",xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")boxplot(len ~ dose, data = ToothGrowth, add = TRUE,boxwex = 0.25, at = 1:3 + 0.2,subset = supp == "OJ", col = "orange")legend(2, 9, c("Ascorbic acid", "Orange juice"),fill = c("yellow", "orange"))## more examples in help(bxp)}\keyword{hplot}