Skip to contents

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

Usage

g6tr_dodged_bar_plot(
  df,
  x,
  y,
  group,
  sig = NULL,
  conf_int = NULL,
  order_by = NULL,
  desc = TRUE,
  text = TRUE,
  legend = TRUE,
  flipped = FALSE,
  color_scale = NULL,
  text_color = NULL,
  text_fontface = c("bold", "plain", "italic", "bold.italic"),
  text_position = c("outside", "beginning", "middle", "end"),
  scale = "percent",
  accuracy = 1,
  text_size = 8,
  bar_width = 0.7,
  error_color = "gray",
  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.

group

Unquoted grouping variable. (optional)

sig

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

conf_int

Unquoted confidence interval variable. If present will add error bars to the plot.

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(). If left empty, will be white if text_position is inside the bars, and will take the color_scale if outside the bars.

text_fontface

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

text_position

Character. One of outside(default), beginning, middle and end.

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.7). Width of the bars ranging from 0-1.

error_color

Character. Valid color spec (6 digit hex-code or must consist in grDevices::colors(). If missing will default to gray.

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)

# Changing almost all possible parameters to showcase the
# features of this function.
df %>%
 g6tr_dodged_bar_plot(
   Embarked,
   percentage,
   Sex,
   sig = prop_pval,
   conf_int = conf_int,
   order_by = 'female',
   desc = TRUE, # will now sort descending by 'female' percentage
   flipped = TRUE,
   text_size = 7,
   error_color = '#dddddd')
#> Warning: No color scale provided. Defaulting to g6tr colors


# One more example
df %>%
 g6tr_dodged_bar_plot(
   Embarked,
   percentage,
   Sex,
   order_by = 'male',
   desc = FALSE, # will now sort ascending by 'male' percentage
   flipped = FALSE,
   text_fontface = 'italic',
   text_position = 'end',
   text_size = 7) +
 theme_g6tr()
#> Warning: No color scale provided. Defaulting to g6tr colors