Skip to contents

[Experimental] A dumbbell plot for two x values per row (x_1/x_2) with Font-Awesome-like icons at endpoints and (optionally) a directional alpha gradient on the connecting segment.

This is a newer, more flexible version that supports custom endpoint icons, handles icon/label collisions by nudging endpoints when values are close, and groups the many styling controls into style_*() helpers (icons, segments, and text) rather than exposing lots of individual arguments.

Usage

g6tr_dumbbell_plot_2(
  data,
  x_1,
  x_2,
  y,
  color_by = NULL,
  icon_style = style_icon(icon = c("", ""), fontface = c("plain", "bold")),
  label_style = style_text(size = 5),
  segment_style = style_segment_alpha_gradient(alpha_1 = 0, alpha_2 = 1, gamma = 1, n =
    300),
  x_reference = style_reference_line(),
  colors = NULL
)

Arguments

data

Data frame.

x_1, x_2

Unquoted x columns (left/right endpoints).

y

Unquoted y column (categorical).

color_by

Optional unquoted column used for categorical coloring.

icon_style

A 2-row data frame returned by style_icon(), describing the endpoint icons in the order x_1, x_2.

label_style

A named list returned by style_text() controlling percent label text.

segment_style

A named list returned by either style_segment() (constant alpha) or style_segment_alpha_gradient() (directional alpha).

x_reference

Optional named list returned by style_reference_line(). Defaults to style_reference_line(). Set to NULL to omit the vertical line.

colors

Optional character vector of colors.

  • If color_by is NULL: NULL uses an internal default, otherwise colors must be length 1 (base color for everything).

  • If color_by is provided: NULL uses a default palette, otherwise colors must be a named vector covering all levels of color_by or an unnamed vector of length nlevels(color_by).

Value

A ggplot object.

Examples

df <- tibble::tibble(
  level = c("population", "female", "male"),
  private = c(0.20, 0.35, 0.55),
  public  = c(0.40, 0.33, 0.58)
)

p1 <- g6tr_dumbbell_plot_2(
  df,
  x_1 = private,
  x_2 = public,
  y = level,
  icon_style = style_icon(icon = c("\uf0c8", "\uf111"), size = 6, when_close = 0.03),
  color_by = level,
  colors = c(
    population = "#4E79A7",
    female     = "#E15759",
    male       = "#59A14F"
  )
)
#> ! Font Awesome not found on the system; falling back to the default font family "DejaVu Sans". If Font Awesome is installed, inspect `systemfonts::system_fonts()` to find the correct family name.
#> ! Font family "Open Sans" not found; using default family "DejaVu Sans".

p2 <- g6tr_dumbbell_plot_2(
  df,
  x_1 = private,
  x_2 = public,
  y = level,
  color_by = NULL,
  segment_style = style_segment(alpha = 0.25)
)
#> ! Font Awesome not found on the system; falling back to the default font family "DejaVu Sans". If Font Awesome is installed, inspect `systemfonts::system_fonts()` to find the correct family name.
#> ! Font family "Open Sans" not found; using default family "DejaVu Sans".

if (interactive()) {
  print(p1)
  print(p2)
}