Skip to contents

Generate a simple pyramid/ reversed pyramid chart with all of Gradient's best practices applied. Almost instantly ready for publication.

Usage

g6tr_pyramid_plot(
  df,
  x,
  y,
  group,
  order_by,
  bar_order = c("none", "asc", "desc"),
  facet_labs = NULL,
  plot_style = c("pyramid", "reversed"),
  display_text = c("all", "highlighted", "none"),
  highlight = NULL,
  highlight_condition = TRUE,
  bar_color = g6tr_pal("core")(1),
  bar_color_other = "grey",
  text_color = "white",
  text_color_other = "black",
  scale = "percent",
  accuracy = 1,
  text_fontface = c("bold", "plain", "italic", "bold.italic"),
  text_position = c("end", "outside", "beginning", "middle"),
  text_size = 8,
  bar_width = 0.7,
  wrap_length = 25,
  label_threshold = NULL
)

Arguments

df

Data frame that holds x and y columns. Data should be in a longer format.

x

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

y

Unquoted Y variable.

group

Unquoted grouping variable.

order_by

Character vector. Groups to order the columns by.

bar_order

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

facet_labs

Character. A vector of tidy names to be displayed on both facets of the plot. It should be a named vector corresponding to the distinct values in group. Values should be in the order bars should be arranged by. i.e facet_labs = c("left_bar" = "Left Bar", "right_bar" = "Right Bar")

plot_style

Character. Bar plot style to be used. One of pyramid(default) or reversed.

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).

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.

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_fontface

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

text_position

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

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.

Value

A ggplot object that can be manipulated further.

Examples

## Creating fake data.
library(dplyr)
library(ggplot2)

library(g6tr.theme)

set.seed(13424)
sample_data <-
  tibble(continent = c("Africa", "Asia", "Europe", "N'America", "S'America")) %>%
  mutate(perc_sea = sample(1:100, nrow(.)) / 100) %>%
  mutate(perc_land = sample(1:100, nrow(.)) / 100) %>%
  tidyr::pivot_longer(cols = starts_with("perc")) %>%
  mutate(across(c("continent", "name"), factor))

facet_labels <- c(perc_land = "Land", perc_sea = "Sea")

sample_data %>%
  g6tr_pyramid_plot(
    x = continent,
    y = value,
    group = name,
    order_by = "perc_land",
    bar_order = "desc",
    facet_labs = facet_labels,
    text_position = "outside",
    text_color = "black",
    highlight = "name == 'perc_sea'",
    bar_color = "red",
    bar_color_other = "blue"
  )

  
facet_labels_b <- c(perc_sea = "Sea", perc_land = "Land")

filter_expr <- 
"(name == 'perc_land' & continent %in% c('Europe', 'Africa')) |
(name == 'perc_sea' & stringr::str_detect(continent, 'America'))"

sample_data %>%
  g6tr_pyramid_plot(
    x = continent,
    y = value,
    group = name,
    order_by = "perc_sea",
    facet_labs = facet_labels_b,
    bar_order = "desc",
    plot_style = "pyramid",
    display_text = "highlighted",
    highlight = filter_expr,
    label_threshold = 0.15
  )