Skip to contents

Plots a set of categorical variables that share the same response categories (e.g. a battery of Likert items) as a single chart, drawing one stacked horizontal bar per item. Internally the items are reshaped to long format and handed to the same machinery as stem_barplot(), with the item taking the role of the group.

Usage

stem_battery(
  data,
  items,
  weight = NULL,
  order_by = NULL,
  item_label = TRUE,
  palette = "div1",
  direction = 1,
  labels = TRUE,
  label_accuracy = 1,
  label_suffix = "",
  label_hide = 0.05,
  label_color = "black",
  label_bicolor = TRUE
)

Arguments

data

Data frame holding the item (and group) variables.

items

Items to plot. Supports tidyselect helpers (e.g. dplyr::starts_with()). The response categories (and their order) are taken from the first item; if another item has answers outside them, stem_battery() stops with an error listing those answers. If the items have different numbers of valid (non-missing) answers, their bars rest on different bases and stem_battery() warns, listing them.

weight

Optional survey weights.

order_by

Optional vector of response categories used to order the items, e.g. c("Definitely Agree", "Rather Agree") orders items by their combined share of those two categories. Every value must match a category of the items exactly; otherwise stem_battery() stops with an error listing the values not found and the categories.

item_label

If TRUE (default), item labels (the "label" attribute) are used instead of variable names. Falls back to names, with a warning, if any item lacks a label. Items must have distinct labels; otherwise stem_battery() stops with an error naming them (use item_label = FALSE or give them distinct labels).

palette

Name of a Stem palette. See stem_palette().

direction

Palette direction. Use -1 to reverse the colours.

labels

If TRUE, prints a percentage label on each bar.

label_accuracy

Rounding accuracy of labels. 1 gives whole numbers, 0.1 one decimal place.

label_suffix

Suffix appended to labels. Defaults to "".

label_hide

Proportions below this threshold are left unlabelled. Defaults to 0.05 when a group is supplied (to keep small stacked segments unlabelled) and 0 otherwise.

label_color

Colour of the segment labels in grouped (stacked) plots. Defaults to "black".

label_bicolor

If TRUE (default), in grouped (stacked) plots each label's colour follows the final fill of its segment: white on dark fills, label_color on light ones. It follows the fill even when the fill scale is replaced later (e.g. + scale_fill_manual()). Set to FALSE to colour every label with label_color.

Value

A ggplot2 object.

Examples

if (FALSE) { # \dontrun{
stem_battery(trust, items = c(police, eu, government, army))
stem_battery(trust,
             items = c(police, eu, government, army),
             weight = W,
             order_by = c("Definitely Agree", "Rather Agree"))
} # }