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 andstem_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; otherwisestem_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; otherwisestem_battery()stops with an error naming them (useitem_label = FALSEor give them distinct labels).- palette
Name of a Stem palette. See
stem_palette().- direction
Palette direction. Use
-1to reverse the colours.- labels
If
TRUE, prints a percentage label on each bar.- label_accuracy
Rounding accuracy of labels.
1gives whole numbers,0.1one decimal place.- label_suffix
Suffix appended to labels. Defaults to
"".- label_hide
Proportions below this threshold are left unlabelled. Defaults to
0.05when agroupis supplied (to keep small stacked segments unlabelled) and0otherwise.- 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_coloron light ones. It follows the fill even when the fill scale is replaced later (e.g.+ scale_fill_manual()). Set toFALSEto colour every label withlabel_color.