| Line 1... |
Line 1... |
| 1 |
% File src/library/graphics/man/barplot.Rd
|
1 |
% File src/library/graphics/man/barplot.Rd
|
| 2 |
% Part of the R package, https://www.R-project.org
|
2 |
% Part of the R package, https://www.R-project.org
|
| 3 |
% Copyright 1995-2007 R Core Team
|
3 |
% Copyright 1995-2019 R Core Team
|
| 4 |
% Distributed under GPL 2 or later
|
4 |
% Distributed under GPL 2 or later
|
| 5 |
|
5 |
|
| 6 |
\name{barplot}
|
6 |
\name{barplot}
|
| 7 |
\alias{barplot}
|
7 |
\alias{barplot}
|
| 8 |
\alias{barplot.default}
|
8 |
\alias{barplot.default}
|
| - |
|
9 |
\alias{barplot.formula}
|
| 9 |
\title{Bar Plots}
|
10 |
\title{Bar Plots}
|
| 10 |
\description{
|
11 |
\description{
|
| 11 |
Creates a bar plot with vertical or horizontal bars.
|
12 |
Creates a bar plot with vertical or horizontal bars.
|
| 12 |
}
|
13 |
}
|
| 13 |
\usage{
|
14 |
\usage{
|
| Line 20... |
Line 21... |
| 20 |
main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
|
21 |
main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
|
| 21 |
xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
|
22 |
xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
|
| 22 |
axes = TRUE, axisnames = TRUE,
|
23 |
axes = TRUE, axisnames = TRUE,
|
| 23 |
cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
|
24 |
cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
|
| 24 |
inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
|
25 |
inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
|
| - |
|
26 |
add = FALSE, ann = !add && par("ann"), args.legend = NULL, \dots)
|
| - |
|
27 |
|
| - |
|
28 |
\method{barplot}{formula}(formula, data, subset, na.action,
|
| 25 |
add = FALSE, args.legend = NULL, \dots)
|
29 |
horiz = FALSE, xlab = NULL, ylab = NULL, \dots)
|
| 26 |
}
|
30 |
}
|
| 27 |
\arguments{
|
31 |
\arguments{
|
| 28 |
\item{height}{either a vector or matrix of values describing the
|
32 |
\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
|
33 |
bars which make up the plot. If \code{height} is a vector, the
|
| 30 |
plot consists of a sequence of rectangular bars with heights
|
34 |
plot consists of a sequence of rectangular bars with heights
|
| Line 103... |
Line 107... |
| 103 |
that by default the axis is suppressed.}
|
107 |
that by default the axis is suppressed.}
|
| 104 |
\item{offset}{a vector indicating how much the bars should be shifted
|
108 |
\item{offset}{a vector indicating how much the bars should be shifted
|
| 105 |
relative to the x axis.}
|
109 |
relative to the x axis.}
|
| 106 |
\item{add}{logical specifying if bars should be added to an already
|
110 |
\item{add}{logical specifying if bars should be added to an already
|
| 107 |
existing plot; defaults to \code{FALSE}.}
|
111 |
existing plot; defaults to \code{FALSE}.}
|
| - |
|
112 |
\item{ann}{logical specifying if the default annotation (\code{main},
|
| - |
|
113 |
\code{sub}, \code{xlab}, \code{ylab}) should appear on the plot, see
|
| - |
|
114 |
\code{\link{title}}.}
|
| 108 |
\item{args.legend}{list of additional arguments to pass to
|
115 |
\item{args.legend}{list of additional arguments to pass to
|
| 109 |
\code{\link{legend}()}; names of the list are used as argument
|
116 |
\code{\link{legend}()}; names of the list are used as argument
|
| 110 |
names. Only used if \code{legend.text} is supplied.}
|
117 |
names. Only used if \code{legend.text} is supplied.}
|
| - |
|
118 |
\item{formula}{a formula where the \code{y} variables are numeric data
|
| - |
|
119 |
to plot against the categorical \code{x} variables. The formula
|
| - |
|
120 |
can have one of three forms: \preformatted{
|
| - |
|
121 |
y ~ x
|
| - |
|
122 |
y ~ x1 + x2
|
| - |
|
123 |
cbind(y1, y2) ~ x},
|
| - |
|
124 |
see the examples.}
|
| - |
|
125 |
\item{data}{a data frame (or list) from which the variables in formula
|
| - |
|
126 |
should be taken.}
|
| - |
|
127 |
\item{subset}{an optional vector specifying a subset of observations
|
| - |
|
128 |
to be used.}
|
| - |
|
129 |
\item{na.action}{a function which indicates what should happen when
|
| - |
|
130 |
the data contain \code{\link{NA}} values. The default is to ignore missing
|
| - |
|
131 |
values in the given variables.}
|
| 111 |
\item{\dots}{arguments to be passed to/from other methods. For the
|
132 |
\item{\dots}{arguments to be passed to/from other methods. For the
|
| 112 |
default method these can include further arguments (such as
|
133 |
default method these can include further arguments (such as
|
| 113 |
\code{axes}, \code{asp} and \code{main}) and
|
134 |
\code{axes}, \code{asp} and \code{main}) and
|
| 114 |
\link{graphical parameters} (see \code{\link{par}}) which are passed to
|
135 |
\link{graphical parameters} (see \code{\link{par}}) which are passed to
|
| 115 |
\code{\link{plot.window}()}, \code{\link{title}()} and
|
136 |
\code{\link{plot.window}()}, \code{\link{title}()} and
|
| 116 |
\code{\link{axis}}.}
|
137 |
\code{\link{axis}}.}
|
| 117 |
}
|
138 |
}
|
| 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{
|
139 |
\value{
|
| 123 |
A numeric vector (or matrix, when \code{beside = TRUE}), say
|
140 |
A numeric vector (or matrix, when \code{beside = TRUE}), say
|
| 124 |
\code{mp}, giving the coordinates of \emph{all} the bar midpoints
|
141 |
\code{mp}, giving the coordinates of \emph{all} the bar midpoints
|
| 125 |
drawn, useful for adding to the graph.
|
142 |
drawn, useful for adding to the graph.
|
| 126 |
|
143 |
|
| 127 |
If \code{beside} is true, use \code{colMeans(mp)} for the
|
144 |
If \code{beside} is true, use \code{colMeans(mp)} for the
|
| 128 |
midpoints of each \emph{group} of bars, see example.
|
145 |
midpoints of each \emph{group} of bars, see example.
|
| 129 |
}
|
146 |
}
|
| - |
|
147 |
\author{R Core, with a contribution by Arni Magnusson.}
|
| 130 |
\references{
|
148 |
\references{
|
| 131 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
149 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
| 132 |
\emph{The New S Language}.
|
150 |
\emph{The New S Language}.
|
| 133 |
Wadsworth & Brooks/Cole.
|
151 |
Wadsworth & Brooks/Cole.
|
| 134 |
|
152 |
|
| 135 |
Murrell, P. (2005) \emph{R Graphics}. Chapman & Hall/CRC Press.
|
153 |
Murrell, P. (2005) \emph{R Graphics}. Chapman & Hall/CRC Press.
|
| 136 |
}
|
154 |
}
|
| 137 |
\seealso{
|
155 |
\seealso{
|
| 138 |
\code{\link{plot}(\dots, type = "h")}, \code{\link{dotchart}},
|
156 |
\code{\link{plot}(\dots, type = "h")}, \code{\link{dotchart}};
|
| 139 |
\code{\link{hist}}.
|
157 |
\code{\link{hist}} for bars of a \emph{continuous} variable.
|
| - |
|
158 |
\code{\link{mosaicplot}()}, more sophisticated to visualize
|
| - |
|
159 |
\emph{several} categorical variables.
|
| 140 |
}
|
160 |
}
|
| 141 |
\examples{
|
161 |
\examples{
|
| - |
|
162 |
# Formula method
|
| - |
|
163 |
barplot(GNP ~ Year, data = longley)
|
| - |
|
164 |
barplot(cbind(Employed, Unemployed) ~ Year, data = longley)
|
| - |
|
165 |
|
| - |
|
166 |
## 3rd form of formula - 2 categories :
|
| - |
|
167 |
op <- par(mfrow = 2:1, mgp = c(3,1,0)/2, mar = .1+c(3,3:1))
|
| - |
|
168 |
summary(d.Titanic <- as.data.frame(Titanic))
|
| - |
|
169 |
barplot(Freq ~ Class + Survived, data = d.Titanic,
|
| - |
|
170 |
subset = Age == "Adult" & Sex == "Male",
|
| - |
|
171 |
main = "barplot(Freq ~ Class + Survived, *)", ylab = "# {passengers}", legend = TRUE)
|
| - |
|
172 |
# Corresponding table :
|
| - |
|
173 |
(xt <- xtabs(Freq ~ Survived + Class + Sex, d.Titanic, subset = Age=="Adult"))
|
| - |
|
174 |
# Alternatively, a mosaic plot :
|
| - |
|
175 |
mosaicplot(xt[,,"Male"], main = "mosaicplot(Freq ~ Class + Survived, *)", color=TRUE)
|
| - |
|
176 |
par(op)
|
| - |
|
177 |
|
| - |
|
178 |
|
| - |
|
179 |
# Default method
|
| 142 |
require(grDevices) # for colours
|
180 |
require(grDevices) # for colours
|
| 143 |
tN <- table(Ni <- stats::rpois(100, lambda = 5))
|
181 |
tN <- table(Ni <- stats::rpois(100, lambda = 5))
|
| 144 |
r <- barplot(tN, col = rainbow(20))
|
182 |
r <- barplot(tN, col = rainbow(20))
|
| 145 |
#- type = "h" plotting *is* 'bar'plot
|
183 |
#- type = "h" plotting *is* 'bar'plot
|
| 146 |
lines(r, tN, type = "h", col = "red", lwd = 2)
|
184 |
lines(r, tN, type = "h", col = "red", lwd = 2)
|
| Line 177... |
Line 215... |
| 177 |
# Bar shading example
|
215 |
# Bar shading example
|
| 178 |
barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
|
216 |
barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
|
| 179 |
legend = rownames(VADeaths))
|
217 |
legend = rownames(VADeaths))
|
| 180 |
title(main = list("Death Rates in Virginia", font = 4))
|
218 |
title(main = list("Death Rates in Virginia", font = 4))
|
| 181 |
|
219 |
|
| 182 |
# border :
|
220 |
# Border color
|
| 183 |
barplot(VADeaths, border = "dark blue") % lwd = 2 << not passed
|
221 |
barplot(VADeaths, border = "dark blue") % lwd = 2 << not passed
|
| 184 |
%notyet barplot(VADeaths, inside = FALSE, main = "barplot(*, inside = FALSE)")
|
222 |
%notyet barplot(VADeaths, inside = FALSE, main = "barplot(*, inside = FALSE)")
|
| 185 |
|
223 |
|
| 186 |
# log scales (not much sense here):
|
224 |
# Log scales (not much sense here)
|
| 187 |
barplot(tN, col = heat.colors(12), log = "y")
|
225 |
barplot(tN, col = heat.colors(12), log = "y")
|
| 188 |
barplot(tN, col = gray.colors(20), log = "xy")
|
226 |
barplot(tN, col = gray.colors(20), log = "xy")
|
| 189 |
|
227 |
|
| 190 |
# args.legend
|
228 |
# Legend location
|
| 191 |
barplot(height = cbind(x = c(465, 91) / 465 * 100,
|
229 |
barplot(height = cbind(x = c(465, 91) / 465 * 100,
|
| 192 |
y = c(840, 200) / 840 * 100,
|
230 |
y = c(840, 200) / 840 * 100,
|
| 193 |
z = c(37, 17) / 37 * 100),
|
231 |
z = c(37, 17) / 37 * 100),
|
| 194 |
beside = FALSE,
|
232 |
beside = FALSE,
|
| 195 |
width = c(465, 840, 37),
|
233 |
width = c(465, 840, 37),
|