Skip to contents

General plotting function

Usage

stem_plot(
  data,
  item,
  group = NULL,
  weight = NULL,
  collapse_item = NULL,
  collapse_group = NULL,
  geom = ggplot2::geom_col,
  geom_args = list(position = ggplot2::position_dodge(width = 0.95)),
  title = FALSE,
  title_label = TRUE,
  title_wrap = 50,
  format_axis = TRUE,
  caption = FALSE,
  label = TRUE,
  label_args = list(position = ggplot2::position_dodge(width = 0.95), color = "black", ,
    size = ggplot2::rel(5), vjust = -1),
  label_hide = 0.05,
  label_accuracy = 1,
  label_scale = 1,
  label_prefix = "",
  label_suffix = "",
  label_big = " ",
  label_decimal = ",",
  group_size = "none",
  group_wrap = NULL,
  item_size = "none",
  item_wrap = NULL,
  scale_x = ggplot2::scale_x_discrete(),
  scale_y = ggplot2::scale_y_continuous(limits = c(0, NA)),
  scale_fill = scale_fill_stem(palette = "div1"),
  scale_color = scale_color_stem(palette = "div1"),
  facet = FALSE,
  guides = ggplot2::guides(),
  coord_flip = FALSE,
  item_reverse = FALSE,
  group_reverse = FALSE
)

Arguments

data

Dataframe including item (and group) variables

item

Plotted item

group

Optional; plotted grouping variable

weight

Optional; survey weights

collapse_item

Optional, a named list to collapse item categories

collapse_group

Optional, a named list to collapse group categories

geom

Geom that will represent data

geom_args

Arguments for the geom function like size, color, etc.

title

Should the name of item be used as plot title? FALSE by default

title_label

Should title use item label instead of name? Only if item has attribute "label".

title_wrap

Length of title in characters before it gets wrapped. Defaults to 50.

format_axis

If TRUE, adds title for x axis and removes titles for other scales.

caption

If TRUE, adds a caption with sample size.

label

Should geom labels be printed? Yes by default

label_args

Arguments for the labels function like vjust, postion, etc.

label_hide

Hides geom labels with values smaller than this threshold. Defaults to 0.05.

label_accuracy

Accuracy geom labels. 1 means no decimal places, 0.1 one decimal place.

label_scale

Multiplicative scale of geom labels. 100 multiple the values by 100

label_prefix

Prefix for geom labels.

label_suffix

Suffix for geom labels

label_big

Character to split big numbers. Whitespace by default (e.g. 1000 will be printed as "1 000").

label_decimal

Character to separate decimal digits. Comma by default (e.g. 1.2 will be printed as "1,2")

group_size

Should the group size be included in the group legend? Either n (absolute frequency), prop (proportions) or none (nothing).

group_wrap

How many characters in group categories before wrapping to the next line. Defaults to NULL - no wrapping.

item_size

Should the group size be included in the item legend? Either n (absolute frequency), prop (proportions) or none (nothing).

item_wrap

How many characters in item categories before wrapping to the next line. Defaults to NULL - no wrapping.

scale_x

Pass ggplot2::scale_x_discrete() to the plot if needed.

scale_y

Pass ggplot2::scale_y_continuous() to the plot if needed.

scale_fill

Pass scale_fill_* to the plot if needed.

scale_color

Pass scale_color_* to the plot if needed.

facet

Should groups be splitted into facets? No by default.

guides

Pass ggplot2::guides() function to the plot if needed.

coord_flip

If TRUE, switches x and y axes.

item_reverse

Reverse order of item categories

group_reverse

Reverse order of group categories

Value

a ggplot2 graph with custom attribute "stem_plot"

Examples

if (FALSE) { # \dontrun{
stem_plot(data = trust,
item = government,
label = FALSE)

stem_plot(data = trust,
          item = police,
          group = eu_index,
          weight = W,
          geom_args = list(position = "stack"),
          label_args = list(position = ggplot2::position_stack(vjust = 0.5), color = "white"),
          label_scale = 100,
          label_suffix = "",
          item_size = "prop",
          group_size = "n",
          scale_y = ggplot2::scale_y_continuous(labels = scales::percent_format(accuracy = 1,
                                                                                suffix = " %"))) +
theme_stem(legend.position = "bottom")
} # }