---
title: "Untitled"
author: "RN7"
date: "1/14/2021"
output: html_document
---

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

# Packages

```{r message=FALSE}
# pacman::p_load()
library(rvest)
library(polite)
library(dplyr)
library(tidyr)
library(lubridate)
library(ggplot2)
library(ggtext)
library(scales)
library(grid)
library(gridExtra)
library(ggimage)
library(purrr)
library(stringr)
library(ggrepel)
library(ggforce)
library(patchwork)
library(glue)
library(forcats)
library(tibble)
library(kableExtra)
library(knitr)
library(extrafont)
loadfonts(quiet = TRUE)
```


# League table

```{r}
jleague_table_2020_cleaned <- readr::read_csv("https://raw.githubusercontent.com/Ryo-N7/soccer_ggplots/master/data/J-League_2020_review/jleague_table_2020_cleaned.csv")

jleague_table_2020_cleaned %>% 
  kable(format = "html", 
               caption = "J.League 2020 Table") %>% 
  kable_styling(full_width = FALSE,
                bootstrap_options = c("condensed", "responsive")) %>% 
  add_header_above(c(" ", "Result" = 4, "Goals" = 3,
                     "Expected Goals" = 3)) %>% 
  column_spec(1:2, bold = TRUE) %>% 
  row_spec(1, bold = TRUE, color = "white", background = "green") %>% 
  row_spec(2:3, bold = TRUE, color = "grey", background = "lightgreen") %>% 
  row_spec(4:15, bold = TRUE, color = "grey", background = "white") %>% 
  row_spec(16:18, color = "white", background = "red") %>% 
  add_footnote(label = "Data: FBref.com & Football-Lab.jp | Note: No relegation in 2020 season",
               notation = "none")
```



# Goals by Time Interval

```{r}
interval_goaltimes_all_df <- readr::read_csv("https://raw.githubusercontent.com/Ryo-N7/soccer_ggplots/master/data/J-League_2020_review/interval_goaltimes_all_df_jleague_2020.csv")
```




## plot function

```{r fig.width=20, fig.height=16}
# blue: #005AB5
# red: #DC3220

create_time_goals_plot <- function(df = interval_goaltimes_all_df, 
                                   team_name = team_name) {
  team_lab <- team_name
  team_name <- enquo(team_name)
  
  df_filtered <- interval_goaltimes_all_df %>% filter(team_name == !!team_name)
  
  ymaxlim <- max(df_filtered$goalFor)
  
  ymedmax <- max(df_filtered$mediangolsFor)
  
  if (ymedmax > ymaxlim) ymaxlim <- ymedmax
  
  shape_legend <- c("League Median Goals Scored" = 1, 
                    "League Median Goals Conceded" = 4) # 6 2
  
  
  interval_goaltimes <- ggplot(df_filtered,
                               aes(x = time)) +
    geom_segment(x = 0, xend = 10, y = 0, yend = 0, size = 0.75) +
    geom_col(aes(y = goalFor), width = 0.65, color = "#000000", fill = "#005AB5") +
    geom_col(aes(y = goalAG), width = 0.65, color = "#000000", fill = "#DC3220") +
    geom_point(aes(y = mediangolsFor, shape = "League Median Goals Scored"), 
               size = 8, stroke = 3.5) +
    geom_point(aes(y = mediangolsAgainst, shape = "League Median Goals Conceded"), 
               size = 8, stroke = 3.5) +
    scale_y_continuous(breaks = seq(-10, 20, by = 2),
                       limits = c(NA, ymaxlim + 4)) +
    scale_shape_manual(values = shape_legend, 
                       breaks = c("League Median Goals Scored", "League Median Goals Conceded"),
                       guide = guide_legend(
                         direction = "horizontal",
                         title = NULL
                       )) +
    labs(x = "Time Intervals", y = "Goals", # : Scored (+) | Conceded (-)
         title = glue::glue("Goals <b style ='color:#005AB5'>Scored</b> & <b style ='color:#DC3220'>Conceded</b> at 10 Minute Intervals"),
         subtitle = paste(team_lab, ("J.League 2020")),
         caption = "Graphic: Ryo Nakagawara | Twitter: @R_by_Ryo | Data: footystats.org") +
    theme_minimal() +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.minor.y = element_blank(),
          legend.position = c(0.5, 0.9),
          text = element_text(family = "Roboto Condensed"),
          plot.title = element_markdown(family = "Roboto Slab", hjust = 0.5, size = 40),
          plot.subtitle = element_markdown(family = "Roboto Slab", 
                                           hjust = 0.5, size = 35),
          plot.caption = element_text(size = 20),
          legend.text = element_text(size = 25),
          legend.spacing.x = unit(1.0, 'cm'),
          axis.title = element_text(size = 30),
          axis.text = element_text(size = 25))
  
  return(interval_goaltimes)
}
```

