7  Plotting odds ratios (forest plots)

Regression results are often presented not only as tables but also as forest plots (figures that line up estimates and confidence intervals as horizontal bars). In this chapter, we turn the logistic regression from Chapter 4 into a forest plot.

7.1 The simplest way: plot()

Passing a gtsummary regression table directly to plot() gives a forest plot (it uses the ggstats package internally).

The result is a ggplot, so you can adjust it by adding ggplot2 functions with +.

7.2 Table and figure side by side: ggstats::ggcoef_table()

In one line you can create the layout often seen in papers: “variables on the left, the plot in the middle, and OR (95% CI) and p-values on the right”.

Note

ggcoef_table() takes the model itself (m), not a gtsummary table. Variable labels are taken from the label attributes of the data.

7.3 Building it yourself: ggplot from the table data

When you want full control over the fine details of the appearance, extract the contents of the gtsummary table (table_body) and draw it with ggplot2. The advantage is that the numbers in the table and the figure are guaranteed to match.

7.3.1 Step 1: Extract the data

  • row_type == "label": the variable-name row (no estimate for categorical variables)
  • reference_row == TRUE: the reference-category row (no estimate)

7.3.2 Step 2: Prepare it for plotting

7.3.3 Step 3: The forest plot

TipWhy a log axis?

An odds ratio of 2 (double) and one of 0.5 (half) represent the same effect size. On an ordinary axis their distances from 1 look different (1 vs 0.5), but on a log axis they are symmetric around 1.

7.3.4 Step 4: Add columns of numbers on the right

With the patchwork package, place text columns for “OR (95% CI)” and “p-value” to the right of the plot.

7.4 Univariable and multivariable side by side

Drawing crude odds ratios (univariable) and adjusted odds ratios (multivariable) in the same figure makes the effect of confounding visible.

7.5 Saving for a paper (on your own PC)

# common in author guidelines: TIFF / 300 dpi / 17 cm wide
ggsave("figure2_forest.tiff", plot = last_plot(),
       width = 17, height = 8, units = "cm", dpi = 300, compression = "lzw")

# vector format (does not degrade when enlarged)
ggsave("figure2_forest.pdf", width = 17, height = 8, units = "cm")
ImportantChapter summary
  • Quick: plot(tbl_regression(...)); table + figure: ggstats::ggcoef_table(model)
  • Full control: extract tbl$table_body and draw it with ggplot2 → table and figure numbers match
  • Odds ratios and hazard ratios on a log axis, with a vertical dotted line at 1
  • patchwork lets you place columns of numbers beside the plot