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
Variable types: Any
When to use: You drew 4 plots and want to arrange them in order.
Functions: cowplot package: plot_grid()
Comment: It aligns the plots automatically, and lets you set the number of rows and columns and the order of the plots.
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 ratiostable_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 headertbl_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