An enhancement of the standard barplot() function. Creates a bar plot
with vertical or horizontal bars. Can plot confidence intervals for each
bar, a lined grid behind the bars, change plot area color and
logarithmic axes may be used.
either a vector or matrix of values describing the
bars which make up the plot. If height is a vector, the
plot consists of a sequence of rectangular bars with heights
given by the values in the vector. If height is a matrix
and beside is FALSE then each bar of the plot
corresponds to a column of height, with the values in the
column giving the heights of stacked “sub-bars” making up the
bar. If height is a matrix and beside is
TRUE, then the values in each column are juxtaposed
rather than stacked.
width
optional vector of bar widths. Re-cycled to length the
number of bars drawn. Specifying a single value will no visible
effect unless xlim is specified.
space
the amount of space (as a fraction of the average bar
width) left before each bar. May be given as a single number or
one number per bar. If height is a matrix and
beside is TRUE, space may be specified by
two numbers, where the first is the space between bars in the
same group, and the second the space between the groups. If not
given explicitly, it defaults to c(0,1) if height
is a matrix and beside is TRUE, and to 0.2
otherwise.
names.arg
a vector of names to be plotted below each bar or
group of bars. If this argument is omitted, then the names are
taken from the names attribute of height if this
is a vector, or the column names if it is a matrix.
legend.text
a vector of text used to construct a legend for
the plot, or a logical indicating whether a legend should be
included. This is only useful when height is a matrix.
In that case given legend labels should correspond to the rows of
height; if legend.text is true, the row names of
height will be used as labels if they are non-null.
beside
a logical value. If FALSE, the columns of
height are portrayed as stacked bars, and if TRUE
the columns are portrayed as juxtaposed bars.
horiz
a logical value. If FALSE, the bars are drawn
vertically with the first bar to the left. If TRUE, the
bars are drawn horizontally with the first at the bottom.
density
a vector giving the the density of shading lines, in
lines per inch, for the bars or bar components.
The default value of NULL means that no shading lines
are drawn. Non-positive values of density also inhibit the
drawing of shading lines.
angle
the slope of shading lines, given as an angle in
degrees (counter-clockwise), for the bars or bar components.
col
a vector of colors for the bars or bar components.
By default, grey is used if height is a vector, and
heat.colors(nrow(height)) if height is a matrix.
prcol
the color to be used for the plot region.
border
the color to be used for the border of the bars.
main, sub
overall and sub titles for the plot.
xlab
a label for the x axis.
ylab
a label for the y axis.
xlim
limits for the x axis.
ylim
limits for the y axis.
xpd
logical. Should bars be allowed to go outside region?
log
a character string which contains ‘"x"’ if the x axis is
to be logarithmic, ‘"y"’ if the y axis is to be logarithmic and
‘"xy"’ or ‘"yx"’ if both axes are to be logarithmic.
axes
logical. If TRUE, a vertical (or horizontal, if
horiz is true) axis is drawn.
axisnames
logical. If TRUE, and if there are
names.arg (see above), the
other axis is drawn (with lty = 0) and labeled.
cex.axis
expansion factor for numeric axis labels.
cex.names
expansion factor for names.
inside
logical. If TRUE, the lines which divide
adjacent (non-stacked!) bars will be drawn. Only applies when
space = 0 (which it partly is when beside = TRUE).
plot
logical. If FALSE, nothing is plotted.
axis.lty
the graphics parameter lty applied to the axis
and tick marks of the categorical (default horzontal) axis. Note
that by default the axis is suppressed.
offset
a vector indicating how much the bars should be shifted
relative to the x axis.
plot.ci
logical. If TRUE, confidence intervals are plotted
over the bars. Note that if a stacked bar plot is generated, confidence
intervals will not be plotted even if plot.ci = TRUE
ci.l,ci.u
The confidence intervals (ci.l = lower bound, ci.u =
upper bound) to be plotted if plot.ci = TRUE. Values must have
the same dim structure as height.
ci.color
the color for the confidence interval line segments
ci.lty
the line type for the confidence interval line segments
ci.lwd
the line width for the confidence interval line
segments
ci.width
length of lines used for the "t" at the end of confidence
interval line segments, as a multple of width. Defaults to 0.5.
plot.grid
if TRUE a lined grid will be plotted behind the bars
grid.inc
the number of grid increments to be plotted
grid.lty
the line type for the grid
grid.lwd
the line width for the grid
grid.col
the line color for the grid
add
logical, if TRUE add barplot to current plot.
panel.first
An expression to be evaluated after the plot region
coordinates have been set up, but prior to the drawing of the bars
and other plot region contents. This can be useful to add additional
plot region content behind the bars. This will also work if
add = TRUE
panel.last
An expression to be evaluated after the bars have
been drawn, but prior to the addition of confidence intervals, a
legend and the axis annotation
...
further graphical parameters (par) are
passed to plot.window(), title() and
axis.
Details
This is a generic function, it currently only has a default method.
A formula interface may be added eventually.
Value
A numeric vector (or matrix, when beside = TRUE), say
mp, giving the coordinates of all the bar midpoints
drawn, useful for adding to the graph.
If beside is true, use colMeans(mp) for the
midpoints of each group of bars, see example.
Note
Prior to R 1.6.0, barplot behaved as if axis.lty = 1,
unintentionally.
0 (zero) and NA values in height will not be plotted if
using logarithmic scales.
If there are NA values in height and beside = FALSE,
values after the NA will not be plotted in stacked bars.