Skip to contents

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

Usage

g6tr_bar_plot(
  df,
  x,
  y,
  display_text = c("all", "highlighted", "none"),
  flipped = FALSE,
  highlight = NULL,
  highlight_condition = FALSE,
  bar_color = g6tr_pal("core")(1),
  bar_color_other = "grey",
  text_color = NULL,
  text_color_other = "black",
  bar_order = c("none", "asc", "desc"),
  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,
  wrap_length = 25,
  label_threshold = NULL,
  conf_int_low = NULL,
  conf_int_high = NULL,
  error_color = "grey"
)

Arguments

df

Data frame that holds x and y columns.

x

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

y

Unquoted Y variable.

display_text

Character. How text should be displayed. One of all(default) (apply text labels to every bar), highlighted (apply text label to only items passed in highlight) or none (don't apply text labels).

flipped

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

highlight

Character or Filter expression. Item(s) from x to be highlighted in the plot. highlight can also be a filter expression. Use a filter expression when you need to highlight elements from x based on other columns in the data.

highlight_condition

Logical. Should highlight be considered as a filter expression.

bar_color, bar_color_other

Character. Valid color (6 digit hex-code or must consist in grDevices::colors()) for the bars and highlights of the plot. By default, when highlight isn't specified, all bars are g6tr core blue (#36c8ef) and when highlight is specified, highlighted bars are displayed in g6tr core blue while the other bars are in grey. If arguments are passed to highlight, bar_color handles the color of the highlighted bars and bar_color_other handles the colors of the bars not highlighted.

text_color, text_color_other

Character. Valid color (6 digit hex-code or must consist in grDevices::colors()) for the text color to be displayed on highlighted/non highlighted bars. When nothing is highlighted and left empty, it will be white if text_position is inside the bars and would take the plot's default text color if outside the bars. If arguments are passed to highlight, text_color handles the color of the highlighted text and text_color_other handles the colors of the unhighlighted texts.

bar_order

Character. How bars should be sorted. One of "none" (default), "asc" or "desc".

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.

wrap_length

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

label_threshold

Values below this threshold will not be displayed.

conf_int_low

Unquoted upper limit variable.

conf_int_high

Unquoted lower limit variable.

error_color

Error bar color.

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) %>% 
  count(Embarked, wt = n) %>% 
  mutate(perc = n / sum(n)) %>%
  mutate(low = perc - 0.05, high = perc + 0.05)

# Display a well sorted horizontal bar chart (in descending order)
df %>%
  g6tr_bar_plot(
   x = Embarked,
   y = perc,
   flipped = TRUE,
   display_text = "all",
   bar_order = "desc",
   text_fontface = 'italic',
   text_position = 'end',
   text_size = 10
   ) +
   theme_g6tr()

 
# Display a non-sorted vertical bar chart
df %>%
  g6tr_bar_plot(
     x = Embarked,
     y = perc,
     flipped = FALSE,
     display_text = "all",
     bar_color = "#50bd90",
     text_fontface = 'bold',
     text_position = 'outside',
     text_size = 10
   ) +
   theme_g6tr()

 
# Highlight "Overall" in plot
df %>%
  g6tr_bar_plot(
    x = Embarked,
    y = perc,
    flipped = TRUE,
    display_text = "all",
    bar_order = "desc",
    text_fontface = 'italic',
    text_position = 'end',
    text_size = 10,
    highlight = "Overall"
    ) +
theme_g6tr()


# Highlight "Overall" and its text 
df %>%
  g6tr_bar_plot(
    x = Embarked,
    y = perc,
    flipped = TRUE,
    bar_order = "desc",
    display_text = "highlighted",
    text_fontface = 'italic',
    text_position = 'end',
    text_size = 10,
    highlight = "Embarked == 'Overall'",
    highlight_condition = TRUE
  ) +
theme_g6tr()


# Highlight more than one category
df %>%
  g6tr_bar_plot(
    x = Embarked,
    y = perc,
    flipped = TRUE,
    bar_order = "desc",
    display_text = "highlighted",
    text_fontface = 'italic',
    text_position = 'end',
    text_size = 10,
    highlight = c("S","C")
  ) +
theme_g6tr()


# Apply individual bar/text colours on highlighted items
## Goal: 
### 1. For "S", we want a `bar color` of black and `text color` of red
### 2. For "C", we want a `bar color` of red and `text color` of black

df %>%
  g6tr_bar_plot(
    x = Embarked,
    y = perc,
    flipped = TRUE,
    display_text = "all",
    bar_order = "desc",
    text_fontface = 'italic',
    text_position = 'end',
    text_size = 10,
    highlight = c("S", "C"),
    bar_color  = c("black", "red"),
    text_color = c("red", "black")
    ) +
  theme_g6tr()

  
# Include error bars

df %>%
  g6tr_bar_plot(
    x = Embarked,
    y = perc,
    flipped = TRUE,
    display_text = "all",
    bar_order = "desc",
    text_fontface = 'italic',
    text_position = 'outside',
    text_size = 10,
    highlight = c("S", "C"),
    conf_int_high = high,
    conf_int_low = low
  ) +
  theme_g6tr()