```{r fig.width=20, fig.height=16}
interval_goaltimes_Frontale <- create_time_goals_plot(df = interval_goaltimes_all_df, team_name = "Kawasaki Frontale")

interval_goaltimes_Frontale
```

```{r fig.width=20, fig.height=16}
interval_goaltimes_SPulse <- create_time_goals_plot(df = interval_goaltimes_all_df, team_name = "Shimizu SPulse")

interval_goaltimes_SPulse
```

Etc...


# Goals by Match Situations


```{r}
jleague_2020_situation_all_df <- readr::read_csv("https://raw.githubusercontent.com/Ryo-N7/soccer_ggplots/master/data/J-League_2020_review/jleague_2020_situation_all_df.csv")
```



## plot function

```{r fig.height = 22, fig.width=25}
create_situation_goals_plot <- function(df, team_name) {
  
  team_name_f <- enquo(team_name)
  situation_all_df <- df %>% 
    filter(team_name == !!team_name_f) %>% 
    mutate(situation = case_when(
      situation == "throughball" ~ "Through Ball",
      situation == "shortpass" ~ "Short Pass",
      situation == "longpass" ~ "Long Pass",
      situation == "setpiece_direct" ~ "Set Piece (Direct)", 
      situation == "setpiece" ~ "Set Piece",
      situation == "penalty" ~ "Penalty",
      situation == "other" ~ "Other", 
      situation == "looseball" ~ "Loose Ball",
      situation == "dribble" ~ "Dribble",
      situation == "cross" ~ "Cross",
      TRUE ~ NA_character_
    ))
  
  ymaxlim <- max(situation_all_df$goals_scored)
  
  ymedmax <- max(situation_all_df$goals_against)
  
  if (ymedmax > ymaxlim) ymaxlim <- ymedmax
  
  shape_legend <- c("League Median Goals Scored" = 21, 
                    "League Median Goals Conceded" = 4)
  
  team_color_score <- "#005AB5"
  team_color_against <- "#DC3220"
  
  ## separate dfs for labels
  topscore_sitch <- situation_all_df %>% 
    arrange(desc(goals_scored)) %>% 
    select(situation) %>% slice(1) %>% pull()
  
  topscore_sitch_f <- enquo(topscore_sitch)
  
  topdf <- situation_all_df %>% filter(situation == !!topscore_sitch_f)
  elsedf <- situation_all_df %>% filter(situation != !!topscore_sitch_f)
  
  match_sitch_concede_plot <- ggplot(situation_all_df) +
    ## background col
    geom_col(aes(x = 25, y = reorder(situation, goals_scored)), 
             width = 0.5, fill = "#DCDCDC") +
    ## foreground col
    ## against
    geom_col(aes(x = goals_against, y = reorder(situation, goals_scored), 
                 fill = team_color_against),
             width = 0.4) +
    ## for
    geom_col(aes(x = goals_scored, y = reorder(situation, goals_scored), 
                 fill = team_color_score),
             width = 0.25) +
    ## Text
    ## Top first row
    ggtext::geom_rich_text(
      data = topdf,
      aes(x = 12.5, y = situation,
          label = glue::glue("{topdf$goals_scored} <b style='color: #005AB5'>Scored</b> | {topdf$goals_against} <b style='color: #DC3220'>Conceded</b>")), 
      size = 10, fill = NA, label.color = NA,
      nudge_y = 0.35, family = "Roboto Condensed") +
    ## Top other rows
    ggtext::geom_rich_text(
      data = elsedf,
      aes(x = 12.5, y = situation,
          label = glue::glue("<b style='color: #005AB5'>{elsedf$goals_scored}</b> | <b style='color: #DC3220'>{elsedf$goals_against}</b>")), 
      size = 10, fill = NA, label.color = NA,
      nudge_y = 0.35, family = "Roboto Condensed") +
    ## Bottom first row
    ggtext::geom_rich_text(
      data = topdf,
      aes(x = 12.5, y = situation,
          label = glue::glue("<b style='color: #005AB5'>{topdf$prop_score * 100}%</b> of Team's Total Goals Scored | <b style='color: #DC3220'>{topdf$prop_against * 100}%</b> of Team's Total Goals Conceded")),
      size = 10, fill = NA, label.color = NA,
      nudge_y = -0.35, family = "Roboto Condensed") +
    ## Bottom other rows
    ggtext::geom_rich_text(
      data = elsedf,
      aes(x = 12.5, y = situation,
          label = glue::glue("<b style='color: #005AB5'>{elsedf$prop_score * 100}%</b> | <b style='color: #DC3220'>{elsedf$prop_against * 100}%</b>")),
      size = 10, fill = NA, label.color = NA, 
      nudge_y = -0.35, family = "Roboto Condensed") +
    ## League avg. markers
    geom_point(aes(x = avg_score, y = situation,
                   shape = "League Median Goals Scored"),
               stroke = 3.5, 
               size = 8,
               color = "#000000") + # color = "#005AB5", fill = "white"
    geom_point(aes(x = avg_against, y = situation,
                   shape = "League Median Goals Conceded"),
               stroke = 3.5, 
               size = 8,
               color = "#000000") + # color = "#DC3220", fill = "white"
    ## Scales
    scale_color_identity() +
    scale_fill_identity() +
    scale_x_continuous(limits = c(-0.5, 25)) +
    scale_y_discrete(expand = c(0.1, 0.1)) +
    scale_shape_manual(values = shape_legend, 
                       breaks = c("League Median Goals Scored", "League Median Goals Conceded"),
                       guide = guide_legend(
                         direction = "horizontal",
                         title = NULL
                       )) +
    #facet_wrap(~ team_label) +
    labs(title = glue::glue("Goals <b style='color: #005AB5'>Scored</b> & <b style='color: #DC3220'>Conceded</b> From Different Match Situations"),
         subtitle = glue::glue("{team_name} (J.League 2020)"),
         x = "Number of Goals <b style='color: #005AB5'>Scored</b> or <b style='color: #DC3220'>Conceded</b>", 
         y = NULL,
         caption = gt::md("**Graphic**: Ryo Nakagawara (**Twitter**: @R_by_Ryo) | **Data**: Football-Lab.jp")) +
    theme_minimal() +
    theme(text = element_text(family = "Roboto Condensed", color = "black"),
          plot.title = ggtext::element_markdown(size = 43, family = "Roboto Slab", 
                                                face = "bold", hjust = 0.5),
          plot.subtitle = element_text(size = 40, family = "Roboto Slab", 
                                       face = "bold", hjust = 0.5),
          plot.caption = element_markdown(size = 25),
          axis.title.x = element_markdown(size = 35, color = "black", family = "Roboto Slab"),
          axis.title.y = element_markdown(size = 35, color = "black", family = "Roboto Slab"),
          axis.text = element_markdown(size = 30, color = "black", family = "Roboto Slab"),
          legend.position = c(0.5, 0.99),
          legend.text = element_text(size = 30),
          legend.spacing.x = unit(1.0, 'cm'),
          panel.grid.minor.x = element_blank(),
          panel.grid.major.y = element_blank()) +
    ## Dividers
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 0.5, yend = 0.5, color = "#000000"),
                 size = 1.25) +
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 1.5, yend = 1.5, color = "#000000"),
                 size = 1.25) +
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 2.5, yend = 2.5, color = "#000000"),
                 size = 1.25) +
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 3.5, yend = 3.5, color = "#000000"),
                 size = 1.25) +
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 4.5, yend = 4.5, color = "#000000"),
                 size = 1.25) +
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 5.5, yend = 5.5, color = "#000000"),
                 size = 1.25) +
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 6.5, yend = 6.5, color = "#000000"),
                 size = 1.25) +
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 7.5, yend = 7.5, color = "#000000"),
                 size = 1.25) +
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 8.5, yend = 8.5, color = "#000000"),
                 size = 1.25) +
    geom_segment(aes(x = 6.25, xend = 18.75,
                     y = 9.5, yend = 9.5, color = "#000000"),
                 size = 1.25)
  
  return(match_sitch_concede_plot)
}
```


