| Title: | Publication-Ready Forest Plots with 'ggplot2' |
|---|---|
| Description: | Transform model coefficients into flexible forest plots using 'ggplot2'. Provides helpers to standardize coefficient data from a range of modelling workflows and render publication-ready forest plots with a consistent interface. |
| Authors: | Carson Richardson [aut, cre, cph] |
| Maintainer: | Carson Richardson <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.2 |
| Built: | 2026-06-04 23:19:54 UTC |
| Source: | https://github.com/thatoneguy006/ggforestplotr |
Compose a summary table onto a forest plot.
add_forest_table( plot = NULL, position = c("left", "right"), show_terms = TRUE, show_n = NULL, show_events = NULL, show_estimate = TRUE, show_p = FALSE, columns = NULL, term_header = "Term", n_header = "N", events_header = "Events", estimate_label = NULL, p_header = "P-value", column_labels = NULL, digits = NULL, estimate_digits = NULL, interval_digits = NULL, p_digits = NULL, estimate_fmt = NULL, ci_fmt = NULL, text_size = NULL, header_text_size = NULL, header_fontface = "bold", header_family = NULL, striped_rows = NULL, stripe_fill = NULL, stripe_colour = NULL, stripe_alpha = NULL, grid_lines = FALSE, grid_line_colour = "black", grid_line_size = 0.3, grid_line_linetype = 1 )add_forest_table( plot = NULL, position = c("left", "right"), show_terms = TRUE, show_n = NULL, show_events = NULL, show_estimate = TRUE, show_p = FALSE, columns = NULL, term_header = "Term", n_header = "N", events_header = "Events", estimate_label = NULL, p_header = "P-value", column_labels = NULL, digits = NULL, estimate_digits = NULL, interval_digits = NULL, p_digits = NULL, estimate_fmt = NULL, ci_fmt = NULL, text_size = NULL, header_text_size = NULL, header_fontface = "bold", header_family = NULL, striped_rows = NULL, stripe_fill = NULL, stripe_colour = NULL, stripe_alpha = NULL, grid_lines = FALSE, grid_line_colour = "black", grid_line_size = 0.3, grid_line_linetype = 1 )
plot |
A plot created by |
position |
Whether to place the table on the left or right of the forest plot. |
show_terms |
Deprecated. Whether to show the term column in the table.
Use |
show_n |
Deprecated. Whether to show the |
show_events |
Deprecated. Whether to show the |
show_estimate |
Deprecated. Whether to show the formatted estimate and
confidence interval column. Use |
show_p |
Deprecated. Whether to display the p-value column. Use
|
columns |
Optional explicit columns to display in the side table, in
the order they should appear. Accepts built-in names such as |
term_header |
Header text for the term column. |
n_header |
Header text for the |
events_header |
Header text for the |
estimate_label |
Header label for the estimate column. Defaults to the model-derived label when available. |
p_header |
Header text for the p-value column. |
column_labels |
Optional named vector used to relabel table column
headers. Names should match values supplied to |
digits |
Deprecated. Number of digits used when formatting estimates
and p-values. Defaults to |
estimate_digits |
Number of digits used for point estimates. |
interval_digits |
Number of digits used for confidence interval bounds. |
p_digits |
Number of digits used for p-values. |
estimate_fmt |
Format string for the estimate column. Use
|
ci_fmt |
Format string for the confidence interval column when
|
text_size |
Text size for table contents. Defaults to |
header_text_size |
Header text size for table column labels. Defaults
to |
header_fontface |
Font face used for table column labels. Defaults to
|
header_family |
Optional font family used for table column labels. |
striped_rows |
Whether to draw alternating row stripes behind the
table. Defaults to the stripe setting used in |
stripe_fill |
Fill colour used for striped rows. Defaults to the
stripe fill used in |
stripe_colour |
Outline colour for striped rows. Defaults to the
stripe outline used in |
stripe_alpha |
Transparency for striped rows. Defaults to the stripe
alpha used in |
grid_lines |
Whether to draw black horizontal grid lines in the table. |
grid_line_colour |
Colour used for the table grid lines. |
grid_line_size |
Line width used for the table grid lines. |
grid_line_linetype |
Line type used for the table grid lines. |
A patchwork-composed plot containing the forest plot and side
table, or a ggplot add-on object when plot = NULL.
coefs <- data.frame( term = c("Age", "BMI", "Treatment"), estimate = c(0.3, -0.2, 0.4), conf.low = c(0.1, -0.4, 0.2), conf.high = c(0.5, 0.0, 0.6), sample_size = c(120, 115, 98), p_value = c(0.012, 0.031, 0.004) ) p <- ggforestplot(coefs, n = "sample_size", p.value = "p_value") add_forest_table( p, position = "left", columns = c("term", "n", "estimate", "p"), estimate_label = "Beta" ) ggforestplot(coefs, n = "sample_size", p.value = "p_value") + add_forest_table( position = "right", columns = c("term", "n", "estimate", "p"), estimate_label = "Beta" )coefs <- data.frame( term = c("Age", "BMI", "Treatment"), estimate = c(0.3, -0.2, 0.4), conf.low = c(0.1, -0.4, 0.2), conf.high = c(0.5, 0.0, 0.6), sample_size = c(120, 115, 98), p_value = c(0.012, 0.031, 0.004) ) p <- ggforestplot(coefs, n = "sample_size", p.value = "p_value") add_forest_table( p, position = "left", columns = c("term", "n", "estimate", "p"), estimate_label = "Beta" ) ggforestplot(coefs, n = "sample_size", p.value = "p_value") + add_forest_table( position = "right", columns = c("term", "n", "estimate", "p"), estimate_label = "Beta" )
Compose split table blocks around a forest plot so that summary data appear on both sides of the plotting panel.
add_split_table( plot = NULL, show_terms = TRUE, show_n = NULL, show_events = NULL, show_estimate = TRUE, show_p = FALSE, left_columns = NULL, right_columns = NULL, term_header = "Term", n_header = "N", events_header = "Events", estimate_label = NULL, p_header = "P-value", column_labels = NULL, digits = NULL, estimate_digits = NULL, interval_digits = NULL, p_digits = NULL, estimate_fmt = NULL, ci_fmt = NULL, text_size = NULL, header_text_size = NULL, header_fontface = "bold", header_family = NULL, striped_rows = NULL, stripe_fill = NULL, stripe_colour = NULL, stripe_alpha = NULL, left_width = NULL, plot_width = NULL, right_width = NULL )add_split_table( plot = NULL, show_terms = TRUE, show_n = NULL, show_events = NULL, show_estimate = TRUE, show_p = FALSE, left_columns = NULL, right_columns = NULL, term_header = "Term", n_header = "N", events_header = "Events", estimate_label = NULL, p_header = "P-value", column_labels = NULL, digits = NULL, estimate_digits = NULL, interval_digits = NULL, p_digits = NULL, estimate_fmt = NULL, ci_fmt = NULL, text_size = NULL, header_text_size = NULL, header_fontface = "bold", header_family = NULL, striped_rows = NULL, stripe_fill = NULL, stripe_colour = NULL, stripe_alpha = NULL, left_width = NULL, plot_width = NULL, right_width = NULL )
plot |
A plot created by |
show_terms |
Whether to include the term column in the default
left-side selection when |
show_n |
Whether to include the |
show_events |
Whether to include the |
show_estimate |
Whether to include the formatted estimate and
confidence interval column in the default right-side selection when
|
show_p |
Whether to include the p-value column in the default
right-side selection when |
left_columns |
Optional explicit columns to place on the left side of
the forest plot. Accepts built-in names such as |
right_columns |
Optional explicit columns to place on the right side
of the forest plot. Accepts built-in names such as |
term_header |
Header text for the term column. |
n_header |
Header text for the |
events_header |
Header text for the |
estimate_label |
Header label for the estimate column. Defaults to the model-derived label when available. |
p_header |
Header text for the p-value column. |
column_labels |
Optional named vector used to relabel table column
headers. Names should match values supplied to |
digits |
Deprecated. Number of digits used when formatting estimates
and p-values. Defaults to |
estimate_digits |
Number of digits used for point estimates. |
interval_digits |
Number of digits used for confidence interval bounds. |
p_digits |
Number of digits used for p-values. |
estimate_fmt |
Format string for the estimate column. Use
|
ci_fmt |
Format string for the confidence interval column when
table columns include |
text_size |
Text size for table contents. Defaults to |
header_text_size |
Header text size for table column labels. Defaults
to |
header_fontface |
Font face used for table column labels. Defaults to
|
header_family |
Optional font family used for table column labels. |
striped_rows |
Whether to draw alternating row stripes behind the
split table layout. Defaults to the stripe setting used in
|
stripe_fill |
Fill colour used for striped rows. Defaults to the
stripe fill used in |
stripe_colour |
Outline colour for striped rows. Defaults to the
stripe outline used in |
stripe_alpha |
Transparency for striped rows. Defaults to the stripe
alpha used in |
left_width |
Optional width allocated to the left table block. By
default this is derived from the number of displayed left-side columns
relative to |
plot_width |
Optional width allocated to the forest plot panel.
Defaults to |
right_width |
Optional width allocated to the right table block. By
default this is derived from the number of displayed right-side columns
relative to |
A patchwork-composed plot containing a left table, the forest plot,
and a right table, or a ggplot add-on object when plot = NULL.
coefs <- data.frame( term = c("Age", "BMI", "Treatment"), estimate = c(0.3, -0.2, 0.4), conf.low = c(0.1, -0.4, 0.2), conf.high = c(0.5, 0.0, 0.6), sample_size = c(120, 115, 98), p_value = c(0.012, 0.031, 0.004) ) p <- ggforestplot(coefs, n = "sample_size", p.value = "p_value") add_split_table( p, left_columns = c("term", "n"), right_columns = c("estimate", "p"), estimate_label = "HR" ) ggforestplot(coefs, n = "sample_size", p.value = "p_value") + add_split_table( left_columns = c(1, 2), right_columns = c(4, 5), estimate_label = "HR" )coefs <- data.frame( term = c("Age", "BMI", "Treatment"), estimate = c(0.3, -0.2, 0.4), conf.low = c(0.1, -0.4, 0.2), conf.high = c(0.5, 0.0, 0.6), sample_size = c(120, 115, 98), p_value = c(0.012, 0.031, 0.004) ) p <- ggforestplot(coefs, n = "sample_size", p.value = "p_value") add_split_table( p, left_columns = c("term", "n"), right_columns = c("estimate", "p"), estimate_label = "HR" ) ggforestplot(coefs, n = "sample_size", p.value = "p_value") + add_split_table( left_columns = c(1, 2), right_columns = c(4, 5), estimate_label = "HR" )
Standardizes a coefficient table into the internal forest-plot data
structure used throughout ggforestplotR.
as_forest_data( data, term, estimate, conf.low, conf.high, label = term, term_labels = NULL, group = NULL, grouping = NULL, separate_groups = NULL, n = NULL, events = NULL, p.value = NULL, exponentiate = FALSE, sort_terms = c("none", "descending", "ascending") )as_forest_data( data, term, estimate, conf.low, conf.high, label = term, term_labels = NULL, group = NULL, grouping = NULL, separate_groups = NULL, n = NULL, events = NULL, p.value = NULL, exponentiate = FALSE, sort_terms = c("none", "descending", "ascending") )
data |
A data frame containing coefficient estimates and intervals. |
term |
Column name holding the model term identifier. |
estimate |
Column name holding the point estimate. |
conf.low |
Column name holding the lower confidence bound. |
conf.high |
Column name holding the upper confidence bound. |
label |
Optional column name used for the displayed row label. |
term_labels |
Optional named vector used to relabel displayed terms. Names should match values in the term column and values are the labels to display. |
group |
Optional column name used for color-grouping multiple estimates per row. |
grouping |
Optional column name used to split rows into grouped plot sections. |
separate_groups |
Optional column name used to identify labeled variable blocks that can be outlined with separator lines. |
n |
Optional column name holding sample sizes or other N labels for table helpers. |
events |
Optional column name holding event counts or event labels for table helpers. |
p.value |
Optional column name holding p-values. |
exponentiate |
Logical; if |
sort_terms |
How to sort rows: |
A standardized data frame ready for ggforestplot() and the table
composition helpers. Original dataframe columns are retained for table
helpers so they can be displayed with add_forest_table(columns = ...).
raw <- data.frame( variable = c("Age", "BMI", "Treatment"), beta = c(0.10, -0.08, 0.34), lower = c(0.02, -0.16, 0.12), upper = c(0.18, 0.00, 0.56) ) as_forest_data( data = raw, term = "variable", estimate = "beta", conf.low = "lower", conf.high = "upper" )raw <- data.frame( variable = c("Age", "BMI", "Treatment"), beta = c(0.10, -0.08, 0.34), lower = c(0.02, -0.16, 0.12), upper = c(0.18, 0.00, 0.56) ) as_forest_data( data = raw, term = "variable", estimate = "beta", conf.low = "lower", conf.high = "upper" )
Builds a forest plot from standardized coefficient data or directly from a fitted model.
ggforestplot( data, term = "term", estimate = "estimate", conf.low = "conf.low", conf.high = "conf.high", label = term, term_labels = NULL, group = NULL, facet = NULL, facet_strip_position = c("left", "right"), grouping = NULL, grouping_strip_position = NULL, separate_groups = NULL, n = NULL, events = NULL, p.value = NULL, exponentiate = NULL, sort_terms = c("none", "descending", "ascending"), point_size = 2.3, point_shape = 19, linewidth = 0.5, line_size = NULL, staple_width = 0.2, dodge_width = 0.6, separate_lines = FALSE, separator_line_linetype = 2, separator_line_colour = "black", separator_line_size = 0.4, striped_rows = FALSE, stripe_fill = "grey95", stripe_colour = NA, stripe_alpha = 1, ref_line = NULL, ref_label = NULL, ref_linetype = 2, ref_color = "grey60" )ggforestplot( data, term = "term", estimate = "estimate", conf.low = "conf.low", conf.high = "conf.high", label = term, term_labels = NULL, group = NULL, facet = NULL, facet_strip_position = c("left", "right"), grouping = NULL, grouping_strip_position = NULL, separate_groups = NULL, n = NULL, events = NULL, p.value = NULL, exponentiate = NULL, sort_terms = c("none", "descending", "ascending"), point_size = 2.3, point_shape = 19, linewidth = 0.5, line_size = NULL, staple_width = 0.2, dodge_width = 0.6, separate_lines = FALSE, separator_line_linetype = 2, separator_line_colour = "black", separator_line_size = 0.4, striped_rows = FALSE, stripe_fill = "grey95", stripe_colour = NA, stripe_alpha = 1, ref_line = NULL, ref_label = NULL, ref_linetype = 2, ref_color = "grey60" )
data |
Either a tidy coefficient data frame or a model object
supported by |
term |
Column name holding the model term identifiers. |
estimate |
Column name holding the point estimates. |
conf.low |
Column name holding the lower confidence bounds. |
conf.high |
Column name holding the upper confidence bounds. |
label |
Optional column name used for the displayed row labels. |
term_labels |
Optional named vector used to relabel displayed terms. Names should match values in the term column and values are the labels to display. |
group |
Optional column name used for color-grouping estimates. |
facet |
Optional column name used to split rows into faceted plot sections. If this column is a factor, its levels control facet order. |
facet_strip_position |
Positioning for facet strip labels. |
grouping |
Deprecated. Use |
grouping_strip_position |
Deprecated. Use |
separate_groups |
Optional column name used to identify labeled variable blocks that can be outlined with grid lines. |
n |
Optional column name holding sample sizes or other N labels for table helpers. |
events |
Optional column name holding event counts or event labels for table helpers. |
p.value |
Optional column name holding p-values. |
exponentiate |
Logical; if |
sort_terms |
How to sort rows: |
point_size |
Point size for coefficient markers. |
point_shape |
Shape used for coefficient markers. |
linewidth |
Line width for confidence intervals. |
line_size |
Deprecated. Use |
staple_width |
Width of the terminal staples on confidence interval lines. |
dodge_width |
Horizontal dodging used for grouped estimates. |
separate_lines |
Logical; if |
separator_line_linetype |
Line type used for separator lines. |
separator_line_colour |
Colour used for separator lines. |
separator_line_size |
Line width used for separator lines. |
striped_rows |
Logical; if |
stripe_fill |
Fill color used for shaded rows. |
stripe_colour |
Border color for shaded rows. |
stripe_alpha |
Transparency for shaded rows. |
ref_line |
Numeric x-value where the reference line is drawn, or
|
ref_label |
Optional label drawn alongside the reference line. |
ref_linetype |
Line type for the reference line. |
ref_color |
Color for the reference line. |
A ggplot object. Use standard ggplot2 functions such as
ggplot2::labs() for plot labels, and add composition helpers after
styling the main plot.
coefs <- data.frame( term = c("Age", "BMI", "Treatment"), estimate = c(0.10, -0.08, 0.34), conf.low = c(0.02, -0.16, 0.12), conf.high = c(0.18, 0.00, 0.56) ) ggforestplot(coefs) ggforestplot(coefs, striped_rows = TRUE, point_shape = 17)coefs <- data.frame( term = c("Age", "BMI", "Treatment"), estimate = c(0.10, -0.08, 0.34), conf.low = c(0.02, -0.16, 0.12), conf.high = c(0.18, 0.00, 0.56) ) ggforestplot(coefs) ggforestplot(coefs, striped_rows = TRUE, point_shape = 17)
Uses broom::tidy() to convert a fitted model into forest-plot data.
tidy_forest_model( model, conf.int = TRUE, conf.level = 0.95, exponentiate = NULL, intercept = FALSE, term_labels = NULL, sort_terms = c("none", "descending", "ascending") )tidy_forest_model( model, conf.int = TRUE, conf.level = 0.95, exponentiate = NULL, intercept = FALSE, term_labels = NULL, sort_terms = c("none", "descending", "ascending") )
model |
A fitted model object supported by |
conf.int |
Logical; if |
conf.level |
Confidence level for intervals. |
exponentiate |
Logical; passed through to |
intercept |
Logical; if |
term_labels |
Optional named vector used to relabel displayed terms. Names should match model term names and values are the labels to display. |
sort_terms |
How to sort rows: |
A standardized coefficient data frame ready for ggforestplot().
if (requireNamespace("broom", quietly = TRUE)) { fit <- lm(mpg ~ wt + hp + qsec, data = mtcars) tidy_forest_model(fit) set.seed(123) logit_data <- data.frame( age = rnorm(250, mean = 62, sd = 8), bmi = rnorm(250, mean = 28, sd = 4), treatment = factor(rbinom(250, 1, 0.45), labels = c("Control", "Treatment")) ) linpred <- -9 + 0.09 * logit_data$age + 0.11 * logit_data$bmi + 0.9 * (logit_data$treatment == "Treatment") logit_data$event <- rbinom(250, 1, plogis(linpred)) logit_fit <- glm(event ~ age + bmi + treatment, data = logit_data, family = binomial()) tidy_forest_model(logit_fit, exponentiate = TRUE) }if (requireNamespace("broom", quietly = TRUE)) { fit <- lm(mpg ~ wt + hp + qsec, data = mtcars) tidy_forest_model(fit) set.seed(123) logit_data <- data.frame( age = rnorm(250, mean = 62, sd = 8), bmi = rnorm(250, mean = 28, sd = 4), treatment = factor(rbinom(250, 1, 0.45), labels = c("Control", "Treatment")) ) linpred <- -9 + 0.09 * logit_data$age + 0.11 * logit_data$bmi + 0.9 * (logit_data$treatment == "Treatment") logit_data$event <- rbinom(250, 1, plogis(linpred)) logit_fit <- glm(event ~ age + bmi + treatment, data = logit_data, family = binomial()) tidy_forest_model(logit_fit, exponentiate = TRUE) }