Report Results: Figures and Tables (ggplot2 & gtsummary)

Before You Start

  1. How do I know what kind of figure or table I should create?
  2. Where can I find templates?
  3. How do I export my figures or tables?

Basic Procedure

Figure decision tree

From Data to Viz: Find the graphic you need

Report Results

How does the literature report the results?

Summary and Practice

Chart Types

Frequency plot or cross-table

  • Variable types: Categorical + categorical
  • When to use: Plotting demographic variables.
  • Functions: ggplot2 package: ggplot()
  • Resources: The R Graph Gallery

Line chart

Lollipop chart

  • When to use: Each category on the x-axis has one value on the y-axis.

Ridgeline plot and violin plot

  • Variable types: Continuous only, or categorical + continuous
  • When to use: Show how the data are distributed.

Histogram

  • Variable types: Continuous (frequency)
  • When to use: Show the frequency distribution of scores on a scale.

Customize a Plot

Change colors

Reorder a variable

Change the legend position

Change the theme

  • When to use: e.g. change the position of the axis titles.
  • Functions: theme(); hrbrthemes package: theme_ipsum()

Rename the labels on an axis

  • Functions: scale_x_discrete(labels = c("X" = "name"))

Flip the x and y axes

  • Functions: coord_flip()

Change the axis range

Prepare Data for Plotting

Convert data to long format

  • Variable types: Any
  • When to use: Move the variable names into a single new variable: turn all the columns into a dataset with only two columns, where the first column holds the variable names and the second holds the values.
  • Functions: tidyr package: pivot_longer() (replaces the superseded gather())

Select only some of the variables

  • Functions: select() for wide data; filter() for long data, where the variable names are values in one column

Combine and Export

Draw and combine multiple plots

Export figures

Tables

Create a table or cross-table

Generate all combinations of experimental conditions

  • When to use: Generate every combination of the experimental conditions, for a complete experimental design and analysis.
  • Functions: tidyr package: expand_grid()
  • Resources: tidyr: expand_grid

Syntax

Merge tables side by side

The example uses the scored version of the simulated dataset described in Example Data; Generalized Linear Model has a fuller version with covariates. The code runs when the site is built, so the table below it is its output.

library(gtsummary)
mydata <- read.csv("data/mydata_scored.csv")

# One logistic regression per outcome, as odds ratios
table_a <- tbl_regression(
  glm(y1 ~ A_total + B_total, data = mydata, family = binomial),
  exponentiate = TRUE
)
table_b <- tbl_regression(
  glm(y2 ~ A_total + B_total, data = mydata, family = binomial),
  exponentiate = TRUE
)
table_c <- tbl_regression(
  glm(y3 ~ A_total + B_total, data = mydata, family = binomial),
  exponentiate = TRUE
)

# Merge several gtsummary tables side by side, each with its own spanning header
tbl_merge(
  list(table_a, table_b, table_c),
  tab_spanner = c("**y1**", "**y2**", "**y3**")
)
Characteristic
y1
y2
y3
OR 95% CI p-value OR 95% CI p-value OR 95% CI p-value
A_total 1.03 1.01, 1.05 <0.001 1.00 0.98, 1.02 >0.9 1.02 1.01, 1.05 0.014
B_total 1.01 0.99, 1.03 0.3 1.06 1.04, 1.09 <0.001 1.03 1.00, 1.05 0.021
Abbreviations: CI = Confidence Interval, OR = Odds Ratio

Resources & References

Further Study

Recommended classes or training