2021 winners
2022 winners
2024 winners
gtsummary
gtsummary
package provides an elegant and flexible way to create publication-ready analytical and summary tablesgt
gt
and gtsummary
gtsummary
Characteristic | N = 2001 |
---|---|
Age | 47 (38, 57) |
Unknown | 11 |
Grade | |
I | 68 (34%) |
II | 68 (34%) |
III | 64 (32%) |
Tumor Response | 61 (32%) |
Unknown | 7 |
Chemotherapy Treatment | |
Drug A | 98 (49%) |
Drug B | 102 (51%) |
1 Median (Q1, Q3); n (%) |
gtsummary
gtsummary
Variable | N | Drug A N = 981 |
Drug B N = 1021 |
p-value2 |
---|---|---|---|---|
Age | 189 | 46 (37, 60) | 48 (39, 56) | 0.7 |
Grade | 200 | 0.9 | ||
I | 35 (36%) | 33 (32%) | ||
II | 32 (33%) | 36 (35%) | ||
III | 31 (32%) | 33 (32%) | ||
Tumor Response | 193 | 28 (29%) | 33 (34%) | 0.5 |
1 Median (Q1, Q3); n (%) | ||||
2 Wilcoxon rank sum test; Pearson’s Chi-squared test |
gtsummary
To save a gtsummary
table we:
gt
table using as_gt()
gtsave()
functiongtsummary
Changing descriptive statistics
tbl_summary(
trial2,
by = trt,
missing = "no", # don't show NA,
statistic = list(all_continuous() ~ "{mean} ({sd})")
)
Characteristic | Drug A N = 981 |
Drug B N = 1021 |
---|---|---|
Age | 47 (15) | 47 (14) |
Grade | ||
I | 35 (36%) | 33 (32%) |
II | 32 (33%) | 36 (35%) |
III | 31 (32%) | 33 (32%) |
Tumor Response | 28 (29%) | 33 (34%) |
1 Mean (SD); n (%) |
gtsummary
add test
Characteristic | Drug A N = 981 |
Drug B N = 1021 |
p-value2 |
---|---|---|---|
Age | 47 (15) | 47 (14) | 0.7 |
Grade | 0.9 | ||
I | 35 (36%) | 33 (32%) | |
II | 32 (33%) | 36 (35%) | |
III | 31 (32%) | 33 (32%) | |
Tumor Response | 28 (29%) | 33 (34%) | 0.5 |
1 Mean (SD); n (%) | |||
2 Wilcoxon rank sum test; Pearson’s Chi-squared test |
gtsummary
Changing test
Characteristic | Drug A N = 981 |
Drug B N = 1021 |
p-value2 |
---|---|---|---|
Age | 47 (15) | 47 (14) | 0.8 |
Grade | 0.9 | ||
I | 35 (36%) | 33 (32%) | |
II | 32 (33%) | 36 (35%) | |
III | 31 (32%) | 33 (32%) | |
Tumor Response | 28 (29%) | 33 (34%) | 0.5 |
1 Mean (SD); n (%) | |||
2 Welch Two Sample t-test; Pearson’s Chi-squared test |
See test names here
gtsummary
Changing test
tbl_summary(
trial2,
by = trt,
missing = "no", # don't show NA,
statistic = list(all_continuous() ~ "{mean} ({sd})")
) |>
add_p(
test = list("response" ~ "t.test",
"age" ~ "wilcox.test")
)
Characteristic | Drug A N = 981 |
Drug B N = 1021 |
p-value2 |
---|---|---|---|
Age | 47 (15) | 47 (14) | 0.7 |
Grade | 0.9 | ||
I | 35 (36%) | 33 (32%) | |
II | 32 (33%) | 36 (35%) | |
III | 31 (32%) | 33 (32%) | |
Tumor Response | 28 (29%) | 33 (34%) | 0.5 |
1 Mean (SD); n (%) | |||
2 Wilcoxon rank sum test; Pearson’s Chi-squared test; Welch Two Sample t-test |
See test names here
gtsummary
tbl_cross()
Cross table
Chemotherapy Treatment
|
Total | ||
---|---|---|---|
Drug A | Drug B | ||
Grade | |||
I | 35 | 33 | 68 |
II | 32 | 36 | 68 |
III | 31 | 33 | 64 |
Total | 98 | 102 | 200 |
See test names here
gtsummary
tbl_cross()
Cross table
Chemotherapy Treatment
|
Total | p-value1 | ||
---|---|---|---|---|
Drug A | Drug B | |||
Grade | 0.9 | |||
I | 35 | 33 | 68 | |
II | 32 | 36 | 68 | |
III | 31 | 33 | 64 | |
Total | 98 | 102 | 200 | |
1 Pearson’s Chi-squared test |
See test names here
gtsummary()
gt
gt
A grammar of Tablesgt
dplyr
, tidyr
, etc.gt
packagecut %in% c("Fair", "Ideal") & color %in% c("D", "G")
T1 <- diamonds |>
group_by(cut, color) |>
summarise(price_median = median(price),
price_iqr = IQR(price),
mm3_mean = mean(x*y*z),
mm3_sd = sd(x*y*z),
n = n()) |>
mutate(n_freq = n / sum(n)) |>
ungroup() |>
mutate(n_freq_tot = n / sum(n)) |>
arrange(cut, color) |>
filter(cut %in% c("Fair", "Ideal") &
color %in% c("D", "G"))
T1
# A tibble: 4 × 9
cut color price_median price_iqr mm3_mean mm3_sd n n_freq n_freq_tot
<ord> <ord> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl>
1 Fair D 3730 2592. 146. 64.3 163 0.101 0.00302
2 Fair G 3057 3086 161. 77.1 314 0.195 0.00582
3 Ideal D 1576 2248. 93.1 48.9 2834 0.132 0.0525
4 Ideal G 1858. 4694. 115. 67.0 4884 0.227 0.0905
gt()
Notice the effect of group_by()
cut | color | price_median | price_iqr | mm3_mean | mm3_sd | n | n_freq | n_freq_tot |
---|---|---|---|---|---|---|---|---|
Fair | D | 3730.0 | 2592.50 | 145.60641 | 64.32012 | 163 | 0.1012422 | 0.003021876 |
Fair | G | 3057.0 | 3086.00 | 160.87748 | 77.09566 | 314 | 0.1950311 | 0.005821283 |
Ideal | D | 1576.0 | 2247.75 | 93.05613 | 48.92116 | 2834 | 0.1315020 | 0.052539859 |
Ideal | G | 1857.5 | 4693.50 | 115.04085 | 66.97330 | 4884 | 0.2266252 | 0.090545050 |
color | price_median | price_iqr | mm3_mean | mm3_sd | n | n_freq | n_freq_tot |
---|---|---|---|---|---|---|---|
Fair | |||||||
D | 3730.0 | 2592.50 | 145.60641 | 64.32012 | 163 | 0.1012422 | 0.003021876 |
G | 3057.0 | 3086.00 | 160.87748 | 77.09566 | 314 | 0.1950311 | 0.005821283 |
Ideal | |||||||
D | 1576.0 | 2247.75 | 93.05613 | 48.92116 | 2834 | 0.1315020 | 0.052539859 |
G | 1857.5 | 4693.50 | 115.04085 | 66.97330 | 4884 | 0.2266252 | 0.090545050 |
gt()
color | price_median | price_iqr | mm3_mean | mm3_sd | n | n_freq | n_freq_tot |
---|---|---|---|---|---|---|---|
Fair | |||||||
D | 3730.0 | 2592.50 | 145.60641 | 64.32012 | 163 | 0.1012422 | 0.003021876 |
G | 3057.0 | 3086.00 | 160.87748 | 77.09566 | 314 | 0.1950311 | 0.005821283 |
Ideal | |||||||
D | 1576.0 | 2247.75 | 93.05613 | 48.92116 | 2834 | 0.1315020 | 0.052539859 |
G | 1857.5 | 4693.50 | 115.04085 | 66.97330 | 4884 | 0.2266252 | 0.090545050 |
price_median | price_iqr | mm3_mean | mm3_sd | n | n_freq | n_freq_tot | |
---|---|---|---|---|---|---|---|
Fair | |||||||
D | 3730.0 | 2592.50 | 145.60641 | 64.32012 | 163 | 0.1012422 | 0.003021876 |
G | 3057.0 | 3086.00 | 160.87748 | 77.09566 | 314 | 0.1950311 | 0.005821283 |
Ideal | |||||||
D | 1576.0 | 2247.75 | 93.05613 | 48.92116 | 2834 | 0.1315020 | 0.052539859 |
G | 1857.5 | 4693.50 | 115.04085 | 66.97330 | 4884 | 0.2266252 | 0.090545050 |
tab_
Creates or modifies parts of the overall table structure
fmt_
Formats the data in the table
col_
Modifies the Columns
cells_
Location helpers for targeting specific cells
gtsave
and as_
Export functionstab_
functions
tab_header()
Add a table headertab_spanner()
Add a spanner column labeltab_spanner_delim()
Create column labels and spanners via delimited namestab_row_group()
Add a row group to a gt tabletab_stubhead()
Add label text to the stubheadtab_footnote()
Add a table footnotetab_source_note()
Add a source note citationtab_style()
Add custom styles to one or more cellstab_options()
Modify the table output optiontab_header
and tab_source_note()
T2 <- T1 |>
group_by(cut) |>
gt(rowname_col = "color") |>
tab_header(
title = "Price and mm3 of diamonds",
subtitle = "Only displaying ´Fair´ and ´Ideal` cuts") |>
tab_source_note(
source_note = "Data is from the Diamonds dataset included in the ggplot2 package") |>
tab_stubhead(
label = "Cut Color"
)
T2
Price and mm3 of diamonds | |||||||
---|---|---|---|---|---|---|---|
Only displaying ´Fair´ and ´Ideal` cuts | |||||||
Cut Color | price_median | price_iqr | mm3_mean | mm3_sd | n | n_freq | n_freq_tot |
Fair | |||||||
D | 3730.0 | 2592.50 | 145.60641 | 64.32012 | 163 | 0.1012422 | 0.003021876 |
G | 3057.0 | 3086.00 | 160.87748 | 77.09566 | 314 | 0.1950311 | 0.005821283 |
Ideal | |||||||
D | 1576.0 | 2247.75 | 93.05613 | 48.92116 | 2834 | 0.1315020 | 0.052539859 |
G | 1857.5 | 4693.50 | 115.04085 | 66.97330 | 4884 | 0.2266252 | 0.090545050 |
Data is from the Diamonds dataset included in the ggplot2 package |
tab_spanner()
Price and mm3 of diamonds | |||||||
---|---|---|---|---|---|---|---|
Only displaying ´Fair´ and ´Ideal` cuts | |||||||
Cut Color | price_median | price_iqr | mm3_mean | mm3_sd |
Counts and frequencies
|
||
n | n_freq | n_freq_tot | |||||
Fair | |||||||
D | 3730.0 | 2592.50 | 145.60641 | 64.32012 | 163 | 0.1012422 | 0.003021876 |
G | 3057.0 | 3086.00 | 160.87748 | 77.09566 | 314 | 0.1950311 | 0.005821283 |
Ideal | |||||||
D | 1576.0 | 2247.75 | 93.05613 | 48.92116 | 2834 | 0.1315020 | 0.052539859 |
G | 1857.5 | 4693.50 | 115.04085 | 66.97330 | 4884 | 0.2266252 | 0.090545050 |
Data is from the Diamonds dataset included in the ggplot2 package |
tab_spanner_delim()
Price and mm3 of diamonds | |||||||
---|---|---|---|---|---|---|---|
Only displaying ´Fair´ and ´Ideal` cuts | |||||||
Cut Color |
price
|
mm3
|
Counts and frequencies
|
||||
median | iqr | mean | sd | n | n_freq | n_freq_tot | |
Fair | |||||||
D | 3730.0 | 2592.50 | 145.60641 | 64.32012 | 163 | 0.1012422 | 0.003021876 |
G | 3057.0 | 3086.00 | 160.87748 | 77.09566 | 314 | 0.1950311 | 0.005821283 |
Ideal | |||||||
D | 1576.0 | 2247.75 | 93.05613 | 48.92116 | 2834 | 0.1315020 | 0.052539859 |
G | 1857.5 | 4693.50 | 115.04085 | 66.97330 | 4884 | 0.2266252 | 0.090545050 |
Data is from the Diamonds dataset included in the ggplot2 package |
fmt_
functions
fmt_number()
Format numeric valuesfmt_integer()
Format values as integersfmt_scientific()
Format values to scientific notationfmt_engineering()
Format values to engineering notationfmt_percent()
Format values as a percentagefmt_currency()
Format values as currenciesfmt_bytes()
Format values as bytesfmt_date()
Format values as datesfmt_time()
Format values as timesfmt_datetime()
Format values as date-timesfmt_markdown()
Format Markdown textfmt_passthrough()
Format by simply passing data throughfmt_missing()
Format missing valuesfmt_currency()
, fmt_number()
, and fmt_percent()
Price and mm3 of diamonds | |||||||
---|---|---|---|---|---|---|---|
Only displaying ´Fair´ and ´Ideal` cuts | |||||||
Cut Color |
price
|
mm3
|
Counts and frequencies
|
||||
median | iqr | mean | sd | n | n_freq | n_freq_tot | |
Fair | |||||||
D | $3,730 | $2,592 | 145.6 | 64.3 | 163 | 10.1% | 0.3% |
G | $3,057 | $3,086 | 160.9 | 77.1 | 314 | 19.5% | 0.6% |
Ideal | |||||||
D | $1,576 | $2,248 | 93.1 | 48.9 | 2834 | 13.2% | 5.3% |
G | $1,858 | $4,694 | 115.0 | 67.0 | 4884 | 22.7% | 9.1% |
Data is from the Diamonds dataset included in the ggplot2 package |
col_
functionsThe cols_*()
functions allow for modifications that act on entire columns.
cols_align()
Set the alignment of columnscols_width()
Set the widths of columnscols_label()
Relabel one or more columnscols_move_to_start()
Move one or more columns to the startcols_move_to_end()
Move one or more columns to the endcols_move()
Move one or more columnscols_hide()
Hide one or more columnscols_unhide()
Unhide one or more columnscols_merge_range()
Merge two columns to a value range columncols_merge_uncert()
Merge two columns to a value & uncertainty columncols_merge_n_pct()
Merge two columns to combine counts and percentagescols_merge()
Merge data from two or more columns to a single columncol_merge_n_pct()
and cols_label()
Price and mm3 of diamonds | ||||||
---|---|---|---|---|---|---|
Only displaying ´Fair´ and ´Ideal` cuts | ||||||
Cut Color |
price
|
mm3
|
Counts and frequencies
|
|||
median | iqr | mean | sd | n (% of group) | % of total | |
Fair | ||||||
D | $3,730 | $2,592 | 145.6 | 64.3 | 163 (10.1%) | 0.3% |
G | $3,057 | $3,086 | 160.9 | 77.1 | 314 (19.5%) | 0.6% |
Ideal | ||||||
D | $1,576 | $2,248 | 93.1 | 48.9 | 2834 (13.2%) | 5.3% |
G | $1,858 | $4,694 | 115.0 | 67.0 | 4884 (22.7%) | 9.1% |
Data is from the Diamonds dataset included in the ggplot2 package |
cells_
and other helper functions
md()
Interpret input text as Markdown-formatted text
cells_*()
functions are used for targeting cells with the locations =
argument in the tab_footnote()
, tab_style()
, and text_transform()
functions.
cells_title()
Location helper for targeting the table title and subtitlecells_stubhead()
Location helper for targeting the table stubhead cellcells_column_spanners()
Location helper for targeting the column spannerscells_column_labels()
Location helper for targeting the column labelscells_row_groups()
Location helper for targeting row groupscells_stub()
Location helper for targeting cells in the table stubcells_body()
Location helper for targeting data cells in the table bodycells_summary()
Location helper for targeting group summary cellscells_grand_summary()
Location helper for targeting cells in a grand summarycells_stub_summary()
Location helper for targeting the stub cells in a summarycells_stub_grand_summary()
Location helper for targeting the stub cells in a grand summarycells_footnotes()
Location helper for targeting the footnotescells_source_notes()
Location helper for targeting the source notesmd()
Price and mm3 of diamonds | ||||||
---|---|---|---|---|---|---|
Only displaying Fair and Ideal cuts | ||||||
Cut Color |
price
|
mm3
|
Counts and frequencies
|
|||
median | iqr | mean | sd | n (% of group) | % of total | |
Fair | ||||||
D | $3,730 | $2,592 | 145.6 | 64.3 | 163 (10.1%) | 0.3% |
G | $3,057 | $3,086 | 160.9 | 77.1 | 314 (19.5%) | 0.6% |
Ideal | ||||||
D | $1,576 | $2,248 | 93.1 | 48.9 | 2834 (13.2%) | 5.3% |
G | $1,858 | $4,694 | 115.0 | 67.0 | 4884 (22.7%) | 9.1% |
Data is from the Diamonds dataset included in the ggplot2 package |
tab_footnote()
and cells_body()
Price and mm3 of diamonds | ||||||
Only displaying Fair and Ideal cuts | ||||||
Cut Color |
price
|
mm3
|
Counts and frequencies
|
|||
---|---|---|---|---|---|---|
median | iqr | mean | sd | n (% of group) | % of total | |
Fair | ||||||
D | $3,730 | $2,592 | 145.6 | 64.3 | 163 (10.1%) | 0.3% |
G | $3,057 | $3,086 | 160.9 | 77.1 | 314 (19.5%) | 0.6% |
Ideal | ||||||
D | 1 $1,576 | $2,248 | 93.1 | 48.9 | 2834 (13.2%) | 5.3% |
G | 1 $1,858 | $4,694 | 115.0 | 67.0 | 4884 (22.7%) | 9.1% |
Data is from the Diamonds dataset included in the ggplot2 package | ||||||
1 OMG this is a CHEAP median price!!!! |
tab_footnote()
and cells_column_labels()
Price and mm3 of diamonds | ||||||
Only displaying Fair and Ideal cuts | ||||||
Cut Color |
price
|
mm3
|
Counts and frequencies
|
|||
---|---|---|---|---|---|---|
median | iqr1 | mean | sd | n (% of group) | % of total | |
Fair | ||||||
D | $3,730 | $2,592 | 145.6 | 64.3 | 163 (10.1%) | 0.3% |
G | $3,057 | $3,086 | 160.9 | 77.1 | 314 (19.5%) | 0.6% |
Ideal | ||||||
D | 2 $1,576 | $2,248 | 93.1 | 48.9 | 2834 (13.2%) | 5.3% |
G | 2 $1,858 | $4,694 | 115.0 | 67.0 | 4884 (22.7%) | 9.1% |
Data is from the Diamonds dataset included in the ggplot2 package | ||||||
1 Interquartile range | ||||||
2 OMG this is a CHEAP median price!!!! |
gtExtras
themesgtExtras
comes with 8 different themes (HTML only)Price and mm3 of diamonds | ||||||
Only displaying Fair and Ideal cuts | ||||||
Cut Color |
price
|
mm3
|
Counts and frequencies
|
|||
---|---|---|---|---|---|---|
median | iqr1 | mean | sd | n (% of group) | % of total | |
Fair | ||||||
D | $3,730 | $2,592 | 145.6 | 64.3 | 163 (10.1%) | 0.3% |
G | $3,057 | $3,086 | 160.9 | 77.1 | 314 (19.5%) | 0.6% |
Ideal | ||||||
D | 2 $1,576 | $2,248 | 93.1 | 48.9 | 2834 (13.2%) | 5.3% |
G | 2 $1,858 | $4,694 | 115.0 | 67.0 | 4884 (22.7%) | 9.1% |
Data is from the Diamonds dataset included in the ggplot2 package | ||||||
1 Interquartile range | ||||||
2 OMG this is a CHEAP median price!!!! |
gtsave