barplot2 | R Documentation |
Extension of barplot.default
which accepts multi-way arrays
and tables, simplifies grouping and spacing, and adds panel.first
and panel.last
functionality.
barplot2(
height,
width = 1,
space = NULL,
names.arg = NULL,
legend.text = NULL,
beside = FALSE,
horiz = FALSE,
density = NULL,
angle = 45,
col = NULL,
border = par("fg"),
main = NULL,
sub = NULL,
xlab = NULL,
ylab = NULL,
xlim = NULL,
ylim = NULL,
xpd = TRUE,
log = "",
axes = TRUE,
axisnames = TRUE,
cex.axis = par("cex.axis"),
cex.names = par("cex.axis"),
inside = TRUE,
plot = TRUE,
axis.lty = 0,
offset = 0,
add = FALSE,
ann = !add && par("ann"),
args.legend = NULL,
panel.first = NULL,
panel.last = NULL,
...
)
height |
a vector, matrix, table, or |
width |
optional vector of bar widths. Re-cycled to length the
number of bars drawn. Specifying a single value will have no
visible effect unless |
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 |
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 |
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 |
beside |
a logical value. If |
horiz |
a logical value. If |
density |
a vector giving the density of shading lines, in
lines per inch, for the bars or bar components.
The default value of |
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, |
border |
the color to be used for the border of the bars.
Use |
main, sub |
main title and subtitle 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 |
string specifying if axis scales should be logarithmic; see
|
axes |
logical. If |
axisnames |
logical. If |
cex.axis |
expansion factor for numeric axis labels (see
|
cex.names |
expansion factor for axis names (bar labels). |
inside |
logical. If |
plot |
logical. If |
axis.lty |
the graphics parameter |
offset |
a vector indicating how much the bars should be shifted relative to the x axis. |
add |
logical specifying if bars should be added to an already
existing plot; defaults to |
ann |
logical specifying if the default annotation ( |
args.legend |
list of additional arguments to pass to
|
panel.first, panel.last |
expressions to be evaluated before or after plotting takes place |
... |
arguments to be passed to/from other methods. For the
default method these can include further arguments (such as
|
A list giving the locations of each bar, .$at
, and the mid-point of
each group, .$group
.
set.seed(1)
x <- array(runif(4 * 3 * 3), c(4, 3, 3), dimnames = list(1:4, 1:3, 1:3))
barplot2(x, legend.text = TRUE)
barplot2(x[, , 1])
## compare
barplot(x[, , 1])
barplot2(with(mtcars, table(cyl, gear, vs)))
## group labels
barplot2(x, names.arg = list(A = 1:3, B = 4:6, C = 7:9))
## same but manually
bp <- barplot2(x, axisnames = FALSE, ann = FALSE)
mtext(1:9, side = 1L, at = bp$at, line = 1)
mtext(c('A', 'B', 'C'), side = 1L, at = bp$group, line = 3)
## simplified space argument
barplot2(
x, space = c(0.1, 1, 2) / 2, las = 1L, col = 1:4,
legend.text = sprintf('Factor %s', 1:4),
args.legend = list(horiz = TRUE, bty = 'n'),
names.arg = list(A = 1:3, B = 4:6, C = 7:9)
)
## extra features - panel.first, panel.last
barplot2(
1:5,
panel.first = {grid(0, NULL); abline(h = 4, col = 2)},
panel.last = {abline(h = 4.5, col = 3)}
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.