rgtsummary

Overall percentages based on total count


Using tbl_summary function from the gtsummary package, I get the below table:

df <- as.data.frame(penguins)
df %>%
   tbl_summary(by = species,
               include = everything(),
               digits = list(
                   all_categorical() ~ c(0,1),
                   all_continuous() ~ 1),
               percent = "row",
               missing = "ifany")%>%
   add_overall(last = T)

This is the output the code above provides gtsummary output using "penguins" dataset and "species" as the categorical variable

I would like for my "Overall" column to have the percent calculated over the column, giving these values:

gtsummary output using penguins dataset with overall characteristics

Is there a way to do this? Besides making a table with the categories and one with the overall and merge the two.


Solution

  • The percent argument of tbl_summary() is inherited by add_overall(). The only thing that we need to do, is to change that attribute before passing tbl_summary object to add_overall().

    library(gtsummary)
    
    as.data.frame(penguins) |>
      tbl_summary(by = species,
                  include = everything(),
                  digits = list(
                    all_categorical() ~ c(0,1),
                    all_continuous() ~ 1),
                  percent = "row",
                  missing = "ifany") |>
      # change percent to column instead of row
      (\(x) {x$inputs$percent <- "column"; x })() |>
      add_overall(last = T)
    

    Created on 2025-07-09 with reprex v2.1.1