如何更改 R 中 gt 表中 tab_spanner 文本的颜色和样式?

问题描述 投票:0回答:1

我正在尝试调整 R 中 gt 表的 tab_spanner 文本的颜色和样式。当前的 tab_spanners 为“Curveball”、“Slider”、“Sweeper”、“Changeup”(见图),我想每一个都加粗并赋予它们独特的颜色。 (例如,我想将“Curveball”设为红色,“Slider”设为蓝色,“Sweeper”设为绿色,“Changeup”设为橙色)。

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))

我尝试添加类似的东西

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

但这不起作用。 非常感谢任何帮助!

r colors gt
1个回答
0
投票

您想使用

tab_style()
来格式化扳手。由于您已经使用标签来识别它们,因此您可以通过名称来识别它们。如果您只是想让它们加粗,您可以在一次调用中完成,但我不确定如何将多种颜色应用于多个扳手,因此有点重复。但这会让你得到你想要的。

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")))
  )
  
© www.soinside.com 2019 - 2024. All rights reserved.