```{r fig.height = 22, fig.width=25}
create_situation_goals_plot(df = jleague_2020_situation_all_df, team_name = "FC Tokyo")
```


```{r fig.height = 22, fig.width=25}
create_situation_goals_plot(df = jleague_2020_situation_all_df, team_name = "Vegalta Sendai")
```


Etc.


# Team shooting



```{r}
jleague_2020_shooting_df <- readr::read_csv("https://raw.githubusercontent.com/Ryo-N7/soccer_ggplots/master/data/J-League_2020_review/jleague_2020_shooting_df.csv")
```


## plot

```{r}
shotsF_avg <- unique(jleague_2020_shooting_df$sh_avg)
  
shotsA_avg <- unique(jleague_2020_shooting_df$sh_against_avg)

bad_box <- data.frame(
  xmin = -Inf, xmax = shotsF_avg, 
  ymin = -Inf, ymax = shotsA_avg)

chance_creation_box <- data.frame(
  xmin = -Inf, xmax = shotsF_avg, 
  ymin = shotsA_avg, ymax = Inf)

midfield_progress_box <- data.frame(
  xmin = shotsF_avg, xmax = Inf, 
  ymin = -Inf, ymax = shotsA_avg)

dual_box <- data.frame(
  xmin = shotsF_avg, xmax = Inf, 
  ymin = shotsA_avg, ymax = Inf)
```


