I am trying to format a table in gt with grouped rows, but so that the label is positioned to the left of the rows instead of above the rows. Is there a way to make spanner row labels (similar to spanner column labels, but for rows) or to re-position the row group label to the side?
Here is a mock-up of what I'd like it to look like.
Please bear with me, I'm quite new to R, but here is a reprex (I hope) of my relevant code.
library(pnwflights14)
flight_data <- flights %>%
group_by(origin, carrier, month) %>%
summarise(monthly_avg = mean(dep_delay)) %>%
ungroup() %>%
group_by(month, origin) %>%
mutate(best_time_month = min(monthly_avg),
worst_time_month = max(monthly_avg),
best_worst = case_when(monthly_avg == best_time_month ~ "best_time",
monthly_avg == worst_time_month ~ "worst_time",
monthly_avg > best_time_month ~ "remove")) %>%
filter(best_worst != "remove") %>%
pivot_wider(names_from = best_worst, values_from = c(carrier, monthly_avg)) %>%
select(-best_time_month, -worst_time_month) %>%
ungroup() %>%
arrange(month)
flight_tbl <- gt(data = flight_data, rowname_col = "month") %>%
tab_spanner(
label = "Best Delay Time",
columns = vars(carrier_best_time, monthly_avg_best_time)) %>%
tab_spanner(
label = "Worst Delay Time",
columns = vars(carrier_worst_time, monthly_avg_worst_time)) %>%
tab_stubhead(label = "Month") %>%
cols_label(carrier_best_time = "Carrier",
monthly_avg_best_time = "Mean delay (min)",
carrier_worst_time = "Carrier",
monthly_avg_worst_time = "Mean delay (min)") %>%
tab_options(table_body.hlines.width = 0) %>%
cols_align(align = "center")