Dumbbell plot (icons + alpha-gradient segment).
Source:R/g6tr_dumbbell_plot.R
g6tr_dumbbell_plot_2.Rd
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 orderx_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) orstyle_segment_alpha_gradient()(directional alpha).- x_reference
Optional named list returned by
style_reference_line(). Defaults tostyle_reference_line(). Set toNULLto omit the vertical line.- colors
Optional character vector of colors.
If
color_byisNULL:NULLuses an internal default, otherwisecolorsmust be length 1 (base color for everything).If
color_byis provided:NULLuses a default palette, otherwisecolorsmust be a named vector covering all levels ofcolor_byor an unnamed vector of lengthnlevels(color_by).
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)
}