
Create a merged gtsummary table by faceting comparisons across multiple categorical variables
Source:R/MakeFacetCatComparisonTable.R
MakeFacetCatComparisonTable.RdGenerates a series of comparison tables using MakeComparisonTable() for each categorical
variable (facet) in the provided list and merges them side-by-side using gtsummary::tbl_merge().
This function extends the functionality of MakeComparisonTable() by automatically detecting
which facet variables are categorical (factor or character) and producing a faceted summary
of how the main comparison variable (e.g., Cluster, TreatmentArm) differs across multiple
categorical dimensions such as Race, Sex, or HIV status.
Usage
MakeFacetCatComparisonTable(
data,
FacetVariables,
variables,
covariates = NULL,
value_digits = 2,
p_digits = 3,
AddEffectSize = FALSE,
effect_size_digits = 2,
AddPairwise = FALSE,
PairwiseMethod = "bonferroni",
Parametric = TRUE,
ParametricDisplay = NULL,
IncludeOverallN = FALSE,
IncludeMissing = FALSE,
suppress_warnings = FALSE,
Referent = NULL,
IncludeOverallStats = FALSE,
ShowPositiveBinaryOnLabel = TRUE,
CompFun = MakeComparisonTable,
Relabel = TRUE,
TreatOrdinalAs = "Categorical",
...,
DataFrame = lifecycle::deprecated(),
Variables = lifecycle::deprecated(),
Covariates = lifecycle::deprecated(),
ValueDigits = lifecycle::deprecated(),
pDigits = lifecycle::deprecated(),
EffectSizeDigits = lifecycle::deprecated()
)Arguments
- data
A data frame containing all variables to be analyzed.
- FacetVariables
A character vector of variable names to facet by. The function automatically selects those that are categorical (
factororcharacter).- variables
A character string naming the variable(s) being compared (e.g., "Cluster").
- covariates
Optional character vector of covariate names to adjust for.
- value_digits
Number of decimal digits to display for numeric values (default = 2).
- p_digits
Number of decimal digits to display for p-values (default = 3).
- AddEffectSize
Logical; if TRUE, include effect sizes (default = FALSE).
- effect_size_digits
Decimal digits for effect size values (default = 2).
- AddPairwise
Logical; if TRUE, include pairwise comparisons (default = FALSE).
- PairwiseMethod
Method for pairwise comparison p-value adjustment (default = "bonferroni").
- Parametric
Logical; if TRUE, use parametric tests (default = TRUE).
- ParametricDisplay
Optional vector specifying which statistics to display for parametric tests.
- IncludeOverallN
Logical; if TRUE, adds overall N to the table (default = FALSE).
- IncludeMissing
Logical; if TRUE, includes missing categories (default = FALSE).
- suppress_warnings
Logical; suppress internal warnings (default = FALSE).
- Referent
Optional string specifying the referent category for binary or categorical comparisons.
- IncludeOverallStats
Logical; if TRUE, adds overall descriptive statistics (default = FALSE).
- ShowPositiveBinaryOnLabel
Logical; if TRUE, labels binary variables with positive outcome (default = TRUE).
- CompFun
Comparison function to apply; defaults to
MakeComparisonTable.- Relabel
Logical; if TRUE (default), use attached variable labels.
- TreatOrdinalAs
How ordinal variables are treated in each table.
- ...
Additional arguments passed to the comparison function.
- DataFrame
Deprecated (since 19.15.0). Use
datainstead.- Variables
Deprecated (since 19.15.0). Use
variablesinstead.- Covariates
Deprecated (since 19.15.0). Use
covariatesinstead.- ValueDigits
Deprecated (since 19.15.0). Use
value_digitsinstead.- pDigits
Deprecated (since 19.15.0). Use
p_digitsinstead.- EffectSizeDigits
Deprecated (since 19.15.0). Use
effect_size_digitsinstead.
Value
A gtsummary table created by merging each facet's MakeComparisonTable() output
side-by-side using gtsummary::tbl_merge(). Each facet variable is labeled with its own
tab spanner header for clarity.
Details
MakeFacetCatComparisonTable
Use this when the same set of measures needs to be compared across several different groupings at once - by diagnosis, and by sex, and by APOE status - and those comparisons belong in one table rather than three.
Running MakeComparisonTable() once per grouping produces tables that are
each correct but cannot be read against each other: the variables are listed
three times, and spotting that a biomarker separates diagnosis groups but not
sexes means looking back and forth between pages. Merging them puts one row
per variable and one column block per grouping, so that comparison is a
left-to-right read.
Facet variables that are not categorical are dropped automatically, so a vector of candidate groupings can be passed without pre-filtering it.
Every argument that controls the individual tables - AddPairwise,
covariates, Parametric, and the rest - is passed through to each facet,
so the blocks stay consistent with one another.
See also
MakeComparisonTable() for a single grouping, and MakeTable1()
for a plain descriptive table with no grouping at all.
Examples
# \donttest{
data(SampleData)
data(SampleVariableTypes)
Labelled <- RevalueData(SampleData, SampleVariableTypes)$RevaluedData
vars_Compare <- c("age", "AXL", "Adiponectin", "tau", "p_tau")
# The same five measures compared across two groupings at once
MakeFacetCatComparisonTable(
data = Labelled,
FacetVariables = c("Diagnosis", "sex"),
variables = vars_Compare
)
Comparison by Diagnosis (values: mean (SD)).
Control
N = 2421
Impaired
N = 911
p-value2
Test2
Female
N = 2041
Male
N = 1291
p-value2
Test2
1 Mean (SD)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes.
# Non-categorical facets are ignored, so a mixed vector is safe
MakeFacetCatComparisonTable(
data = Labelled,
FacetVariables = c("Diagnosis", "age", "sex"),
variables = c("AXL", "tau")
)
Comparison by Diagnosis (values: mean (SD)).
Control
N = 2421
Impaired
N = 911
p-value2
Test2
Female
N = 2041
Male
N = 1291
p-value2
Test2
1 Mean (SD)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes.
# Options are forwarded to every facet
MakeFacetCatComparisonTable(
data = Labelled,
FacetVariables = c("Diagnosis", "sex"),
variables = vars_Compare,
AddEffectSize = TRUE
)
Comparison by Diagnosis (values: mean (SD)).
Control
N = 2421
Impaired
N = 911
p-value2
Test2
Effect size
ES method
Female
N = 2041
Male
N = 1291
p-value2
Test2
Effect size
ES method
1 Mean (SD)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes.
# Covariate adjustment, applied within each grouping
MakeFacetCatComparisonTable(
data = Labelled,
FacetVariables = c("Diagnosis", "sex"),
variables = c("AXL", "Adiponectin", "tau", "p_tau"),
covariates = "age"
)
Comparison by Diagnosis (values: mean (SD)). p-values adjusted for Age.
Control
N = 2421
Impaired
N = 911
p-value2
Test2
Female
N = 2041
Male
N = 1291
p-value2
Test2
1 Mean (SD)
2 p-values use Type II ANCOVA for continuous outcomes and logistic likelihood-ratio test for binary outcomes or multinomial likelihood-ratio test for multicategory outcomes for categorical outcomes.
# A three-level grouping alongside a two-level one, with pairwise contrasts
Labelled$ApoeGroup <- factor(
dplyr::case_when(
Labelled$Genotype %in% c("E2E2", "E2E3") ~ "E2 carrier",
Labelled$Genotype == "E3E3" ~ "E3/E3",
TRUE ~ "E4 carrier"
),
levels = c("E2 carrier", "E3/E3", "E4 carrier")
)
MakeFacetCatComparisonTable(
data = Labelled,
FacetVariables = c("Diagnosis", "ApoeGroup"),
variables = c("age", "tau", "p_tau"),
AddPairwise = TRUE
)
Comparison by Diagnosis (values: mean (SD)). Pairwise p-values are bonferroni-adjusted within outcome.
Control
N = 2421
Impaired
N = 911
p-value2
Test2
Control - Impaired
E2 carrier
N = 391
E3/E3
N = 1671
E4 carrier
N = 1271
p-value2
Test2
E2 carrier - E3/E3
E2 carrier - E4 carrier
E3/E3 - E4 carrier
1 Mean (SD)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes. Pairwise p-values are bonferroni-adjusted within outcome.
# }