Skip to contents

Adds customizable formatting to variable group headers in gtsummary tables. Variable groups are created using gtsummary::add_variable_group_header() to organize variables into sections. This function makes group headers stand out from individual variable labels.

Usage

add_group_styling(tbl, format = c("bold", "italic"), indent_labels = 0L)

Arguments

tbl

A gtsummary table object (e.g., from tbl_summary(), tbl_regression())

format

Character vector specifying text formatting. Options include "bold", "italic", or both. Default is c("bold", "italic").

indent_labels

Integer specifying indentation level (in spaces) for variable labels under group headers. Default is 0L (left-aligned). Set to 4L to preserve gtsummary's default group indentation, or use any non-negative integer for custom spacing.

Value

A gtsummary table object with specified formatting applied to variable group headers

Details

The function targets rows where row_type == 'variable_group' and applies the specified text formatting to the label column.

By default, variable labels are left-aligned (indent_labels = 0L) to distinguish them from categorical levels and statistics. Use indent_labels = 4L to preserve the default gtsummary behavior where grouped variables are indented under their group headers.

See also

Examples

# \donttest{
# Default formatting (bold and italic)
gtsummary::trial |>
  gtsummary::tbl_summary(by = trt, include = c(age, marker, grade)) |>
  gtsummary::add_variable_group_header(
    header = "Patient Characteristics",
    variables = age:grade
  ) |>
  add_group_styling()
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
Patient Characteristics

Age 46 (37, 60) 48 (39, 56)
    Unknown 7 4
Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21)
    Unknown 6 4
Grade

    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
1 Median (Q1, Q3); n (%)
# Bold only gtsummary::trial |> gtsummary::tbl_summary(by = trt, include = c(age, marker)) |> gtsummary::add_variable_group_header( header = "Demographics", variables = age:marker ) |> add_group_styling(format = "bold")
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
Demographics

Age 46 (37, 60) 48 (39, 56)
    Unknown 7 4
Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21)
    Unknown 6 4
1 Median (Q1, Q3)
# Multiple group headers gtsummary::trial |> gtsummary::tbl_summary(by = trt) |> gtsummary::add_variable_group_header( header = "Demographics", variables = age ) |> gtsummary::add_variable_group_header( header = "Clinical Measures", variables = marker:response ) |> add_group_styling()
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
Demographics

Age 46 (37, 60) 48 (39, 56)
    Unknown 7 4
Clinical Measures

Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21)
    Unknown 6 4
T Stage

    T1 28 (29%) 25 (25%)
    T2 25 (26%) 29 (28%)
    T3 22 (22%) 21 (21%)
    T4 23 (23%) 27 (26%)
Grade

    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
Tumor Response 28 (29%) 33 (34%)
    Unknown 3 4
Patient Died 52 (53%) 60 (59%)
Months to Death/Censor 23.5 (17.4, 24.0) 21.2 (14.5, 24.0)
1 Median (Q1, Q3); n (%)
# Custom indentation for grouped variables gtsummary::trial |> gtsummary::tbl_summary(by = trt, include = c(age, marker)) |> gtsummary::add_variable_group_header( header = "Patient Measures", variables = age:marker ) |> add_group_styling(indent_labels = 4L) # gtsummary indentation restored
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
Patient Measures

    Age 46 (37, 60) 48 (39, 56)
    Unknown 7 4
    Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21)
    Unknown 6 4
1 Median (Q1, Q3)
# }