propfall: propfall

View source: R/plot2.R

propfallR Documentation

propfall

Description

A barplot of proportions.

Usage

propfall(data, group, order, group.order = unique(group), col = NULL, ...)

Arguments

data

a data frame (or object to be coerced)

group

a vector with length nrow(data) defining the group to which each row of data belongs

order

a vector of column names of data defining how numeric values should be sorted

group.order

an optional vector of unique values of group giving the desired order for groups

col

a vector of colors for each of order, recycled as needed

...

additional parameters passed to barplot or graphical parameters passed to par

Value

A list of length two with the result of the call to barplot (bp) giving the x-axis positions for each bar and the order that each observation has been sorted in the barplot.

See Also

waterfall

Examples

dat <- within(mtcars, {
  disp <- disp / 10
  wt <- wt * 10
})

vars <- c('mpg', 'disp', 'wt')
dat[, vars] <- t(apply(dat[, vars], 1L, function(x) x / sum(x)))

dat <- dat[, vars]
dat$group <- colnames(dat)[max.col(dat)]

cols <- c('grey', 'lightpink', 'indianred1')
propfall(dat[, 1:3])
propfall(dat[, 1:3], group = dat$group, col = cols)

## use the return value to add labels or identify observations
bp <- propfall(
  dat[, 1:3], col = cols, group = dat$group,
  order = c('disp', 'wt', 'mpg'),
  group.order = c('mpg', 'wt', 'disp')
)
text(bp$bp, 0, labels = rownames(dat)[bp$o], srt = 90, col = 0, adj = 0)


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