Code
soldiers %>%
select(sex, heightcm, weightkg) %>%
tbl_summary()gtsummary, gt, and gtExtras
Steen Flammild Harsted
January 10, 2024
gt and gtsummary packages and load them.Use install.packages() to download the gt and gtsummary packages. When you have done that add library(gt) and library(gtsummary) to the code chunk where you call your libraries. Execute the lines.
gtsummarysoldiers use tbl_summary() to show the sex, heightcm, weightkg, and race of the soldierssoldiers use tbl_summary() to show the sex, heightcm, weightkg, split by WritingPreference of the soldiersTry the following functions:
add_overall()add_stat_label()bold_labels()italicize_levels()as_gt() %>% gtsave(filename = here(""))You probably need to investigate the help file for tbl_summary() to solve these. * Change the statistics to mean and sd * Change the statistical test of the continous variables from a “Kruskal-Wallis rank sum test” to a One-way ANOVA * Find better names for sex, heightcm, and weightkg * save the table as a .docx file in your tables folder
my_table <- soldiers %>%
select(sex, heightcm, weightkg, WritingPreference) %>%
tbl_summary(
by = WritingPreference,
missing = "no",
# Change labels
label = list(
sex ~ "Sex",
weightkg ~ "Weight (kg)",
heightcm ~ "Height (cm)"),
# Change statistics
statistic = list(all_continuous() ~ "{mean} ({sd})")
) %>%
# t.test
add_p(
test = list(all_continuous() ~ "aov",
all_categorical() ~ "chisq.test.no.correct")
) %>%
bold_labels() %>%
italicize_levels() %>%
add_overall()
my_table
my_table %>%
as_gt() %>%
gtsave(filename = here("tables", "my_table.docx"))gtgt are and what they dotab_*()fmt_*()cols_*()cells_*()Below is a suggestion for soldiers, but you are free to try with you own data if you prefer that.
Using soldiers and gt(), create a table in the following steps:
Installation, sex, and all the columns that ends with circumference,Fort Rucker - it only has one soldierInstallation and sexacross() function inside summarise(). If you are going to be working with a dataset that has many columns, I suggest you invest some time into learning about across()gt() and set the rowname_col argument to sexmy_tblmy_tbl <- soldiers %>%
# Select some columns and arrange the tible
select(Installation, sex,
ends_with("circumference")) %>%
# Remove Fort Rucker
filter(Installation != "Fort Rucker") %>%
# Remove the Installation with only one Soldier
#group_by(Installation) %>%
#add_count() %>%
#filter(n > 1) %>%
# Summary stats by Installation and Race
group_by(Installation, sex) %>%
summarise(
across(.cols = ends_with("circumference"),
.fns = list(mean = ~ mean(.x, na.rm = TRUE),
sd = ~ sd(.x, na.rm = TRUE)))) %>%
# group only by installation
# because we want to use sex in the rowname_col argument in gt() - see below
group_by(Installation) %>%
# Send to gt and perform a few styling functions
gt(rowname_col = "sex") %>%
tab_header(
title = md("**Overview of soldiers soldiers by sex and installation**"),
subtitle = md("*The data is a mock up version of the soldiers dataset*")) %>%
tab_footnote(
footnote = "To preserve anonymity, observations from Fort Rucker has been removed becuase of a low number of observations"
)
my_tblmy_tbl as you likeYou can some try some of these functions
tab_header()
tab_source_note()
tab_stubhead()
tab_spanner()
tab_spanner_delim()
fmt_number()
fmt_percent()
fmt_missing()
col_merge_n_pct()
cols_label()
md()
cells_body() and tab_footnote()
# It is always a choice how much you want to style your in R, and what you leave for manual editing afterwards (e.g. Word)
# You can style everything in R, but it can be code intensive
# In general - you want the basic structure of your table to be in place
# You NEVER want to manually edit values or merge columns.
# Editing column and spanners names are less labour intensive and dont contain the same risk of making errors
# I think the below table is an ok place to stop the styling in R.
my_tbl_styled <- my_tbl %>%
tab_spanner_delim(
delim = "_",
columns = everything()
) %>%
fmt_number(
columns = contains("circumference"),
decimals = 1
) %>%
cols_merge(
columns = contains("thigh"),
pattern = "{1} ({2})"
) %>%
cols_merge(
columns = contains("waist"),
pattern = "{1} ({2})"
) %>%
cols_merge(
columns = contains("ankle"),
pattern = "{1} ({2})"
) %>%
cols_merge(
columns = contains("biceps"),
pattern = "{1} ({2})"
) %>%
cols_merge(
columns = contains("calf"),
pattern = "{1} ({2})"
)
my_tbl_styledgtsave()tab_options()gtExtrasInstall the package gtExtras and add library(gtExtras) to the codechunk where you load your libraries.
gt_theme_thighcircumference_mean with Hulk colors using gt_hulk_col_numeric(thighcircumference_mean)