Skip to contents

Generate a stacked bar plot with all of Gradient's best practices applied. Almost instantly ready for publication.

Usage

g6tr_stacked_bar_plot(
  df,
  x,
  y,
  group,
  sig = NULL,
  order_by = NULL,
  desc = TRUE,
  text = TRUE,
  legend = TRUE,
  flipped = FALSE,
  color_scale = NULL,
  text_color = "white",
  text_fontface = c("bold", "plain", "italic", "bold.italic"),
  scale = "percent",
  accuracy = 1,
  text_size = 8,
  bar_width = 0.9,
  wrap_length = 25,
  label_threshold = NULL
)

Arguments

df

Data frame that holds x, y and group.

x

Unquoted X variable. A character or factor variable on the x-axis.

y

Unquoted Y variable. Should be a proportion (e.g. c(0.3, 0.5, 0.2)) and ideally sum to 1, but not necessarily.

group

Unquoted grouping variable.

sig

Unquoted p.value variable. If present, will flag significant (p.value < 0.05) differences with an asterisk (*).

order_by

Character vector. Groups to order the columns by.

desc

Logical. If TRUE (default) will reorder the columns descending.

text

Logical if TRUE (default) will add the Y value as text labels.

legend

Logical. If TRUE (default) will show the legend. Further changes to the legend should be applied with ggplot2::theme()

flipped

Logical. Should the graph be flipped? Default is FALSE.

color_scale

Character vector. Color scale to use (input as hex-codes).

text_color

Character. Valid color spec (6 digit hex-code or must be in grDevices::colors()). Default value is "white".

text_fontface

Character. One of bold(default), "plain", "italic" or "bold.italic".

scale

How should the labels and x axis be formatted. One of "percent", "dollar", "none" or a function e.g scales::comma.

accuracy

Numeric. Passed to the accuracy argument of the selected scale function to set numeric accuracy. Default is 1.

text_size

Numeric. Size of the text passed to ggplot2::geom_text().

bar_width

Numeric (defaults to 0.9). Width of the bars ranging from 0-1.

wrap_length

Numeric vector. passed to scales::wrap_format() as width.

label_threshold

Values below this threshold will not be displayed.

Value

A ggplot object that can be manipulated further.

Examples

library(dplyr)
library(titanic)
library(g6tr.survey)
df <- titanic::titanic_test

df <- df %>%
  srv_add_prop_test(x = Sex, y = Embarked)

df %>%
  g6tr_stacked_bar_plot(
    Embarked,
    percentage,
    Sex,
    sig = prop_pval,
    order_by = "female",
    desc = TRUE
  )
#> Warning: No color scale provided. Defaulting to g6tr colors


df %>%
  g6tr_stacked_bar_plot(
    Embarked,
    percentage,
    Sex,
    order_by = "male",
    desc = FALSE,
    text_fontface = "italic"
  )
#> Warning: No color scale provided. Defaulting to g6tr colors