```{r fig.height=20, fig.width=24}
jleague_2020_shooting_plot <- jleague_2020_shooting_df %>% 
  ggplot(aes(x = `Sh/90`, y = `Sh/90_against`)) + 
  ## area fills
  geom_rect(data = chance_creation_box,
            aes(x = NULL, y = NULL,
                xmin = xmin, xmax = xmax,
                ymin = ymin, ymax = ymax),
            fill = "red", alpha = 0.1) +
  geom_rect(data = bad_box,
            aes(x = NULL, y = NULL,
                xmin = xmin, xmax = xmax,
                ymin = ymin, ymax = ymax),
            fill = "yellow", alpha = 0.1) +
  geom_rect(data = midfield_progress_box,
            aes(x = NULL, y = NULL,
                xmin = xmin, xmax = xmax,
                ymin = ymin, ymax = ymax),
            fill = "green", alpha = 0.2) +
  geom_rect(data = dual_box,
            aes(x = NULL, y = NULL,
                xmin = xmin, xmax = xmax,
                ymin = ymin, ymax = ymax),
            fill = "yellow", alpha = 0.1) +
  geom_point(size = 10) +
  geom_text_repel(data = jleague_2020_shooting_df %>% filter(!Squad %in% c("Kashima Antlers", "Kawasaki Frontale")),
                  aes(label = Squad),
                  size = 12, nudge_y = 0.25, force = 4,
                  min.segment.length = 0, segment.size = 1.25, fontface = "bold",
                  segment.color = "#000000", seed = 8, box.padding = unit(10, "mm"),
                  family = "Roboto Condensed") +
  geom_text_repel(data = jleague_2020_shooting_df %>% filter(Squad == "Kashima Antlers"),
                  aes(label = Squad),
                  size = 12, nudge_x = 0.25, force = 4,
                  min.segment.length = 0, segment.size = 1.25, color = "darkred", fontface = "bold",
                  segment.color = "#000000", seed = 8, box.padding = unit(10, "mm"),
                  family = "Roboto Condensed") +
  geom_text_repel(data = jleague_2020_shooting_df %>% filter(Squad == "Kawasaki Frontale"),
                  aes(label = Squad),
                  size = 12, nudge_y = 0.25, force = 4,
                  min.segment.length = 0, segment.size = 1.25, color = "#1EB8FF", fontface = "bold",
                  segment.color = "#000000", seed = 8, box.padding = unit(10, "mm"),
                  family = "Roboto Condensed") +
  ## median reference lines
  geom_hline(yintercept = shotsA_avg, color = "grey20", alpha = 0.7, size = 1.25) +
  geom_vline(xintercept = shotsF_avg, color = "grey20", alpha = 0.7, size = 1.25) +
  ## area labels
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    x = 8, y = 17, 
    hjust = 0, color = "red", size = 12,
    label = "Quiet Attack | Busy Defense") +
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    x = 8, y = 9, 
    hjust = 0, color = "#7f7f00", size = 12,
    label = "Quiet Attack | Quiet Defense") +
  annotate( # #7f7f00  #228B22 #CCCC00
    "text", family = "Roboto Condensed", fontface = "bold",
    x = 17, y = 17, 
    hjust = 0, color = "#7f7f00", size = 12,
    label = "Busy Attack | Busy Defense") +
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    x = 17, y = 9, 
    hjust = 0, color = "#228B22", size = 12,
    label = "Busy Attack | Quiet Defense") +
  ## League averages
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    x = 13.5, y = 16.5, 
    hjust = 0, color = "grey20", size = 12,
    label = glue("Average: {shotsF_avg} Shots Taken")) +
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    x = 17, y = 13.15, 
    hjust = 0, color = "grey20", size = 12,
    label = glue("Average: {shotsA_avg} Shots Conceded")) +
  scale_x_continuous(limit = c(8, 20),
                     labels = seq(8, 20, 2), 
                     breaks = seq(8, 20, 2)) +
  scale_y_reverse(limit = c(17, 9),
                  labels = seq(9, 17, 2), 
                  breaks = seq(9, 17, 2)) +
  labs(title = glue("
                    <b style='color: #1EB8FF'>Kawasaki Frontale</b> & <b style='color: darkred'>Kashima Antlers</b> Doing Very Well at Both Ends of the Pitch"),
       subtitle = "Shots Taken vs. Shots Conceded: J.League 2020",
       x = "Shots per 90",
       y = "Shots Against per 90",
       caption = "Graphic: Ryo Nakagawara | Twitter: @R_by_Ryo | Source: FBref.com") + 
  theme_minimal() +
  theme(text = element_text(size = 30, family = "Roboto Slab"),
        #plot.background = element_rect(fill = "grey"),
        plot.title = element_markdown(size = 40),
        plot.subtitle = element_text(size = 35),
        plot.caption = element_text(size = 30),
        axis.title = element_text(size = 35),
        axis.text = element_text(size =30),
        panel.grid.major = element_line(size = 2),
        panel.grid.minor = element_line(size = 2))

jleague_2020_shooting_plot
```


