Skip to contents

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

Usage

g6tr_pyramid_plot_2(
  df,
  x,
  y1,
  y2,
  order_by = NULL,
  bar_order = c("none", "asc", "desc"),
  display_text = c("all", "highlighted", "none"),
  scale = "percent",
  accuracy = 1,
  plot_style = c("pyramid", "reversed"),
  plot_labels = NULL,
  bar_width = 0.7,
  wrap_length = 25,
  text_size = 8,
  left_margin = 40,
  text_color = NULL,
  text_color_other = "black",
  text_fontface = c("bold", "plain", "italic", "bold.italic"),
  text_position = c("outside", "beginning", "middle", "end"),
  label_threshold = NULL,
  highlight = NULL,
  bar_color = g6tr_pal("core")(1),
  bar_color_other = "grey",
  x_label = NULL,
  title = NULL
)

Arguments

df

Data frame that holds x and y columns.

x

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

y1

Unquoted Y variable. To be displayed on the left side of the pyramid chart.

y2

Unquoted Y variable. To be displayed on the right side of the pyramid chart.

order_by

Unquoted variable. If ordering is needed, what variable should the plot be ordered by?

bar_order

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

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

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.

plot_style

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

plot_labels

Character. Titles to be given to each of the bar plots. It is in the order (y1, y2).

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.

text_size

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

left_margin

Integer. Spacing (in "pt") between the left plot and y axis label names. Default is 40.

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. To make highlighting unique for each plot, pass a list of highlighting parameters with names (y1 and y2). See examples for use case.

text_fontface

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

text_position

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

label_threshold

Values below this threshold will not be displayed.

highlight

Character. Item(s) from x to be highlighted in the plot. To make highlighting unique for each plot, pass a list of highlighting parameters with names (y1 and y2). See examples for use case.

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. To make highlighting unique for each plot, pass a list of highlighting parameters with names (y1 and y2). See examples for use case.

x_label

Character. X axis title.

title

Character. Title of the plot.

Value

A patchwork object.

Examples


if (FALSE) { # \dontrun{

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

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)

## The function uses the g6tr_bar_plot function as base.
## So, every feature of the function is imported in here.

## A basic pyramid chart.
sample_data %>%
  g6tr_pyramid_plot_2(
    x = continent,
    y1 = perc_sea,
    y2 = perc_land,
    order_by = perc_sea,
    bar_order = "asc"
  )

## Changing `plot_style` to other produces a reversed pyramid chart.
sample_data %>%
  g6tr_pyramid_plot_2(
    x = continent,
    y1 = perc_sea,
    y2 = perc_land,
    order_by = perc_sea,
    bar_order = "asc",
    plot_style = "reversed",
    text_position = "end",
    bar_color = g6tr_pal("core")(5)[2],
    label_threshold = 0.15
  )

## Exploring its highlighting features and adding labels.
sample_data %>%
  g6tr_pyramid_plot_2(
    x = continent,
    y1 = perc_sea,
    y2 = perc_land,
    order_by = perc_sea,
    bar_order = "desc",
    plot_style = "pyramid",
    plot_labels = c("Sea", "Land"),
    display_text = "highlighted",
    title = "Difference in land and sea coverage",
    highlight = "Europe",
    bar_color = g6tr_pal("core")(5)[2],
    text_position = "end",
    label_threshold = 0.15
  )

## Use different colors for left and right sides
sample_data %>%
  g6tr_pyramid_plot_2(
    x = continent,
    y1 = perc_sea,
    y2 = perc_land,
    order_by = perc_sea,
    bar_order = "desc",
    plot_style = "pyramid",
    plot_labels = c("Sea", "Land"),
    display_text = "highlighted",
    title = "Difference in land and sea coverage",
    bar_color = list(y1 = g6tr_pal("core")(5)[2],
                     y2 = g6tr_pal("core")(5)[3]),
    text_position = "end",
    label_threshold = 0.15
  )

## Apply highlights to individual plots
sample_plot <-
  sample_data %>%
  g6tr_pyramid_plot_2(
    x = continent,
    y1 = perc_sea,
    y2 = perc_land,
    order_by = perc_sea,
    bar_order = "desc",
    plot_style = "pyramid",
    plot_labels = c("Sea", "Land"),
    display_text = "highlighted",
    title = "Difference in land and sea coverage",
    highlight = list(y1 = sample_data$continent[1:3],
                     y2 = sample_data$continent[3:5]),
    bar_color = list(y1 = g6tr_pal("core")(5)[2],
                     y2 = g6tr_pal("core")(5)[3]),
    text_position = "end",
    label_threshold = 0.15
  )

## View plot
sample_plot

## Make extra edits
## note the & instead of +, that's because sample_plot is a patchwork object
sample_plot &
  theme(panel.grid.major.x = element_line(color = "gray90"))
  
} # }