Christopher M. Uyehara



Preamble

This document, knitted by RStudio under the all-knowing gaze of the great Hadley Wickham (blessed be his name, long may he reign) is a record of a great battle that took place in the year two-thousand and twenty-two of the common era between the members of two star-touched labs. We fought. We cheered. We yelled. We cursed. And then, once the day was done, we made peace. For in the end, though each began the bout with sneers and bold boasts of our acuity and wisdom, each us was ultimately forced to confront the terrors our own ignorance. Not a single one was perfect. Not a single one saw the true end!

Alas, dear reader, it is tempting, but folly, to find fault in our stars. Far greater men and woman have wrought far greater feats in the face of far greater adversity than we humble few. No! We embrace our failures as our own and, through that embrace, put aside our animosity and embrace each other as well. Competitors no longer, we stand tall as one–united in defeat.

So say we all. (So say we all).

library(conflicted)
library(dplyr)
library(magrittr)
library(ggupset)
library(ggside)
library(glue)
library(here)
library(patchwork)
library(stringr)
library(tidyr)
library(purrr)
library(forcats)
library(gtExtras)
library(gt)
library(gridExtra)
library(eulerr)
library(ggtext)

outTheme = theme_bw() + 
  theme(text = element_text(size = 14, color = 'black'),
        axis.text = element_text(color = 'black') )

theme_set(outTheme)
plotBaseBracket = function(bracket.base){
  bracket.plot = bracket.base$horiz %>%
    ggplot(aes(y = y, x = x)) +
    geom_segment(data = bracket.base$horiz, aes(x = x, xend = xend, y = y, yend = y )) + 
    geom_segment(data = bracket.base$vert, aes(xend = xend, yend = yend)) + 
    geom_label(data = bracket.base$labs, aes(label = label), label.size = NA, fill = 'white') + 
    scale_y_reverse() +
    theme_void() + 
    theme(legend.position = 'none',
          plot.title = element_text(hjust = 0.5, size = 16),
          plot.subtitle = element_text(hjust = 0.5, size = 13)) + 
    NULL  
  
 return(bracket.plot) 
}

checkPicks = function(picks, actual.df, round){
  actual.df = actual.df %>%
    select(match, id, label, winLose) %>%
    rename(actual = 'label', win.actual = 'winLose')
  
  picks = picks %>%
    left_join(actual.df, by = c('match', 'id') ) %>%
    dplyr::filter(actual != '') 
  
  if(round != 'F' & round != '3P'){
    picks = picks %>%
      mutate(check = case_when(label == actual ~ 'Correct',
                               label != actual & label %in% actual.df$actual ~ 'In round, wrong pos',
                               T ~ 'Wrong'))      
  } else{
    winner.actual = dplyr::filter(actual.df, win.actual == 'W')
    loser.actual = dplyr::filter(actual.df, win.actual != 'W')
    #print(loser.actual$actual)
    
    picks = picks %>%
      mutate(check = case_when(winLose == 'W' & label == winner.actual$actual ~ 'Correct',
                               winLose == '' & label == loser.actual$actual ~ 'Correct',
                               label %in% actual.df$actual ~ 'In round, wrong pos',
                               T ~ 'Wrong'))
  }

  return(picks) 
}

addWinLose = function(bracket.plot, bracket.df, title){
  bracket.plot +
    geom_text(data = bracket.df, aes(label = label, fontface = font, color = winLose),
              size = 14/.pt) + 
    labs(title = title, subtitle = 'World Cup 2022') + 
    scale_color_manual(values = c('black', 'magenta3')) + 
    NULL
}

addCorrectIncorrect = function(bracket.plot, bracket.df, title){
  bracket.plot +
    geom_text(data = bracket.df, aes(label = label, color = check),
              size = 14/.pt)  + 
    labs(title = title, subtitle = 'World Cup 2022') + 
    scale_color_manual(values = c('springgreen4', 'goldenrod3', 'red2')) + 
    theme(legend.position = 'bottom') + 
    guides(color = guide_legend(title.position = 'top', title.hjust = 0.5))
}
points.df = list(
  Ro16 = list(Correct = 2, `In round, wrong pos` = 1, Wrong = 0),
  QF = list(Correct = 4, `In round, wrong pos` = 2, Wrong = 0),
  SF = list(Correct = 8, `In round, wrong pos` = 4, Wrong = 0),
  `F` = list(Correct = 10, `In round, wrong pos` = 8, Wrong = 0),
  `3P` = list(Correct = 5, `In round, wrong pos` = 4, Wrong = 0)
  ) %>%
  dplyr::bind_rows(.id = 'round') %>%
  pivot_longer(-round, names_to = 'check', values_to = 'pts_per_round')

arabicNames = readxl::read_xlsx('./Assets/Data/players.xlsx')

countries = read.csv('./Assets/Data/countries.csv')

bracket.base = list(horiz = './Assets/Data/bracket_horizSeg.csv', 
               vert = './Assets/Data/bracket_vertSeg.csv',
               labs = './Assets/Data/bracket_labs.csv' ) %>%
  lapply(., read.csv)

base.plot = plotBaseBracket(bracket.base)

fifa_picks = read.csv('./Assets/Data/fifa_picks.csv') %>%
  mutate(font = ifelse(winLose == 'W', 'bold', 'plain')) %>%
  dplyr::left_join(., arabicNames, by = 'name') %>%
  dplyr::left_join(countries, by = c('label' = 'country')) %>%
  mutate(title = glue('{name} {arabic}')) %>%
  nest(dat = -c(name, title)) %>%
  mutate(dat = setNames(dat, name)) %>%
  arrange(name) %>%
  mutate(brackets = map2(dat, title, ~addWinLose(base.plot, bracket.df = .x, title = .y)))

Brackets

Note: There was a mixup with the printed brackets that were used for the competition. The semi-finals were transposed, causing some peoples’ brackets to have impossible choices.

Highlighted by predicted winner

fifa_picks %$%
  walk2(name, brackets, function(n, b){
    cat(paste0('### ', n, '\n'))
    print(b)
    cat('\n\n')
  })

Actual

Alejandra

Alex

Chris

Dylan

Effie

Kaushiki

Kritika

Ly-Sha

Matthias

Subhashini

UkJin

Highlighted by correctness

pick_check = fifa_picks %>%
  unnest(dat) %>%
  mutate(round = case_when(match <= 8 ~ 'Ro16',
                           match <= 12  ~ 'QF',
                           match <= 14 ~ 'SF',
                           match == 15 ~ 'F',
                           match == 16 ~ '3P')) %>%
  nest(dat = -c(name, title, round)) %>%
  dplyr::group_by(round) %>%
  dplyr::mutate(actual = dat[which(name == 'Actual')]) %>%
  mutate(dat = pmap(list(dat, actual, round), ~checkPicks(..1, ..2, ..3))) %>%
  select(-actual) %>%
  unnest(dat) %>%
  nest(dat = -c(name, title)) %>%
  mutate(brackets = map2(dat, title, ~addCorrectIncorrect(base.plot, bracket.df = .x, title = .y)))
pick_check %$%
  walk2(name, brackets, function(n, b){
    cat(paste0('### ', n, '\n'))
    print(b)
    cat('\n\n')
  })

Actual

Alejandra

Alex

Chris

Dylan

Effie

Kaushiki