
Add background colors to group headers with automatic gt conversion
Source:R/styling.R
add_group_colors.RdConvenience function that adds background
colors to variable group headers and converts the
table to gt. This is a terminal operation that
combines get_group_rows(), gtsummary::as_gt(),
and gt::tab_style() into a single pipeable
function.
For text formatting (bold/italic), use
add_group_styling() before calling this function.
Arguments
- tbl
A gtsummary table object with variable group headers created by
gtsummary::add_variable_group_header()- color
Background color(s) for group headers. Default
"#E8E8E8"(light gray). Accepts a single color (applied to all groups) or a vector of colors (one per group). Can be any valid CSS color (hex code, color name, rgb(), etc.).
Value
A gt table object with colored group headers. Note: This is a terminal operation that converts to gt. You cannot pipe to additional gtsummary functions after calling this function.
Details
This function:
Identifies group header rows with
get_group_rows()Converts the table to gt with
gtsummary::as_gt()Applies background color using
gt::tab_style()
Since this function converts to gt, it should be
used as the final styling step in your pipeline.
Apply all gtsummary functions (like
modify_caption(), modify_footnote(), etc.) and
text formatting with add_group_styling() before
calling add_group_colors().
See also
add_group_styling()for text formatting only (stays gtsummary)get_group_rows()for identifying group header rowsgtsummary::add_variable_group_header()for creating variable groupsgt::tab_style()for additional gt-specific styling
Examples
# \donttest{
# Basic usage - text formatting then color
gtsummary::trial |>
gtsummary::tbl_summary(by = trt) |>
extras() |>
gtsummary::add_variable_group_header(
header = "Patient Characteristics",
variables = age:stage
) |>
add_group_styling() |>
add_group_colors()
Overall
N = 2001
Drug A
N = 981
Drug B
N = 1021
p-value2
1 Median (Q1, Q3); n (%)
2 Wilcoxon rank sum test; Pearson’s Chi-squared test
# Custom color - light blue
gtsummary::trial |>
gtsummary::tbl_summary(by = trt) |>
extras() |>
gtsummary::add_variable_group_header(
header = "Baseline Characteristics",
variables = age:marker
) |>
add_group_styling() |>
add_group_colors(color = "#E3F2FD")
Overall
N = 2001
Drug A
N = 981
Drug B
N = 1021
p-value2
1 Median (Q1, Q3); n (%)
2 Wilcoxon rank sum test; Pearson’s Chi-squared test
# Bold only formatting with custom color
gtsummary::trial |>
gtsummary::tbl_summary(by = trt) |>
extras() |>
gtsummary::add_variable_group_header(
header = "Clinical Measures",
variables = marker:stage
) |>
add_group_styling(format = "bold") |>
add_group_colors(color = "#FFF9E6")
Overall
N = 2001
Drug A
N = 981
Drug B
N = 1021
p-value2
1 Median (Q1, Q3); n (%)
2 Wilcoxon rank sum test; Pearson’s Chi-squared test
# Multiple group headers with same color
gtsummary::trial |>
gtsummary::tbl_summary(by = trt) |>
extras() |>
gtsummary::add_variable_group_header(
header = "Demographics",
variables = age
) |>
gtsummary::add_variable_group_header(
header = "Disease Measures",
variables = marker:response
) |>
add_group_styling() |>
add_group_colors(color = "#E8E8E8")
Overall
N = 2001
Drug A
N = 981
Drug B
N = 1021
p-value2
1 Median (Q1, Q3); n (%)
2 Wilcoxon rank sum test; Pearson’s Chi-squared test
# Different colors per group
gtsummary::trial |>
gtsummary::tbl_summary(by = trt) |>
extras() |>
gtsummary::add_variable_group_header(
header = "Demographics",
variables = age
) |>
gtsummary::add_variable_group_header(
header = "Disease Measures",
variables = marker:response
) |>
add_group_styling() |>
add_group_colors(color = c("#E3F2FD", "#FFF9E6"))
Overall
N = 2001
Drug A
N = 981
Drug B
N = 1021
p-value2
1 Median (Q1, Q3); n (%)
2 Wilcoxon rank sum test; Pearson’s Chi-squared test
# }