Barplot type

Description

Type function for producing barplots. For formulas of type ~ x (without left-hand side) the barplot visualizes the counts (absolute frequencies) of the levels of x. For formulas of type y ~ x the value of y within each level of x is visualized, if necessary aggregated using some function (default: mean).

Usage

type_barplot(
  width = 5/6,
  beside = FALSE,
  center = FALSE,
  FUN = NULL,
  xlevels = NULL,
  xaxlabels = NULL,
  drop.zeros = FALSE
)

Arguments

width numeric, optional vector of bar widths. (The distance between the midpoints of the bars is always 1.)
beside logical. In case of a by grouping variable, should bars be juxtaposed? Default is to use stacked bars instead.
center logical or numeric. In case of stacked barplots (beside = FALSE) should the bars be centered (or all start at zero, default)? If set to TRUE the center is at the mid-point of the middle category (in case of uneven number of categories) or between the two middle categories (in case of an even number). Additionally it is possible to set center = 2 or center = 2.5 to indicate that centering should be after the second category or the mid-way in the third category, respectively.
FUN a function to compute the summary statistic for y within each group of x in case of using a two-sided formula y ~ x (default: mean).
xlevels a character or numeric vector specifying in which order the levels of the x variable should be plotted.
xaxlabels a character vector with the axis labels for the x variable, defaulting to the levels of x.
drop.zeros logical. Should bars with zero height be dropped? If set to FALSE (default) a zero height bar is still drawn for which the border lines will still be visible.

Examples

library("tinyplot")

# Basic examples of frequency tables (without y variable)
tinyplot(~ cyl, data = mtcars, type = "barplot")

tinyplot(~ cyl | vs, data = mtcars, type = "barplot")

tinyplot(~ cyl | vs, data = mtcars, type = "barplot", beside = TRUE)

tinyplot(~ cyl | vs, data = mtcars, type = "barplot", beside = TRUE, fill = 0.2)

# Note: Above we used automatic argument passing for `beside`. But this
# wouldn't work for `width`, since it would conflict with the top-level
# `tinyplot(..., width = <width>)` argument. It's safer to pass these args
# through the `type_barplot()` functional equivalent.
tinyplot(~ cyl | vs, data = mtcars, fill = 0.2,
  type = type_barplot(beside = TRUE, drop.zeros = TRUE, width = 0.65))

tinytheme("clean2")

# Example for numeric y aggregated by x (default: FUN = mean) + facets
tinyplot(extra ~ ID | group, facet = "by", data = sleep,
  type = "barplot", fill = 0.6)

# Fancy frequency table:
tinyplot(Freq ~ Sex | Survived, facet = ~ Class, data = as.data.frame(Titanic),
  type = "barplot", facet.args = list(nrow = 1), flip = TRUE, fill = 0.6)

# Centered barplot for conditional proportions of hair color (black/brown vs.
# red/blond) given eye color and sex
tinytheme("clean2", palette.qualitative = c("black", "sienna", "indianred", "goldenrod"))
hec = as.data.frame(proportions(HairEyeColor, 2:3))
tinyplot(Freq ~ Eye | Hair, facet = ~ Sex, data = hec, type = "barplot",
  center = TRUE, flip = TRUE, facet.args = list(ncol = 1), yaxl = "percent")

tinytheme()