# Team xG


```{r}
xG_all_df <- readr::read_csv("https://raw.githubusercontent.com/Ryo-N7/soccer_ggplots/master/data/J-League_2020_review/team_xG_J-League-2020.csv")
```


## plot



```{r}
xGpg_avg <- unique(xG_all_df$xG_perGame_avg)
  
xGApg_avg <- unique(xG_all_df$xGA_perGame_avg)

bad_box <- data.frame(
  xmin = -Inf, xmax = xGpg_avg, 
  ymin = -Inf, ymax = xGApg_avg)

chance_creation_box <- data.frame(
  xmin = xGpg_avg, xmax = Inf, 
  ymin = -Inf, ymax = xGApg_avg)

midfield_progress_box <- data.frame(
  xmin = -Inf, xmax = xGpg_avg, 
  ymin = xGApg_avg, ymax = Inf)

dual_box <- data.frame(
  xmin = xGpg_avg, xmax = Inf, 
  ymin = xGApg_avg, ymax = Inf)
```



```{r fig.height=20, fig.width=24}
xG_xGA_j_league_2020_plot <- ggplot(xG_all_df,
       aes(x = xG_perGame, y = xGA_perGame)) +
  ## area fills
  geom_rect(data = chance_creation_box,
            aes(x = NULL, y = NULL,
                xmin = xmin, xmax = xmax,
                ymin = ymin, ymax = ymax),
            fill = "green", alpha = 0.1) +
  geom_rect(data = bad_box,
            aes(x = NULL, y = NULL,
                xmin = xmin, xmax = xmax,
                ymin = ymin, ymax = ymax),
            fill = "yellow", alpha = 0.1) +
  geom_rect(data = midfield_progress_box,
            aes(x = NULL, y = NULL,
                xmin = xmin, xmax = xmax,
                ymin = ymin, ymax = ymax),
            fill = "red", alpha = 0.2) +
  geom_rect(data = dual_box,
            aes(x = NULL, y = NULL,
                xmin = xmin, xmax = xmax,
                ymin = ymin, ymax = ymax),
            fill = "yellow", alpha = 0.1) +
  geom_hline(aes(yintercept = xGA_perGame_avg), color = "grey20", size = 2) + 
  geom_vline(aes(xintercept = xG_perGame_avg), color = "grey20", size = 2) + 
  geom_point(size = 10) +
  geom_text_repel(aes(label = team_name),
                  size = 12, nudge_y = 0.025, force = 4,
                  min.segment.length = 0, segment.size = 1.25, fontface = "bold",
                  segment.color = "#000000", seed = 8, box.padding = unit(10, "mm"),
                  family = "Roboto Condensed") +
  #geom_image(aes(image = img), size = 0.055) +
  ## area labels
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    y = 2, x = 0.83, 
    hjust = 0, color = "red", size = 12,
    label = "Bad Attack | Bad Defense") +
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    y = 2, x = 2, 
    hjust = 0, color = "#7f7f00", size = 12,
    label = "Good Attack | Bad Defense") +
  annotate( # #7f7f00  #228B22 #CCCC00
    "text", family = "Roboto Condensed", fontface = "bold",
    y = 0.75, x = 0.83, 
    hjust = 0, color = "#7f7f00", size = 12,
    label = "Bad Attack | Good Defense") +
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    y = 0.75, x = 2, 
    hjust = 0, color = "#228B22", size = 12,
    label = "Good Attack | Good Defense") +
    ## League averages
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    y = 0.85, x = 1.4, 
    hjust = 0, color = "grey20", size = 12,
    label = glue("Average: {round(xGpg_avg, 2)} xG per Game")) +
  annotate(
    "text", family = "Roboto Condensed", fontface = "bold",
    y = 1.45, x = 2.2, 
    hjust = 0, color = "grey20", size = 12,
    label = glue("Average: {round(xGApg_avg, 2)} xGA per Game")) +
  ## scales
  scale_x_continuous(limit = c(0.8, 2.6),
                     labels = seq(0.5, 2.6, 0.5),
                     breaks = seq(0.5, 2.6, 0.5)) +
  scale_y_reverse(limit = c(2, 0.75),
                  labels = seq(0, 2, 0.5),
                  breaks = seq(0, 2, 0.5)) +
  labs(
    title = "Quality of Shots Taken (xG) vs. Quality of Shots Conceded (xGA)",
    subtitle = "J.League 2020",
    x = "xG per Game",
    y = "xGA per Game",
    caption = "Graphic: Ryo Nakagawara | Twitter: @R_by_Ryo | Source: Football-Lab.jp"
  ) +
  theme_minimal() +
  theme(text = element_text(size = 30, family = "Roboto Slab"),
        plot.title = element_markdown(size = 45),
        plot.subtitle = element_text(size = 35),
        plot.caption = element_text(size = 30),
        axis.title = element_text(size = 35),
        axis.text = element_text(size =30),
        panel.grid.major = element_line(size = 2),
        panel.grid.minor = element_line(size = 2))

xG_xGA_j_league_2020_plot
```


