Overview
lx_strip_plot() is handy for quick exploration and model
checks, but the main production plot in this package is the dumbbell
plot built with lx_dumbbell_plot(). This vignette shows how
to use it with predictor-based cohorts and custom cohort objects, and
how to control icons and segment gradients.
Quick exploration: strip plots
Use strip plots for exploratory checks on the model outputs:
model_pets <- example_lx_model
lx_strip_plot(model_pets)
#> ℹ No weights provided. Using equal weights of 1.
Production plots: predictor-based cohorts
In most cases, you will want subgroup summaries for variables that were used as model predictors. You can request these directly by passing predictor names as a character vector:
cohorts_plot <- c("d_gender", "d_age")
lx_dumbbell_plot(model_pets, cohorts = cohorts_plot, color_by = column, name_by = level)
#> ! 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 you also want the overall population in the same output, include
"population" in the vector. Predictor-based cohorts use
weights stored in the parsed model data, so if survey weights matter for
these quick summaries, make sure weight_col was set in
lx_parse_data().
color_by and name_by can reference columns
in the generated cohort set. This makes it easy to color by one grouping
while labeling by another (or the same).
Custom cohort objects
You can also pass any explicitly defined cohort or
cohort_set:
cohort_set_custom <- example_srv_lx |>
create_cohort_set(
id_col = response_id,
grp_cols = c(d_gender),
weight_col = weight
)
lx_dumbbell_plot(model_pets, cohorts = cohort_set_custom, color_by = level, name_by = level)
#> ! 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".
Icons, collision handling, and segment gradients
The dumbbell plot is styled via style_*() helpers.
Icons and collisions
Use style_icon() to set the two endpoint icons (for
x_1 and x_2), their families, font faces,
sizes, and the when_close threshold used to decide when to
vertically separate icons/labels to avoid overlaps.
By default, style_icon() tries to find an installed Font
Awesome family via systemfonts::system_fonts() and use it.
The exact family name can differ across systems, so if you need
consistent rendering you can set it explicitly with the
family parameter in style_icon(). You can also
supply a vector of two font families to family to use
different fonts for x_1 and x_2. This is
useful when an icon set ships as a collection of families (e.g. Font
Awesome “Solid” vs other variants), and the icons you want live in
different families. The fontface parameter works the same
way and defaults to "plain".
icon_style <- style_icon(
icon = c("\uf0e0", "\uf075"),
size = c(6, 6),
when_close = 0.05
)
#> ! 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.
lx_dumbbell_plot(model_pets, cohorts = cohorts_plot, icon_style = icon_style, name_by = level)
#> ! Font family "Open Sans" not found; using default family "DejaVu Sans".![]()
when_close is in data units (the same scale as your x
values). If x_1 and x_2 differ by less than
this threshold, the points and labels are nudged to avoid
collisions.
Segment gradients
For directional emphasis, use
style_segment_alpha_gradient(). The alpha at
x_1 is alpha_1, and the alpha at
x_2 is alpha_2. gamma controls
how quickly the transition happens along the segment.
segment_style <- style_segment_alpha_gradient(
alpha_1 = 0, # more transparent near private
alpha_2 = 1, # less near public
gamma = 1.3 # >1 concentrates the change towards public
)
lx_dumbbell_plot(
model_pets, cohorts = cohorts_plot,
segment_style = segment_style,
name_by = level
)
#> ! 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".
Further customization
lx_dumbbell_plot() returns a ggplot object, so you can
add facets or tweak themes as usual:
lx_dumbbell_plot(model_pets, cohorts = cohorts_plot, color_by = column, name_by = level) +
ggplot2::facet_wrap(~column) +
ggplot2::theme(
legend.position = "bottom",
axis.text.y = ggplot2::element_text(size = 10)
)
#> ! 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".