I'm trying to create a contingency table using the gtsummary and gt packages in R. I have two questions that I'm hoping will correct the table I've made so far:
How can I move the row group name to the stubhead so that it is in the same line as the other column names?
How can I center the row labels under the row group name? In the attached image (which is output with the code below), the row labels aren't properly centered.
RacexEth <- REDCapTab %>%
filter(PTDbloods >= 1) %>%
tbl_cross(mrace8905, eth8905,
label = list(mrace8905 ~ "Race", eth8905 ~ "Ethnicity")) %>%
bold_labels() %>%
modify_column_alignment(columns = everything(), align = "center") %>%
as_gt() %>%
cols_width(everything() ~ px(140)) %>%
cols_align(align = "center", columns = everything()) %>%
tab_header(title = md("**Race by Ethnicity**")) %>%
tab_style(style = list(cell_text(align = "center")),
locations = cells_stub())
RacexEth
I've tried using the tab_style() function with cells_stub() and cells_stubhead() to center align the row labels but to no avail. I'm unsure how (or if) you can move the row group name to the stubhead in a contingency table.

Since you are not sharing your data, I am using
datasets::Titanicas an example.Created on 2024-03-13 with reprex v2.0.2
For your example, it would be like this: