nflverse / nflseedR

Functions to Efficiently Simulate and Evaluate NFL Seasons
https://nflseedr.com
Other
19 stars 3 forks source link

`gt` code for output tables #11

Closed jthomasmock closed 2 years ago

jthomasmock commented 3 years ago

Howdy folks!

I've got a few different table outputs via gt that correspond nicely to the outputs from sims

I'll clean up the code to not rely on espnscrapeR, and embed the code proper into a fully-contained function.

https://gist.github.com/jthomasmock/3576e0620fe54ed84e02aa371444778b

mrcaseb commented 3 years ago

Awesome! I would have added it already if espnscrapeR was a CRAN package because I think CRAN doesn't allow non CRAN or non bioconductor dependencies.

mrcaseb commented 3 years ago

I asked Tucker to post his code for the tables in this tweet just to have a variant. I like the style and think it's basically @jthomasmock 's code with some minor tweaks.

tuckerboynton22 commented 3 years ago

As @mrcaseb said, my template is based on @jthomasmock's 538 replication with a few minor tweaks.

Here's the full code for the theme & table construction:

Screen Shot 2021-02-27 at 8 25 04 PM
gt_theme_538 <- function(data,...) {
  data %>%
    opt_all_caps()  %>%
    opt_table_font(
      font = list(
        google_font("Chivo"),
        default_fonts()
      )
    ) %>%
    tab_style(
      style = cell_borders(
        sides = "bottom", color = "black", weight = px(3)
      ),
      locations = cells_body(
        columns = TRUE,
        # This is a relatively sneaky way of changing the bottom border
        # Regardless of data size
        rows = nrow(data$`_data`)
      )
    )  %>% 
    tab_options(
      column_labels.background.color = "white",
      table.border.top.width = px(3),
      table.border.top.color = "transparent",
      table.border.bottom.color = "transparent",
      table.border.bottom.width = px(3),
      table_body.border.bottom.color = "black",
      column_labels.border.top.width = px(3),
      column_labels.border.top.color = "black",
      column_labels.vlines.style = "solid",
      column_labels.border.bottom.width = px(3),
      column_labels.border.bottom.color = "black",
      data_row.padding = px(3),
      source_notes.font.size = 12,
      table.font.size = 16,
      heading.align = "left",
      source_notes.border.bottom.style = "solid",
      source_notes.border.bottom.width = px(3),
      source_notes.border.bottom.color = "black",
      ...
    ) 
}

sims$overall %>%
  # filter(conf == "AFC") %>%
  arrange(desc(won_sb)) %>%
  gt() %>%
  cols_hide(columns = 1:2) %>%
  tab_header(
    title = "2020 NFL Season Simulation, AFC",
    subtitle = "Based on Vegas odds | Season simulated 10,000 times"
  ) %>%
  gt_theme_538() %>%
  tab_source_note(md("**Table:** @Tucker_TnL | **Data:** @nflfastR, @nflseedR")) %>%
  cols_label(division = "Division",
             team = "Team",
             wins = "Wins",
             playoff = "Made Playoffs",
             div1 = "Won Division",
             seed1 = "1st Seed",
             won_conf = "Won Conference",
             won_sb = "Won Super Bowl",
             draft1 = "1st Overall Pick",
             draft5 = "Top 5 Pick") %>%
  cols_align(align = "center", columns = c(3:11)) %>%
  data_color(
    columns = 4:9,
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(
        palette = "ggsci::green_material"
      ) %>% as.character(),
      domain = NULL
    )
  ) %>%
  data_color(
    columns = 10:11, 
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(
        palette = "ggsci::red_material"
      ) %>% as.character(),
      domain = NULL
    )
  ) %>%
  fmt_number(columns = 4, decimals = 2) %>%
  fmt_percent(columns = 5:11, decimals = 2)