5

I want to add a thicker (px(2)) vertical line between each of the spanner columns that runs throughout the figure. I have looked through the tab_options and have not found a combination that allows for the line to go through all of the table at the column spanner locations. I am using the gt package.

Here is some example data:

gtcars %>%
  dplyr::select(
    -mfr, -trim, bdy_style, drivetrain,
    -drivetrain, -trsmn, -ctry_origin
  ) %>%
  dplyr::slice(1:8) %>%
  gt(rowname_col = "model") %>%
  tab_spanner(
    label = "performance",
    columns = c(
      hp, hp_rpm, trq, trq_rpm,
      mpg_c, mpg_h
    )
  )

In this data, I would like the vertical line on either side of year, bdy_style, performance, and msrp.

Thanks!

stefan
  • 90,330
  • 6
  • 25
  • 51
melmo
  • 757
  • 3
  • 15

1 Answers1

3

Since msrp and bdy_style share a border with the performance spanner, targeting those will also get the performance borders. Just target the year, bdy_style, and msrp columns.

gtcars %>%
  dplyr::select(
    -mfr, -trim, bdy_style, drivetrain,
    -drivetrain, -trsmn, -ctry_origin
  ) %>%
  dplyr::slice(1:8) %>%
  gt(rowname_col = "model") %>%
  tab_spanner(
    label = "performance",
    columns = c(
      hp, hp_rpm, trq, trq_rpm,
      mpg_c, mpg_h
    )
  ) %>% 
  tab_style(
    style = cell_borders(
      sides = c("left", "right"),
      weight = px(2)),
    locations = cells_body(
      columns = c(year, bdy_style, msrp)
      )
    )

You don't technically have to even include year since it won't adjust the left border as that is the stub border. If you want that border as well, you have to do that separately:

  tab_options(
    stub.border.width = px(2),
    stub.border.color = "black"
  )
Abigail
  • 370
  • 1
  • 11