Search code examples
rcolorsgt

How do I change the color and style of tab_spanner text in a gt table within R?


I am trying to adjust the color and styling of the tab_spanner text of a gt table in R. The current tab_spanners read "Curveball", "Slider", "Sweeper", "Changeup" (see image) and I'd like to bold each of these and give them each unique color. (For example, I'd like to make "Curveball" red, "Slider" blue, "Sweeper" green, "Changeup" orange).

enter image description here

library(gt)
library(gtExtras)

df1 <- structure(list(matchup_pitcher_full_name = c("Trevor Megill", 
        "Mark Leiter Jr.", "Ben Brown", "Ray Kerr", "Blake Snell"), whiff_rate = c(55,53.6, 51, 50.6, 50), 
        matchup_pitcher_full_name.1 = c("Josh Hader","Jason Adam", "Andrew Chafin", "Luis García", "Brandon Hughes"),
        whiff_rate.1 = c(60.8, 57, 56.2, 53.4, 52.5), matchup_pitcher_full_name.2 = c("Luke Little","Ryan Weathers", "Danny Young", "Porter Hodge", "Blake Treinen"), 
        whiff_rate.2 = c(54.3, 53.1, 52.4, 51.8, 50), matchup_pitcher_full_name.3 = c("Angel Chivilli","Paul Skenes", "MacKenzie Gore", "Carlos Rodón", "Devin Williams"), 
        whiff_rate.3 = c(55.9, 54.8, 51.9, 49.5, 48.8)), row.names = c(NA,5L), class = "data.frame")
 
 df1 %>% gt() %>% 
   
   cols_label(
              starts_with("matchup") ~ md("Pitcher"),
              starts_with("whiff") ~ md("Whiff%")
   ) %>%  

   tab_header(title = 'Top Ten Whiff Rates by Pitch (Breaking/Offspeed)',
              subtitle = 'Minimum 100 Pitches Thrown (2024 Season)')  %>% 
   tab_source_note(source_note = "Data: Statcast via Baseball Savant and baseballr") %>% 
   opt_table_font(
     font = list(
       google_font(name = "Work Sans"),
       "Cochin", "serif"
     )
   ) %>% 
   tab_spanner(label = "Curveball", columns = c(1:2)) %>% 
   tab_spanner(label = "Slider", columns = c(3:4)) %>% 
   tab_spanner(label = "Sweeper", columns = c(5:6)) %>% 
   tab_spanner(label = "Changeup", columns = c(7:8))

I have tried adding something like

       cell_text(color = "red")),
     locations = tab_spanner(
       columns = 1:6))

but this does not work. Any help is much appreciated!


Solution

  • You want to use tab_style() to format your spanners. And since you've already identified them with labels, you can identify them by name. If you just wanted them bold, you could do it in a single call, but I'm not sure how to apply multiple colors to multiple spanners, so it's a bit repetitive. But this will get you what you want.

    library(gt)
    library(gtExtras)
    
    df1 <- structure(list(matchup_pitcher_full_name = c("Trevor Megill", 
                                                        "Mark Leiter Jr.", "Ben Brown", "Ray Kerr", "Blake Snell"), whiff_rate = c(55,53.6, 51, 50.6, 50), 
                          matchup_pitcher_full_name.1 = c("Josh Hader","Jason Adam", "Andrew Chafin", "Luis García", "Brandon Hughes"),
                          whiff_rate.1 = c(60.8, 57, 56.2, 53.4, 52.5), matchup_pitcher_full_name.2 = c("Luke Little","Ryan Weathers", "Danny Young", "Porter Hodge", "Blake Treinen"), 
                          whiff_rate.2 = c(54.3, 53.1, 52.4, 51.8, 50), matchup_pitcher_full_name.3 = c("Angel Chivilli","Paul Skenes", "MacKenzie Gore", "Carlos Rodón", "Devin Williams"), 
                          whiff_rate.3 = c(55.9, 54.8, 51.9, 49.5, 48.8)), row.names = c(NA,5L), class = "data.frame")
    
    df1 %>% gt() %>% 
      
      cols_label(
        starts_with("matchup") ~ md("Pitcher"),
        starts_with("whiff") ~ md("Whiff%")
      ) %>%  
      
      tab_header(title = 'Top Ten Whiff Rates by Pitch (Breaking/Offspeed)',
                 subtitle = 'Minimum 100 Pitches Thrown (2024 Season)')  %>% 
      tab_source_note(source_note = "Data: Statcast via Baseball Savant and baseballr") %>% 
      opt_table_font(
        font = list(
          google_font(name = "Work Sans"),
          "Cochin", "serif"
        )
      ) %>% 
      tab_spanner(label = "Curveball", columns = c(1:2)) %>% 
      tab_spanner(label = "Slider", columns = c(3:4)) %>% 
      tab_spanner(label = "Sweeper", columns = c(5:6)) %>% 
      tab_spanner(label = "Changeup", columns = c(7:8)) |> 
      tab_style(
        style = cell_text(weight = "bold",
                          size = px(13),
                          align = "center",
                          color = "red"),
        locations = list(
          cells_column_spanners(matches("Curveball")))
      ) |> 
      tab_style(
        style = cell_text(weight = "bold",
                          size = px(13),
                          align = "center",
                          color = "blue"),
        locations = list(
          cells_column_spanners(matches("Slider")))
      ) |> 
      tab_style(
        style = cell_text(weight = "bold",
                          size = px(13),
                          align = "center",
                          color = "darkgreen"),
        locations = list(
          cells_column_spanners(matches("Sweeper")))
      ) |> 
      tab_style(
        style = cell_text(weight = "bold",
                          size = px(13),
                          align = "center",
                          color = "orange"),
        locations = list(
          cells_column_spanners(matches("Changeup")))
      )