Search code examples
rgt

Add space between columns in gt()


enter image description hereI am dealing with a considerable volume of data and aim to enhance the visual clarity of the tables I have generated.

Referring to the following example, I want to expand the separation between the three main sections of the figure. To be more precise, I am looking to introduce greater spacing between Part 1 and Part 2, as well as between Part 2 and Part 3.

I appreciate any help. Thank you very much

library(gt)

exibble |>
  gt(rowname_col = "row", groupname_col = "group") |>
  summary_rows(
    groups = "grp_a",
    columns = c(num, currency),
    fns = c("min", "max")
  ) |>
  grand_summary_rows(
    columns = currency,
    fns = total ~ sum(., na.rm = TRUE)
  ) |>
  tab_source_note(source_note = "This is a source note.") |>
  tab_footnote(
    footnote = "This is a footnote.",
    locations = cells_body(columns = 1, rows = 1)
  ) |>
  tab_header(
    title = "The title of the table",
    subtitle = "The table's subtitle"
  ) |>
  tab_spanner(label = md("**Part 1**"),
              columns = c(num, char),
              level = 2
  )|>
  tab_spanner(label = md("**Part 2**"),
              columns = c(fctr, date, time),
              level = 2
  )|>
  tab_spanner(label = md("**Part 3**"),
              columns = c(datetime, currency),
              level = 2)][1]][1] 

Solution

  • You can insert extra columns between the groups using cols_add, and then rename the columns to white space using cols_label to make them (mostly) vanish. You can also add the argument missing_text = "" to summary_rows and grand_summary_rows.

      cols_add('TEST1' = '', .after = 'char') |>
      cols_add('TEST2' = '', .after = 'time') |>
      cols_label('TEST1' = md('   '),
                 'TEST2' = md('   '))