| 42333 |
ripley |
1 |
% File src/library/graphics/man/barplot.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 76259 |
maechler |
3 |
% Copyright 1995-2019 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 27442 |
ripley |
6 |
\name{barplot}
|
| 56186 |
murdoch |
7 |
\alias{barplot}
|
| 27442 |
ripley |
8 |
\alias{barplot.default}
|
| 76259 |
maechler |
9 |
\alias{barplot.formula}
|
| 27442 |
ripley |
10 |
\title{Bar Plots}
|
| 28517 |
hornik |
11 |
\description{
|
|
|
12 |
Creates a bar plot with vertical or horizontal bars.
|
|
|
13 |
}
|
| 27442 |
ripley |
14 |
\usage{
|
| 36139 |
ripley |
15 |
barplot(height, \dots)
|
|
|
16 |
|
| 27442 |
ripley |
17 |
\method{barplot}{default}(height, width = 1, space = NULL,
|
|
|
18 |
names.arg = NULL, legend.text = NULL, beside = FALSE,
|
|
|
19 |
horiz = FALSE, density = NULL, angle = 45,
|
| 29534 |
ripley |
20 |
col = NULL, border = par("fg"),
|
| 27442 |
ripley |
21 |
main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
|
| 34030 |
maechler |
22 |
xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
|
| 27442 |
ripley |
23 |
axes = TRUE, axisnames = TRUE,
|
|
|
24 |
cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
|
| 34030 |
maechler |
25 |
inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
|
| 76259 |
maechler |
26 |
add = FALSE, ann = !add && par("ann"), args.legend = NULL, \dots)
|
|
|
27 |
|
|
|
28 |
\method{barplot}{formula}(formula, data, subset, na.action,
|
|
|
29 |
horiz = FALSE, xlab = NULL, ylab = NULL, \dots)
|
| 27442 |
ripley |
30 |
}
|
|
|
31 |
\arguments{
|
|
|
32 |
\item{height}{either a vector or matrix of values describing the
|
|
|
33 |
bars which make up the plot. If \code{height} is a vector, the
|
|
|
34 |
plot consists of a sequence of rectangular bars with heights
|
|
|
35 |
given by the values in the vector. If \code{height} is a matrix
|
|
|
36 |
and \code{beside} is \code{FALSE} then each bar of the plot
|
|
|
37 |
corresponds to a column of \code{height}, with the values in the
|
| 42961 |
ripley |
38 |
column giving the heights of stacked sub-bars making up the
|
| 27442 |
ripley |
39 |
bar. If \code{height} is a matrix and \code{beside} is
|
|
|
40 |
\code{TRUE}, then the values in each column are juxtaposed
|
|
|
41 |
rather than stacked.}
|
|
|
42 |
\item{width}{optional vector of bar widths. Re-cycled to length the
|
| 61433 |
ripley |
43 |
number of bars drawn. Specifying a single value will have no
|
| 42373 |
murdoch |
44 |
visible effect unless \code{xlim} is specified.}
|
| 27442 |
ripley |
45 |
\item{space}{the amount of space (as a fraction of the average bar
|
|
|
46 |
width) left before each bar. May be given as a single number or
|
|
|
47 |
one number per bar. If \code{height} is a matrix and
|
|
|
48 |
\code{beside} is \code{TRUE}, \code{space} may be specified by
|
|
|
49 |
two numbers, where the first is the space between bars in the
|
|
|
50 |
same group, and the second the space between the groups. If not
|
|
|
51 |
given explicitly, it defaults to \code{c(0,1)} if \code{height}
|
|
|
52 |
is a matrix and \code{beside} is \code{TRUE}, and to 0.2
|
|
|
53 |
otherwise.}
|
|
|
54 |
\item{names.arg}{a vector of names to be plotted below each bar or
|
|
|
55 |
group of bars. If this argument is omitted, then the names are
|
|
|
56 |
taken from the \code{names} attribute of \code{height} if this
|
|
|
57 |
is a vector, or the column names if it is a matrix.}
|
|
|
58 |
\item{legend.text}{a vector of text used to construct a legend for
|
|
|
59 |
the plot, or a logical indicating whether a legend should be
|
|
|
60 |
included. This is only useful when \code{height} is a matrix.
|
|
|
61 |
In that case given legend labels should correspond to the rows of
|
|
|
62 |
\code{height}; if \code{legend.text} is true, the row names of
|
|
|
63 |
\code{height} will be used as labels if they are non-null.}
|
|
|
64 |
\item{beside}{a logical value. If \code{FALSE}, the columns of
|
|
|
65 |
\code{height} are portrayed as stacked bars, and if \code{TRUE}
|
|
|
66 |
the columns are portrayed as juxtaposed bars.}
|
|
|
67 |
\item{horiz}{a logical value. If \code{FALSE}, the bars are drawn
|
|
|
68 |
vertically with the first bar to the left. If \code{TRUE}, the
|
|
|
69 |
bars are drawn horizontally with the first at the bottom.}
|
| 27625 |
ripley |
70 |
\item{density}{a vector giving the density of shading lines, in
|
| 27442 |
ripley |
71 |
lines per inch, for the bars or bar components.
|
|
|
72 |
The default value of \code{NULL} means that no shading lines
|
|
|
73 |
are drawn. Non-positive values of \code{density} also inhibit the
|
|
|
74 |
drawing of shading lines.}
|
|
|
75 |
\item{angle}{the slope of shading lines, given as an angle in
|
|
|
76 |
degrees (counter-clockwise), for the bars or bar components.}
|
| 29534 |
ripley |
77 |
\item{col}{a vector of colors for the bars or bar components.
|
| 29769 |
hornik |
78 |
By default, grey is used if \code{height} is a vector, and a
|
|
|
79 |
gamma-corrected grey palette if \code{height} is a matrix.}
|
| 39762 |
murrell |
80 |
\item{border}{the color to be used for the border of the bars.
|
|
|
81 |
Use \code{border = NA} to omit borders. If there are shading
|
|
|
82 |
lines, \code{border = TRUE} means use the same colour for
|
|
|
83 |
the border as for the shading lines.}
|
| 27442 |
ripley |
84 |
\item{main,sub}{overall and sub title for the plot.}
|
|
|
85 |
\item{xlab}{a label for the x axis.}
|
|
|
86 |
\item{ylab}{a label for the y axis.}
|
|
|
87 |
\item{xlim}{limits for the x axis.}
|
|
|
88 |
\item{ylim}{limits for the y axis.}
|
|
|
89 |
\item{xpd}{logical. Should bars be allowed to go outside region?}
|
| 34030 |
maechler |
90 |
\item{log}{string specifying if axis scales should be logarithmic; see
|
|
|
91 |
\code{\link{plot.default}}.}
|
| 27442 |
ripley |
92 |
\item{axes}{logical. If \code{TRUE}, a vertical (or horizontal, if
|
|
|
93 |
\code{horiz} is true) axis is drawn.}
|
|
|
94 |
\item{axisnames}{logical. If \code{TRUE}, and if there are
|
|
|
95 |
\code{names.arg} (see above), the
|
| 61153 |
ripley |
96 |
other axis is drawn (with \code{lty = 0}) and labeled.}
|
| 77420 |
kalibera |
97 |
\item{cex.axis}{expansion factor for numeric axis labels (see
|
|
|
98 |
\code{\link{par}('cex')}).}
|
| 27442 |
ripley |
99 |
\item{cex.names}{expansion factor for axis names (bar labels).}
|
|
|
100 |
\item{inside}{logical. If \code{TRUE}, the lines which divide
|
|
|
101 |
adjacent (non-stacked!) bars will be drawn. Only applies when
|
|
|
102 |
\code{space = 0} (which it partly is when \code{beside = TRUE}).}
|
| 40281 |
ripley |
103 |
%MM: we still have "NotYetUsed" -- the above describes S-plus 6.1 (not
|
| 27442 |
ripley |
104 |
% precisely documented!) behavior!
|
|
|
105 |
\item{plot}{logical. If \code{FALSE}, nothing is plotted.}
|
| 77420 |
kalibera |
106 |
\item{axis.lty}{the graphics parameter \code{lty} (see
|
|
|
107 |
\code{\link{par}('lty')}) applied to the axis
|
| 27625 |
ripley |
108 |
and tick marks of the categorical (default horizontal) axis. Note
|
| 27442 |
ripley |
109 |
that by default the axis is suppressed.}
|
| 28517 |
hornik |
110 |
\item{offset}{a vector indicating how much the bars should be shifted
|
|
|
111 |
relative to the x axis.}
|
| 34030 |
maechler |
112 |
\item{add}{logical specifying if bars should be added to an already
|
|
|
113 |
existing plot; defaults to \code{FALSE}.}
|
| 76259 |
maechler |
114 |
\item{ann}{logical specifying if the default annotation (\code{main},
|
|
|
115 |
\code{sub}, \code{xlab}, \code{ylab}) should appear on the plot, see
|
|
|
116 |
\code{\link{title}}.}
|
| 46980 |
ripley |
117 |
\item{args.legend}{list of additional arguments to pass to
|
| 46986 |
ripley |
118 |
\code{\link{legend}()}; names of the list are used as argument
|
| 46980 |
ripley |
119 |
names. Only used if \code{legend.text} is supplied.}
|
| 76259 |
maechler |
120 |
\item{formula}{a formula where the \code{y} variables are numeric data
|
|
|
121 |
to plot against the categorical \code{x} variables. The formula
|
| 77213 |
hornik |
122 |
can have one of three forms:
|
|
|
123 |
\preformatted{
|
| 76259 |
maechler |
124 |
y ~ x
|
|
|
125 |
y ~ x1 + x2
|
| 77213 |
hornik |
126 |
cbind(y1, y2) ~ x
|
|
|
127 |
}
|
|
|
128 |
(see the examples).}
|
| 76259 |
maechler |
129 |
\item{data}{a data frame (or list) from which the variables in formula
|
|
|
130 |
should be taken.}
|
|
|
131 |
\item{subset}{an optional vector specifying a subset of observations
|
|
|
132 |
to be used.}
|
|
|
133 |
\item{na.action}{a function which indicates what should happen when
|
|
|
134 |
the data contain \code{\link{NA}} values. The default is to ignore missing
|
|
|
135 |
values in the given variables.}
|
| 36139 |
ripley |
136 |
\item{\dots}{arguments to be passed to/from other methods. For the
|
| 37119 |
ripley |
137 |
default method these can include further arguments (such as
|
| 56113 |
ripley |
138 |
\code{axes}, \code{asp} and \code{main}) and
|
|
|
139 |
\link{graphical parameters} (see \code{\link{par}}) which are passed to
|
| 37119 |
ripley |
140 |
\code{\link{plot.window}()}, \code{\link{title}()} and
|
| 27442 |
ripley |
141 |
\code{\link{axis}}.}
|
|
|
142 |
}
|
|
|
143 |
\value{
|
|
|
144 |
A numeric vector (or matrix, when \code{beside = TRUE}), say
|
|
|
145 |
\code{mp}, giving the coordinates of \emph{all} the bar midpoints
|
|
|
146 |
drawn, useful for adding to the graph.
|
|
|
147 |
|
|
|
148 |
If \code{beside} is true, use \code{colMeans(mp)} for the
|
|
|
149 |
midpoints of each \emph{group} of bars, see example.
|
|
|
150 |
}
|
| 76259 |
maechler |
151 |
\author{R Core, with a contribution by Arni Magnusson.}
|
| 27442 |
ripley |
152 |
\references{
|
|
|
153 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
|
|
154 |
\emph{The New S Language}.
|
| 47262 |
ripley |
155 |
Wadsworth & Brooks/Cole.
|
| 36222 |
ripley |
156 |
|
|
|
157 |
Murrell, P. (2005) \emph{R Graphics}. Chapman & Hall/CRC Press.
|
| 27442 |
ripley |
158 |
}
|
|
|
159 |
\seealso{
|
| 76259 |
maechler |
160 |
\code{\link{plot}(\dots, type = "h")}, \code{\link{dotchart}};
|
|
|
161 |
\code{\link{hist}} for bars of a \emph{continuous} variable.
|
|
|
162 |
\code{\link{mosaicplot}()}, more sophisticated to visualize
|
|
|
163 |
\emph{several} categorical variables.
|
| 27442 |
ripley |
164 |
}
|
|
|
165 |
\examples{
|
| 76259 |
maechler |
166 |
# Formula method
|
|
|
167 |
barplot(GNP ~ Year, data = longley)
|
|
|
168 |
barplot(cbind(Employed, Unemployed) ~ Year, data = longley)
|
|
|
169 |
|
|
|
170 |
## 3rd form of formula - 2 categories :
|
|
|
171 |
op <- par(mfrow = 2:1, mgp = c(3,1,0)/2, mar = .1+c(3,3:1))
|
|
|
172 |
summary(d.Titanic <- as.data.frame(Titanic))
|
|
|
173 |
barplot(Freq ~ Class + Survived, data = d.Titanic,
|
|
|
174 |
subset = Age == "Adult" & Sex == "Male",
|
|
|
175 |
main = "barplot(Freq ~ Class + Survived, *)", ylab = "# {passengers}", legend = TRUE)
|
|
|
176 |
# Corresponding table :
|
|
|
177 |
(xt <- xtabs(Freq ~ Survived + Class + Sex, d.Titanic, subset = Age=="Adult"))
|
|
|
178 |
# Alternatively, a mosaic plot :
|
|
|
179 |
mosaicplot(xt[,,"Male"], main = "mosaicplot(Freq ~ Class + Survived, *)", color=TRUE)
|
|
|
180 |
par(op)
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
# Default method
|
| 41502 |
ripley |
184 |
require(grDevices) # for colours
|
| 61153 |
ripley |
185 |
tN <- table(Ni <- stats::rpois(100, lambda = 5))
|
|
|
186 |
r <- barplot(tN, col = rainbow(20))
|
| 27442 |
ripley |
187 |
#- type = "h" plotting *is* 'bar'plot
|
| 61168 |
ripley |
188 |
lines(r, tN, type = "h", col = "red", lwd = 2)
|
| 27442 |
ripley |
189 |
|
| 61153 |
ripley |
190 |
barplot(tN, space = 1.5, axisnames = FALSE,
|
| 27442 |
ripley |
191 |
sub = "barplot(..., space= 1.5, axisnames = FALSE)")
|
|
|
192 |
|
|
|
193 |
barplot(VADeaths, plot = FALSE)
|
|
|
194 |
barplot(VADeaths, plot = FALSE, beside = TRUE)
|
|
|
195 |
|
|
|
196 |
mp <- barplot(VADeaths) # default
|
|
|
197 |
tot <- colMeans(VADeaths)
|
|
|
198 |
text(mp, tot + 3, format(tot), xpd = TRUE, col = "blue")
|
|
|
199 |
barplot(VADeaths, beside = TRUE,
|
|
|
200 |
col = c("lightblue", "mistyrose", "lightcyan",
|
|
|
201 |
"lavender", "cornsilk"),
|
|
|
202 |
legend = rownames(VADeaths), ylim = c(0, 100))
|
|
|
203 |
title(main = "Death Rates in Virginia", font.main = 4)
|
|
|
204 |
|
|
|
205 |
hh <- t(VADeaths)[, 5:1]
|
|
|
206 |
mybarcol <- "gray20"
|
|
|
207 |
mp <- barplot(hh, beside = TRUE,
|
|
|
208 |
col = c("lightblue", "mistyrose",
|
|
|
209 |
"lightcyan", "lavender"),
|
| 61153 |
ripley |
210 |
legend = colnames(VADeaths), ylim = c(0,100),
|
| 27442 |
ripley |
211 |
main = "Death Rates in Virginia", font.main = 4,
|
|
|
212 |
sub = "Faked upper 2*sigma error bars", col.sub = mybarcol,
|
|
|
213 |
cex.names = 1.5)
|
|
|
214 |
segments(mp, hh, mp, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5)
|
| 61168 |
ripley |
215 |
stopifnot(dim(mp) == dim(hh)) # corresponding matrices
|
| 27442 |
ripley |
216 |
mtext(side = 1, at = colMeans(mp), line = -2,
|
|
|
217 |
text = paste("Mean", formatC(colMeans(hh))), col = "red")
|
|
|
218 |
|
|
|
219 |
# Bar shading example
|
|
|
220 |
barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
|
|
|
221 |
legend = rownames(VADeaths))
|
|
|
222 |
title(main = list("Death Rates in Virginia", font = 4))
|
|
|
223 |
|
| 76259 |
maechler |
224 |
# Border color
|
| 27442 |
ripley |
225 |
barplot(VADeaths, border = "dark blue") % lwd = 2 << not passed
|
| 61153 |
ripley |
226 |
%notyet barplot(VADeaths, inside = FALSE, main = "barplot(*, inside = FALSE)")
|
| 34030 |
maechler |
227 |
|
| 76259 |
maechler |
228 |
# Log scales (not much sense here)
|
| 61153 |
ripley |
229 |
barplot(tN, col = heat.colors(12), log = "y")
|
|
|
230 |
barplot(tN, col = gray.colors(20), log = "xy")
|
| 46980 |
ripley |
231 |
|
| 76259 |
maechler |
232 |
# Legend location
|
| 46980 |
ripley |
233 |
barplot(height = cbind(x = c(465, 91) / 465 * 100,
|
|
|
234 |
y = c(840, 200) / 840 * 100,
|
|
|
235 |
z = c(37, 17) / 37 * 100),
|
|
|
236 |
beside = FALSE,
|
|
|
237 |
width = c(465, 840, 37),
|
|
|
238 |
col = c(1, 2),
|
|
|
239 |
legend.text = c("A", "B"),
|
|
|
240 |
args.legend = list(x = "topleft"))
|
| 27442 |
ripley |
241 |
}
|
|
|
242 |
\keyword{hplot}
|