barprop: barprop

View source: R/plot2.R

barpropR Documentation

barprop

Description

A barplot for proportions. This function is intended to show a matrix of proportions as a stacked bar plot but each group will have a common starting height. Therefore, it is recommended that each column sums to 100

Usage

barprop(
  height,
  col = NULL,
  labels = height,
  col.labels = par("col.lab"),
  horiz = TRUE,
  gap = NULL,
  min.prop = Inf,
  axis.pos = NULL,
  xlab = NULL,
  ylab = NULL,
  ...
)

Arguments

height

a matrix (or vector) of proportions

col

colors for each group, i.e., nrow(height) colors

labels

a matrix or vector of labels for each bar plot segment; if FALSE, no labels are drawn

col.labels

a matrix or vector of colors for each label, i.e., length(height) colors

horiz

logical; if TRUE (default), bars are drawn horizontally with the first at the bottom; otherwise, bars are drawn vertically with the first to the left

gap

the space between each group as a single value or vector in units of height, recycled as needed

min.prop

the minimum proportion controlling which labels are drawn in the center of each bar; for any height > min.prop, labels are drawn to the right of the bar (or to the top if horiz = FALSE)

axis.pos

a vector of length 2 giving the position of the x- and y-axes; note that axis.pos[1L] can only be 1 or 3 and axis.pos[2L] can only be 2 or 4

xlab, ylab

the x- and y-axis labels, default is names(dimnames(height))

...

additional graphical parameters passed to par

labels (i.e., the labels for each bar segment) may be customized using col.labels; cex.lab, and/or font.lab

the axis labels (i.e., the labels for each group) may be customized using col.axis; cex.axis, and/or font.axis

the main labels (i.e., xlab and ylab) may be customized using col.main; cex.main, and/or font.main if given

Examples

x <- table(Gear = mtcars$gear, Cylinder = mtcars$cyl)
dimnames(x) <- lapply(dimnames(x), function(y) rawr::num2char(as.numeric(y)))
p <- round(prop.table(x, 2) * 100)
barprop(p)

barprop(x, horiz = FALSE)
## compare
barplot(x)

## on vectors (or single-column matrices)
barprop(p[, 1])
barprop(p[, 1, drop = FALSE])

## options/aesthetics
barprop(p, min.prop = 0, col.labels = ifelse(row(p) == 1, 'white', 'black'))
barprop(p, gap = 10)
barprop(p, min.prop = 0, gap = c(10, 0))
barprop(x, min.prop = 0, labels = sprintf('%s%%', x), cex.lab = 1.5,
  font.axis = 2, cex.axis = 1.5)


raredd/plotr documentation built on Nov. 19, 2023, 4:09 a.m.