---
title: "Bundesliga"
author: "RN7"
date: "5/24/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# pkgs

```{r, message=FALSE, warning=FALSE}
pacman::p_load(tidyverse, polite, scales, ggimage, ggforce,
               rvest, glue, extrafont, ggrepel, magick)
loadfonts()
```

## add_logo

```{r}
add_logo <- function(plot_path, logo_path, logo_position, logo_scale = 10){

    # Requires magick R Package https://github.com/ropensci/magick

    # Useful error message for logo position
    if (!logo_position %in% c("top right", "top left", "bottom right", "bottom left")) {
        stop("Error Message: Uh oh! Logo Position not recognized\n  Try: logo_positon = 'top left', 'top right', 'bottom left', or 'bottom right'")
    }

    # read in raw images
    plot <- magick::image_read(plot_path)
    logo_raw <- magick::image_read(logo_path)

    # get dimensions of plot for scaling
    plot_height <- magick::image_info(plot)$height
    plot_width <- magick::image_info(plot)$width

    # default scale to 1/10th width of plot
    # Can change with logo_scale
    logo <- magick::image_scale(logo_raw, as.character(plot_width/logo_scale))

    # Get width of logo
    logo_width <- magick::image_info(logo)$width
    logo_height <- magick::image_info(logo)$height

    # Set position of logo
    # Position starts at 0,0 at top left
    # Using 0.01 for 1% - aesthetic padding

    if (logo_position == "top right") {
        x_pos = plot_width - logo_width - 0.01 * plot_width
        y_pos = 0.01 * plot_height
    } else if (logo_position == "top left") {
        x_pos = 0.01 * plot_width
        y_pos = 0.01 * plot_height
    } else if (logo_position == "bottom right") {
        x_pos = plot_width - logo_width - 0.01 * plot_width
        y_pos = plot_height - logo_height - 0.01 * plot_height
    } else if (logo_position == "bottom left") {
        x_pos = 0.01 * plot_width
        y_pos = plot_height - logo_height - 0.01 * plot_height
    }

    # Compose the actual overlay
    magick::image_composite(plot, logo, offset = paste0("+", x_pos, "+", y_pos))
}
```


# Bundesliga

## webscrape soccerway

```{r}
url <- "https://us.soccerway.com/national/germany/bundesliga/20182019/regular-season/r47657/"

session <- bow(url)

team_links <- scrape(session) %>% 
  html_nodes("#page_competition_1_block_competition_tables_7_block_competition_league_table_1_table .large-link a") %>% 
  html_attr("href")

team_links_df <- team_links %>% 
  enframe(name = NULL) %>% 
  separate(value, c(NA, NA, NA, "team_name", "team_num"), sep = "/") %>% 
  mutate(link = glue("
                     https://us.soccerway.com/teams/germany/{team_name}/{team_num}/squad/"),
         stat_link = glue("{link %>% str_replace('squad', 'statistics')}"))

# for each team link:

player_name_info <- function(session) {
  
  player_name_info <- scrape(session) %>% 
    html_nodes("#page_team_1_block_team_squad_3-table .name.large-link") %>% 
    html_text()
}

num_goals_info <- function(session) {

  num_goals_info <- scrape(session) %>% 
    html_nodes(".goals") %>% 
    html_text()
  
  num_goals_info_clean <- num_goals_info[-1]
}

num_assists_info <- function(session) {

  num_assists_info <- scrape(session) %>% 
    html_nodes(".assists") %>% 
    html_text()
  
  num_assists_info_clean <- num_assists_info[-1]
}

team_goals_info <- function(session) {
  team_goals_info <- scrape(session) %>% 
    html_nodes("tr.first:nth-child(6) > td:nth-child(2)") %>% 
    html_text()
}

# BIG FUNCTION
bundesliga_stats_info <- function(link, statlink) {
  
  session <- bow(link)
  session2 <- bow(statlink)
  
  player_name <- player_name_info(session = session)

  num_goals <- num_goals_info(session = session)

  num_assists <- num_assists_info(session = session)
  
  team_goals <- team_goals_info(session = session2)
  
  resultados <- list(player_name, num_goals, num_assists, team_goals)
  col_names <- c("name", "goals", "assists", "team_goals") 
  
  bundesliga_stats <- resultados %>% 
    reduce(cbind) %>% 
    as_tibble() %>% 
    set_names(col_names) 
  
}
```

### all at once

```{r}
# ALL 18 TEAMS AT ONCE, WILL TAKE A WHILE:
bundesliga_goal_contribution_df_ALL <- map2(.x = team_links_df$link,
                .y = team_links_df$stat_link,
                ~ bundesliga_stats_info(link = .x, statlink = .y))

bundesliga_goal_contribution_df <- bundesliga_goal_contribution_df_ALL %>% 
  set_names(team_links_df$team_name) %>% 
  bind_rows(.id = "team_name")

## save
saveRDS(bundesliga_goal_contribution_df, file = glue("{here::here()}/data/bundesliga_goal_contrib_df_soccerway.RDS"))
```

## clean

