Visualizes MaxDiff output scores using either a bar or lollipop chart. This function supports raw PWU values, percent importance, mean100 rescaling, and anchored MaxDiff scores.
Usage
md_plot(
data,
stat,
plot_type = NULL,
conf_int = FALSE,
center_at = NULL,
scale = NULL,
text_position = NULL,
flipped = TRUE,
label_most = "Most important",
label_least = "Least important",
color_most = "#50bd90",
color_least = "#de425b",
point_size = 22,
text_size = 6,
wrap_length = 50,
lims = c(-100, 150),
expand = c(0.05, 0.1),
include_label = TRUE,
hide_segment_line = FALSE,
text_color = "#FFFFFF",
text_color_other = "#FFFFFF",
...
)Arguments
- data
MaxDiff summary results. Result from
md_stats().- stat
The name of the column in
datato unpack and plot. Must be a nested column (e.g.,"score_mean100").- plot_type
Optional string indicating plot type:
"bar"or"lollipop". IfNULL, inferred fromstat.- conf_int
Logical; Include confidence intervals?
- center_at
Optional numeric value to center the plot on. Defaults to 1 for
"score_mean100"in lollipop plots, and 0 otherwise.- scale
How should the labels and x axis be formatted. One of
"percent","dollar","none"or a function e.gscales::comma.- text_position
Position of text:
bar: "outside", "beginning", "middle", "end".
lollipop: "outside", "inside".
- flipped
Logical. Should the graph be flipped? Default is
FALSE.- label_most, label_least
Strings for the "Most important" and "Least important" annotations.
- color_most, color_least
Colors for positive and negative lollipop bars. When used for bar plot and without highlighting, use
color_mostfor overall bar plot color.- point_size
Size of lollipop dots.
- text_size
Numeric. Size of the text passed to
ggplot2::geom_text().- wrap_length
Numeric vector. passed to
scales::wrap_format()as width.- lims
A vector of length 2 specifying the y-axis range for plotting. Default is
c(-100, 150).- expand
Numeric. A vector of length 2 specifying axis expansion passed to
ggplot2::expansion(). Default isc(0.05, 0.1).- include_label
Logical; if
TRUE, adds annotation text/arrows for labels.- hide_segment_line
Should the segment line be hidden? Useful when displaying error bars.
- 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 iftext_positionis inside the bars and would take the plot's default text color ifoutsidethe bars. If arguments are passed tohighlight,text_colorhandles the color of the highlighted text andtext_color_otherhandles the colors of the unhighlighted texts.- ...
Additional arguments passed to
g6tr.theme::g6tr_bar_plot()org6tr.theme::g6tr_diverging_plot()(e.g.,highlight,highlight_condition, etc.) when using bar plot.
Details
The plot type is inferred from the stat column name if not explicitly
provided:
"pwu"or"prob"→ bar plot."score_mean100"→ lollipop plot."score_anchor_*"→ bar plot.
Confidence intervals and mean-centering are also supported.
Examples
mod_mlogit <- md_model(example_md_parsed_data, type = "mlogit")
stats_mlogit <- md_stats(mod_mlogit)
## Plot pwu values
stats_mlogit |>
md_plot(
stat = "pwu",
conf_int = TRUE,
text_color = "black",
text_color_other = "black"
)
## Plot importance scores
stats_mlogit |>
md_plot(
stat = "prob",
text_color = "black",
text_color_other = "black"
)
## Plot mean100 scores. By default, it produces a lollipop chart
md_plot(stats_mlogit, stat = "score_mean100")
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> ℹ The deprecated feature was likely used in the g6tr.theme package.
#> Please report the issue to the authors.
#> Scale for y is already present.
#> Adding another scale for y, which will replace the existing scale.
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
## Plot mean100 with confidence intervals
md_plot(
stats_mlogit,
stat = "score_mean100",
conf_int = TRUE,
hide_segment_line = TRUE
)
#> Scale for y is already present.
#> Adding another scale for y, which will replace the existing scale.
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
## Create bar plot
md_plot(
stats_mlogit,
stat = "score_mean100",
plot_type = "bar",
scale = \(x) round(x * 100),
text_position = "end",
highlight = "mean > 1",
highlight_condition = TRUE,
text_color_other = "white",
conf_int = TRUE
)
mod_anchored <- md_model(
example_md_parsed_data,
type = "mlogit",
anchor_data = example_md_anchor_answers
)
stats_anchored <- md_stats(mod_anchored)
stats_anchored |>
md_plot(
stat = "score_anchor_5",
plot_type = "bar",
conf_int = TRUE,
scale = \(x) round(x * 100),
highlight = "mean > 1",
highlight_condition = TRUE,
text_color = "black",
text_color_other = "black"
)
## Create plot for a cohort set.
cohort_set <- example_md_data |>
haven::as_factor() |>
g6tr.cohorts::create_cohort_set(id_col = "response_id", grp_cols = "s_gender")
#> ℹ No weights provided. Using equal weights of 1.
mod_mlogit_gender <- md_model(example_md_parsed_data, type = "mlogit", cohorts = cohort_set)
mod_mlogit_gender <-
mod_mlogit_gender |>
dplyr::rowwise() |>
dplyr::mutate(stats = list(md_stats(model)), plot = list(md_plot(stats, stat = "score_mean100")))
#> Scale for y is already present.
#> Adding another scale for y, which will replace the existing scale.
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.
#> Scale for y is already present.
#> Adding another scale for y, which will replace the existing scale.
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.
#> Scale for y is already present.
#> Adding another scale for y, which will replace the existing scale.
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.