barplot {base} | R Documentation |
Bar Plots
Description
Creates a bar plot with vertical or horizontal bars.
Usage
barplot(height, width = 1, space = NULL, names.arg = NULL,
legend.text = NULL, beside = FALSE, horiz = FALSE,
col = heat.colors(NR), border = par("fg"),
main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
xlim = NULL, ylim = NULL,
axes = TRUE, axisnames = TRUE, inside = TRUE, plot = TRUE, ...)
Arguments
height |
either a vector or matrix of values describing the
bars which make up the plot. If |
width |
optional vector of bar widths. |
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. This is only useful when |
beside |
a logical value. If |
horiz |
a logical value. If |
col |
a vector of colors for the bars or bar components. |
border |
the color to be used for the border of the bars. |
main , sub |
overall and sub title 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. |
axes |
logical. If |
axisnames |
logical. If |
plot |
logical. If |
... |
further graphical parameters ( |
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 apply(mp, 2, mean)
for the
midpoints of each group of bars, see example.
See Also
plot(..., type="h")
, dotplot
,
hist
.
Examples
tN <- table(Ni <- rpois(100, lambda=5))
r <- barplot(tN, col='gray')
#- type = "h" plotting *is* `bar'plot
lines(r, tN, type='h', col='red', lwd=2)
barplot(tN, space = 1.5, axisnames=FALSE,
sub = "barplot(...., space= 1.5, axisnames = FALSE)")
data(VADeaths, package = "base")
barplot(VADeaths, plot = FALSE)
barplot(VADeaths, plot = FALSE, beside = TRUE)
mp <- barplot(VADeaths) # default
tot <- apply(VADeaths, 2, sum)
text(mp, tot + 3, format(tot), xpd = NA, col = "blue")
barplot(VADeaths, beside = TRUE,
col = c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk"),
legend = rownames(VADeaths), ylim = c(0, 100))
title(main = "Death Rates in Virginia", font.main = 4)
hh <- t(VADeaths)[, 5:1]
mybarcol <- "gray20"
mp <- barplot(hh, beside = TRUE,
col = c("lightblue", "mistyrose",
"lightcyan", "lavender"),
legend = colnames(VADeaths), ylim= c(0,100),
main = "Death Rates in Virginia", font.main = 4,
sub = "Faked upper 2*sigma error bars", col.sub = mybarcol)
segments(mp, hh, mp, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5)
stopifnot(dim(mp) == dim(hh))# corresponding matrices
mtext(side = 1, at = apply(mp, 2, mean), line = -2,
text = paste("Mean", formatC(apply(hh, 2, mean))), col = "red")