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