barplot(height, …)# S3 method for default
barplot(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, args.legend = NULL, …)
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.xlim
is specified.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
attribute of height
if this
is a vector, or the column names if it is a matrix.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.FALSE
, the columns of
height
are portrayed as stacked bars, and if TRUE
the columns are portrayed as juxtaposed bars.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.NULL
means that no shading lines
are drawn. Non-positive values of density
also inhibit the
drawing of shading lines.height
is a vector, and a
gamma-corrected grey palette if height
is a matrix.border = NA
to omit borders. If there are shading
lines, border = TRUE
means use the same colour for
the border as for the shading lines.plot.default
.TRUE
, a vertical (or horizontal, if
horiz
is true) axis is drawn.TRUE
, and if there are
names.arg
(see above), the
other axis is drawn (with lty = 0
) and labeled.TRUE
, the lines which divide
adjacent (non-stacked!) bars will be drawn. Only applies when
space = 0
(which it partly is when beside = TRUE
).FALSE
, nothing is plotted.lty
applied to the axis
and tick marks of the categorical (default horizontal) axis. Note
that by default the axis is suppressed.FALSE
.legend()
; names of the list are used as argument
names. Only used if legend.text
is supplied.axes
, asp
and main
) and
graphical parameters (see par
) which are passed to
plot.window()
, title()
and
axis
.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.plot(…, type = "h")
, dotchart
,
hist
.require(grDevices) # for colours tN <- table(Ni <- stats::rpois(100, lambda = 5)) r <- barplot(tN, col = rainbow(20)) #- 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)") barplot(VADeaths, plot = FALSE) barplot(VADeaths, plot = FALSE, beside = TRUE) mp <- barplot(VADeaths) # default tot <- colMeans(VADeaths) text(mp, tot + 3, format(tot), xpd = TRUE, 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, cex.names = 1.5) 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 = colMeans(mp), line = -2, text = paste("Mean", formatC(colMeans(hh))), col = "red") # Bar shading example barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black", legend = rownames(VADeaths)) title(main = list("Death Rates in Virginia", font = 4)) # border : barplot(VADeaths, border = "dark blue") <!-- % lwd = 2 << not passed --> <!-- %notyet barplot(VADeaths, inside = FALSE, main = "barplot(*, inside = FALSE)") --> # log scales (not much sense here): barplot(tN, col = heat.colors(12), log = "y") barplot(tN, col = gray.colors(20), log = "xy") # args.legend barplot(height = cbind(x = c(465, 91) / 465 * 100, y = c(840, 200) / 840 * 100, z = c(37, 17) / 37 * 100), beside = FALSE, width = c(465, 840, 37), col = c(1, 2), legend.text = c("A", "B"), args.legend = list(x = "topleft"))
Run the code above in your browser using DataCamp Workspace