rgt

Is there a way to change the color of the tab_spanner when creating a gt table?


I am wondering if I'm able to change the color of the tab spanner in a gt table in r. I would like to change the color to white but I haven't been able to do so.

Tab Spanner image

I've tried different methods particularly using the cell_borders function:

library(gt)
#> Warning: package 'gt' was built under R version 4.3.3
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

table <- data.frame(Team = c("Yankees", "Dodgers", "Mets"), League = c("American", "National", "National"), `2023` = c(94, 98, 90), `2024` = c(98, 92, 100)) %>% rename(`2023` = "X2023", `2024` = "X2024")

table %>% gt() %>%  tab_spanner(label = "Wins", columns = 3:4, id = "W") %>% tab_style(style = cell_borders(sides = "bottom", "white", weight = px(3)), locations = cells_column_spanners(spanners = "W"))

And I've also tried using the column_labels.border.bottom.color argument inside of the tab_options function but when i do that the tab spanner changes color but so does the the line below the column header:

library(gt)
#> Warning: package 'gt' was built under R version 4.3.3
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

table <- data.frame(Team = c("Yankees", "Dodgers", "Mets"), League = c("American", "National", "National"), `2023` = c(94, 98, 90), `2024` = c(98, 92, 100)) %>% rename(`2023` = "X2023", `2024` = "X2024")

table %>% gt() %>%  tab_spanner(label = "Wins", columns = 3:4, id = "W") %>% tab_style(style = cell_borders(sides = "bottom", "white", weight = px(3)), locations = cells_column_spanners(spanners = "W")) %>% tab_options(column_labels.border.bottom.color = "white")

Thank you


Solution

  • I haven't found an option to achieve your desired result via tab_style or tab_options but one can use opt_css to set the border color or width or ... for the column spanner like so:

    library(gt)
    
    table <- data.frame(
      Team = c("Yankees", "Dodgers", "Mets"),
      League = c("American", "National", "National"),
      `2023` = c(94, 98, 90),
      `2024` = c(98, 92, 100),
      check.names = FALSE
    )
    
    table %>%
      gt() %>%
      tab_spanner(
        label = "Wins",
        columns = 3:4,
        id = "W"
      ) %>%
      opt_css(
        css = "
        #W .gt_column_spanner {
          border-bottom-color: red;
          border-bottom-width: 3px;
          border-bottom-style: dashed
        }
        "
      )
    

    enter image description here

    And for your desired result we can do:

    table %>%
      gt() %>%
      tab_spanner(
        label = "Wins",
        columns = 3:4,
        id = "W"
      ) %>%
      opt_css(
        css = "
        #W .gt_column_spanner {
          border-bottom-color: white;
          border-bottom-width: 3px
        }
        "
      )
    

    enter image description here