```{r}
bundesliga_goal_contribution_clean_df <- bundesliga_goal_contribution_df %>% 
  mutate_at(.vars = c("goals", "assists"), 
            ~str_replace(., "-", "0") %>% as.numeric) %>% 
  mutate(team = team_name %>% str_replace_all(., "-", " ") %>% str_to_title,
         total_goals = as.numeric(team_goals)) %>% 
  group_by(team) %>% 
  mutate(total_assists = sum(assists),
         goal_contrib = goals/total_goals,
         assist_contrib = assists/total_goals) %>% 
  ungroup() %>% 
  select(-team_name, -team_goals)

## save
saveRDS(bundesliga_goal_contribution_clean_df, 
        file = glue("{here::here()}/data/bundesliga_goal_contrib_clean_df.RDS"))
bundesliga_goal_contribution_clean_df <- readRDS(file = glue("{here::here()}/data/bundesliga_goal_contrib_clean_df.RDS"))
```

## plot

```{r fig.width = 10, fig.height = 8}  
## Description text
desc_hazard <- "Hazard FC: With 16 goals and 15 assists Eden Hazard has been involved in the most goals for a team this season."
desc_vardymurray <- "Scoring 37.5% and 37.1% of their team's goals, Jamie Vardy and Glen Murray have proven to be talismans for their team yet again!"
desc_fraser <- "Another fantastic season from Ryan Fraser with 7 goals and 14 assists (one behind league-leader Hazard)"

## PLOT!
bundesliga_goal_contribution_clean_df %>% 
  ggplot(aes(assist_contrib, goal_contrib)) +
  geom_point(data = bundesliga_goal_contribution_clean_df %>%
                    filter(goal_contrib < 0.225 | assist_contrib < 0.125),
             color = "grey20", size = 4, alpha = 0.2) +
  geom_point(data = bundesliga_goal_contribution_clean_df %>%
                    filter(goal_contrib > 0.225 | assist_contrib > 0.125),
             color = "red", size = 4) +
  geom_hline(yintercept = 0.225, color = "grey20", alpha = 0.4) +
  geom_vline(xintercept = 0.125, color = "grey20", alpha = 0.4) +
  geom_text_repel(data = bundesliga_goal_contribution_clean_df %>%
                    filter(goal_contrib > 0.225 | assist_contrib > 0.125),
                  aes(label = name, family = "Roboto Condensed", fontface = "bold"), 
                  seed = 15, size = 5, 
                  min.segment.length = 0, segment.color = "red",
                  point.padding = 0.5) +
  # geom_mark_circle(aes(filter = name == "E. Hazard", label = "Eden Hazard",
  #                       description = desc_hazard), 
  #                   label.family = "Roboto Condensed", label.fontsize = c(14, 10)) +
  # geom_mark_hull(aes(filter = name %in% c("G. Murray", "J. Vardy"), label = "Vardy & Murray",
  #                       description = desc_vardymurray),
  #                   label.buffer = unit(20, "mm"), label.fontsize = c(14, 10),
  #                   label.family = "Roboto Condensed") +
  # geom_mark_circle(aes(filter = name == "R. Fraser", label = "Ryan Fraser",
  #                       description = desc_fraser),
  #                   label.buffer = unit(9.8, "mm"), label.fontsize = c(14, 10),
  #                   label.family = "Roboto Condensed") +
  scale_x_continuous(labels = percent_format(accuracy = 1),
                     breaks = c(0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3),
                     limits = c(0, 0.225)) +
  scale_y_continuous(labels = percent_format(accuracy = 1),
                     breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5),
                     limits = c(0, 0.4)) +
  labs(title = "Goal Contribution Matrix: Bundesliga (2018-2019 Season)",
       subtitle = "Team Goal Involvement as Percentage of Total Club Goals and/or Assists.",
       caption = glue("
                      Data: soccerway.com
                      By: @R_by_Ryo"),
       x = "Percentage of Club Goals Assisted",
       y = "Percentage of Club Goals Scored") +
  theme_minimal() +
  theme(text = element_text(family = "Roboto Condensed"),
        title = element_text(size = 18),
        plot.subtitle = element_text(size = 16),
        plot.caption = element_text(size = 10),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        panel.grid.minor.x = element_blank()) -> bundesliga_goal_contribution_matrix

bundesliga_goal_contribution_matrix
```

## save

```{r}
ggsave(plot = bundesliga_goal_contribution_matrix, 
       "../Bundesliga 2018-2019/output/goal_contribution_matrix_plot_bundesliga.png",
       height = 9, width = 11)
```

```{r}
plot_logo <- add_logo(
  plot_path = "../Bundesliga 2018-2019/output/goal_contribution_matrix_plot_bundesliga.png",
  logo_path = "https://upload.wikimedia.org/wikipedia/en/d/df/Bundesliga_logo_%282017%29.svg",
  logo_position = "top right",
  logo_scale = 13)

plot_logo
```

```{r}
image_write(image = plot_logo, 
            "../Bundesliga 2018-2019/output/goal_contribution_matrix_plot_logo_bundesliga.png")
```
