MR_forest: Generate Forest Plots across Multiple MR Methods

View source: R/mr_forest.R

MR_forestR Documentation

Generate Forest Plots across Multiple MR Methods

Description

Creates a forest plot comparing causal estimates across different MR methods (e.g., IVW, RAPS, Egger).

Usage

MR_forest(
  summary_df,
  effect,
  custom_xlim = NULL,
  dot_size = 3,
  axis_text_size = 10,
  axis_title_size = 12,
  pval_text_size = 3,
  clamp_nonpositive = FALSE,
  log_scale = TRUE
)

Arguments

summary_df

MR results data frame, typically the output from run_mr_analysis().

effect

Character string or vector indicating the effect scale ("Beta", "OR", or "HR").

custom_xlim

Optional numeric vector of length 2 for x-axis limits.

dot_size

Numeric value specifying the point size. Default is 3.

axis_text_size

Numeric value for axis font size.

axis_title_size

Numeric value for title font size.

pval_text_size

Numeric value for p-value label size.

clamp_nonpositive

Logical; whether non-positive estimates should be clamped to a small positive value before log-transformation.

log_scale

Logical; if TRUE and effect is "OR" or "HR", the x-axis label shows log(OR) or log(HR). Note that MR_forest always plots on the log scale internally for OR/HR; this argument only controls the axis label. Default is TRUE.

Value

An MRForestPlots object containing one ggplot per outcome-exposure pair, with causal estimates compared across MR methods. Use export_forest_plots() to write plots to disk with optional filtering.

Examples


data("merged_data")
input3   <- harmonize_mr_data(df = merged_data)$input_df
outcome3 <- run_mr_analysis(
  MR_input_data     = input3,
  outcome.form      = c("Beta","OR"),
  use_ivw           = TRUE,
  use_raps          = TRUE,
  use_median        = TRUE,
  use_egger         = TRUE,
  use_mr_presso     = TRUE,
  use_mr_horse      = TRUE,
  use_mr_grip       = TRUE,
  NbDistribution    = 1000,
  SignifThreshold   = 0.05,
  mr_horse_n_iter   = 5000,
  mr_horse_n_burnin = 1000,
  mr_grip_parameters = NULL
)

mr_plots <- MR_forest(
  summary_df        = outcome3,
  effect            = c("Beta","OR"),
  custom_xlim       = NULL,
  dot_size          = 3,
  axis_text_size    = 10,
  axis_title_size   = 12,
  pval_text_size    = 3,
  clamp_nonpositive = FALSE,
  log_scale         = TRUE
)

# Retrieve the exact outcome/exposure labels stored in the object
out_name <- mr_plots@outcomes[1]   # "fi_49item"
exp_name <- mr_plots@exposures[1]  # "Zn"

# Export all method-level plots as PDF (commented — writes to disk)
export_forest_plots(mr_plots, save_dir = tempdir(), file_type = "png")

# Export plots for one outcome only
export_forest_plots(mr_plots, save_dir = tempdir(), outcome = out_name)

# Export plots for one exposure only
export_forest_plots(mr_plots, save_dir = tempdir(), exposure = exp_name)

# Export one specific outcome-exposure pair
export_forest_plots(mr_plots, save_dir = tempdir(), outcome = out_name, exposure = exp_name)


autoMR documentation built on June 12, 2026, 9:07 a.m.

Related to MR_forest in autoMR...