## individual xG

```{r}
xGLeaders_df <- readr::read_csv("https://raw.githubusercontent.com/Ryo-N7/soccer_ggplots/master/data/J-League_2020_review/jleague_2020_individual_xG.csv")
```



```{r fig.height=20, fig.width=24}
xGleaders_plot <- xGLeaders_df %>% 
  ggplot(aes(x = npxG, y = npGoals)) + 
    imap(seq(0.5, 1.5, by = 0.25), function(slope, i) {

    # Calculate the position of the labels, such that
    # they run along the top horizontally, beyond a
    # maximum y value
    max_x <- 32
    max_y <- 32
    label_x <- ifelse(slope*max_x <= max_y, max_x, (max_y / slope))
    label_y <- slope*label_x

    # Only show the full label for the first annotation
    label <- str_glue("{slope * 100}% of xG")
    if (i == 5) {
      label <- str_glue("Scored {slope * 100}% of xG")
    }

    # Return the layers
    list(
      geom_segment(x = 0, y = 0, 
                   xend = max_x * 2, yend = slope* max_x * 2, 
                   linetype = "dashed", colour = "#e60000", size = 2),
      annotate(geom = "label", x = label_x, label_y,
               label = label, hjust = 1, size = 12, 
               fill = "#F0F0F0", colour = "#800000", 
               label.size = 0, 
               family = "Roboto Slab", fontface = "bold")
    )
  }) +
  geom_point(size = 8) + 
  geom_text_repel(#data = filter(xGLeaders_df, 
                                #npxG >= 10, npGoals >= 10),
                  aes(label = player_name_EN),
                  min.segment.length = 0, 
                  size = 12, 
                  force = 15, force_pull = 0.1,
                  family = "Roboto Slab", fontface = "bold",
                  color = "#000000",
                  segment.size = 2,
                  #point.padding = unit(30, "lines"), 
                  #label.padding = 0.5,
                  box.padding = 0.9
                  ) +
  coord_cartesian(xlim = c(0, 32), ylim = c(0, 32)) +
  scale_x_continuous() +
  scale_y_continuous() +
  labs(title = "Elite finishers of the J.League",
       subtitle = "Top 20 xG leaders in the 2020 season",
       caption = "Graphic: Ryo Nakagawara | Twitter: @R_by_Ryo | Source: Football-Lab.jp",
       x = "Non-Penalty xG",
       y = "Non-Penalty Goals") +
  theme_minimal() + 
  theme(plot.title = element_text(size = 55),
        plot.subtitle = element_text(size = 40),
        plot.caption = element_text(size = 30),
        text = element_text(size = 40, family = "Roboto Slab"),
        plot.background = element_rect(fill = "#F0F0F0"),
        panel.grid = element_line(color = "black"))

xGleaders_plot
```




