Plot moderation curves with Johnson-Neyman highlights
Source:R/plot_moderation_curve.R
plot_moderation_curve.Rdplot_moderation_curve() visualises how an indirect effect
(theta_curve) or a path coefficient (path_curve) varies along a
continuous moderator W.
The routine
extracts the requested record (
path_name) fromresult$moderation, preferringtheta_curvewhen it is available in both curves;draws the conditional effect (
Estimate) against the raw moderator grid (W_raw);overlays the Monte-Carlo confidence band (
CI.LL,CI.UL) and finds every Johnson–Neyman segment whose 95 % CI excludes zero (CI.LL * CI.UL > 0);shades these significant regions and annotates each with its start / end percentiles (for example,
"sig 12.5%-38.3%").
Visual elements
Red ribbon – overall 95 % confidence band (
ns_fill);Green ribbon – significant Johnson–Neyman intervals (
sig_fill);Solid line – point estimate;
Dashed h-line – zero reference;
Dashed v-lines – J–N bounds.
Usage
plot_moderation_curve(
result,
path_name,
title = NULL,
x_label = "Moderator (W)",
y_label = "Estimate",
ns_fill = "#FEE0D2",
sig_fill = "#C7E9C0",
alpha_ci = 0.35,
alpha_sig = 0.35,
base_size = 14
)Arguments
- result
A
wsMed()result that contains a$moderationelement.- path_name
Exact name of the path to plot (e.g.
"indirect_1_2"or"b_1_2"). When the name exists in both curves,theta_curveis used.- title
Optional plot title (default
sprintf("Effect Curve: (%s)", path_name)).- x_label, y_label
Axis labels. Defaults are
"Moderator (W)"and"Estimate".- ns_fill, sig_fill
Fill colours for the confidence band and the significant regions.
- alpha_ci, alpha_sig
Alpha values for the two ribbons.
- base_size
Base font size passed to
ggplot2::theme_minimal().
Examples
data("example_data", package = "wsMed")
result <- wsMed(
data = example_data,
M_C1 = c("A1","B1","C1"),
M_C2 = c("A2","B2","C2"),
Y_C1 = "D1",
Y_C2 = "D2",
form = "CP",
W = "D3",
W_type = "continuous",
MP = c("a1","b2","d1","cp","b_1_2","d_1_2"),
)
plot_moderation_curve(result, "indirect_effect_1_2") # indirect
plot_moderation_curve(result, "b_1_2") # direct path