---
title: "Untitled"
author: "RN7"
date: "June 20, 2018"
output: html_document
---

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

## Recreate & Animate Your Favorite World Cup Goals!




## example points


```{r echo=FALSE, fig.width=8, fig.height=5}
library(ggsoccer)
library(ggplot2)

point_data <- data.frame(x = c( 100, 83, 100, 83, 88.5, 100,  83, 83, 100, 100),
                         y = c(   0, 21,  21, 38, 50,    50,  62, 80,  80, 100),
                         label = c("100, 0", "83, 21", "100, 21", "83, 38", "88.5, 50", 
                                   "100, 50", "83, 62", "83, 80", "100, 80", "100, 100"))

field <- ggplot(point_data) +
  annotate_pitch() +
  theme_pitch(aspect_ratio = NULL) +
  coord_flip() +
  geom_point(aes(x = x, y = y), size = 1.5) +
  geom_text(aes(x= x, y = y,
                label = label),
            vjust = 1.5, color = "red")
ggsave(field, filename = "field.png", width = 8, height = 5)
```





## Gazinsky: First Goal!

```{r first goal, fig.height=6, fig.width=8}
library(ggplot2)
library(dplyr)
library(ggsoccer)
library(ggimage)
library(extrafont)
# loadfonts()

#                              2   1  
pass_data <- data.frame(x = c( 84, 82, 94),
                        y = c(  6, 32, 35),
                        x2 = c(77, 84, 83),
                        y2 = c(13, 8 , 32.5))

#                            corner kick + golovin cross
curve_data <- data.frame(x = c(100, 76),
                         y = c(0, 19),
                         x2 = c(94, 94),
                         y2 = c(35, 60))

# Gazinsky header
ball_data <- data.frame(x = c(94),
                        y = c(60),
                        x2 = c(99.2),
                        y2 = c(47.5))

# soccer ball image
goal_img <- data.frame(x = 100,
                       y = 47) %>% 
  mutate(image = "https://d30y9cdsu7xlg0.cloudfront.net/png/43563-200.png")

# golovin + zhirkov movement
movement_data <- data.frame(x = c(83, 98),
                           y = c(24.25, 2),
                           x2 = c(77, 88),
                           y2 = c(21, 6))

saudi_data <- data.frame(
  x = c(96.5),
  y = c(35),
  label = "M. Al-Breik"
)

g <- ggplot(pass_data) +
  annotate_pitch() +
  geom_segment(aes(x = x, y = y, xend = x2, yend = y2),
               arrow = arrow(length = unit(0.25, "cm"),
                             type = "closed")) +
  geom_segment(data = ball_data,
               aes(x = x, y = y, xend = x2, yend = y2), 
               linetype = "dashed", size = 0.85,
               color = "red") +
  geom_segment(data = movement_data,
               aes(x = x, y = y, xend = x2, yend = y2), 
               linetype = "dashed", size = 1.2,
               color = "black") +
  geom_curve(data = curve_data, 
             aes(x = x, y = y, xend = x2, yend = y2), 
             curvature = 0.25, 
             arrow = arrow(length = unit(0.25, "cm"),
                           type = "closed")) +
  geom_image(data = goal_img,
             aes(x = x, y = y,
                 image = image), 
             size = 0.035) +
  theme_pitch() + 
  theme(text = element_text(family = "Dusha V5")) +
  coord_flip(xlim = c(49, 101),
             ylim = c(-1, 101)) +
  ggtitle(label = "Russia (5) vs. (0) Saudi Arabia", 
          subtitle = "First goal, Yuri Gazinsky (12th Minute)") +
  labs(caption = "By Ryo Nakagawara (@R_by_Ryo)") +
  geom_label(aes(x = 94, y = 60, 
                 label = "Gazinsky"), 
             hjust = -0.1, color = "red", family = "Dusha V5") +
  geom_label(aes(x = 83, y = 23, 
                 label = "Golovin"), 
             hjust = -0.05, color = "red", family = "Dusha V5") +
  geom_label(aes(x = 75, y = 11, 
                 label = "Golovin"), 
             hjust = -0.1, color = "red", family = "Dusha V5") +
  geom_label(aes(x = 98, y = 0, 
                 label = "Zhirkov"), 
             vjust = -0.3, color = "red", family = "Dusha V5") +
  geom_label(aes(x = 84, y = 6, 
                 label = "Zhirkov"), 
             vjust = -0.3, color = "red", family = "Dusha V5") +
  geom_label(
    data = saudi_data,
    aes(x = x, y = y,
        label = label),
    color = "darkgreen", family = "Dusha V5") +
  annotate("text", x = 69, y = 65, family = "Dusha V5",
           label = "After a poor corner kick clearance\n from Saudi Arabia, Golovin picks up the loose ball, \n exchanges a give-and-go pass with Zhirkov\n before finding Gazinsky with a beautiful cross!")

ggsave(g, filename = "gazinsky_goal.png", height = 6, width = 8)
```



### Gazinsky animate


```{r complete gazinsky gganimate, fig.width=8, fig.height=6}
library(ggplot2)
library(dplyr)
library(ggsoccer)
library(ggimage)
library(extrafont)
library(gganimate)
# loadfonts()

# data
pass_data <- data.frame(
  x = c(100, 94, 82, 82.5,  84, 76.5, 75.5, 94, 99.2),       # pass balls
  y = c(0,   35, 31, 22,     8, 13, 19, 60, 47.5),
  time = c(1, 2, 3, 4, 5, 6, 7, 8, 9))

golovin_movement <- data.frame(
  x = c(78, 80, 80, 80, 75.5, 74.5, 73.5, 73, 73),   #75, 74, 73
  y = c(30, 30, 27, 25,   10,    9, 15, 15, 15),
  label = "Golovin",
  time = c(1, 2, 3,  4,  5,  6,  7,  8,  9)
)

zhirkov_movement <- data.frame(
  x = c(98, 90, 84, 84, 84, 84, 84, 84, 84),
  y = c( 0,  2,  2,  2,  2,  2,  2,  2,  2),
  label = "Zhirkov",
  time = c(1, 2, 3, 4, 5, 6, 7, 8, 9)
)

gazinsky_movement <- data.frame(
  x = c(92),
  y = c(66.8),
  label = "Gazinsky",
  time = c(6, 7, 8, 9)
)

# segment golovin should only appear 4-5?
# segment zhirkov should only appear 1-3?
segment_data <- data.frame(
  x = c(77.5, 98),
  y = c(22, 2),
  xend = c(75, 84),
  yend = c(15, 3),
  linetype = c("dashed", "dashed"),
  color = c("black", "black"),
  size = c(1.2, 1.25)
)

saudi_data <- data.frame(
  x = c(95),
  y = c(35),
  label = "M. Al-Breik"
)

### soccer ball
ball_data <- tribble(
  ~x,  ~y, ~time,
  100,   0,   1,
  94,   35,   2,
  82,   31,   3,
  82.5, 25,   4,
  84,    6,   5, 
  77,   13,   6,
  76,   19,   7,
  94,   60,   8,
  99.2, 47.5, 9,
  
) 


gazin_ani <- ggplot(pass_data) +
  annotate_pitch() +
  theme_pitch() +
  coord_flip(xlim = c(49, 101),
             ylim = c(-1, 101)) +
  geom_segment(data = segment_data, 
               aes(x = x, y = y, 
                   xend = xend, yend = yend),
               size = segment_data$size,
               color = segment_data$color,
               linetype = c("dashed", "dashed")) +
  geom_label(
    data = saudi_data,
    aes(x = x, y = y,
        label = label),
    color = "darkgreen") +
  geom_label(data = zhirkov_movement,
    aes(x = x, y = y,
        frame = time,
        label = label),
    color = "red") +
  geom_label(data = golovin_movement,
    aes(x = x, y = y,
        frame = time,
        label = label),
    color = "red") +
  geom_label(
    data = gazinsky_movement,
    aes(x = x, y = y,
        label = label),
    color = "red") +
  ggimage::geom_emoji(
    data = ball_data,
    aes(x = x, y = y, frame = time),   
    image = "26bd", size = 0.035) +
  ggtitle(label = "Russia (5) vs. (0) Saudi Arabia", 
          subtitle = "First goal, Yuri Gazinsky (12th Minute)") +
  labs(caption = "By Ryo Nakagawara (@R_by_Ryo)") +
  annotate("text", x = 69, y = 65, family = "Dusha V5",
           label = "After a poor corner kick clearance\n from Saudi Arabia, Golovin picks up the loose ball, \n exchanges a give-and-go pass with Zhirkov\n before finding Gazinsky with a beautiful cross!") +
  theme(text = element_text(family = "Dusha V5"))

gganimate(gazin_ani, 
          width = 8, height = 6, 
          title_frame = FALSE,  
          "gazin_ggani_final.gif")


```




```{r complete gazinskiy tweenr, fig.width=8, fig.height=6}
library(ggplot2)
library(dplyr)
library(ggsoccer)
library(ggimage)
library(extrafont)
library(gganimate)
library(tweenr)
library(purrr)
# loadfonts()

# data
pass_data <- data.frame(
  x = c(100, 94, 82, 82.5,  84, 76.5, 75.5, 94, 99.2),       # pass balls
  y = c(0,   35, 31, 22,     8, 13, 19, 60, 47.5),
  time = c(1, 2, 3, 4, 5, 6, 7, 8, 9))

golovin_movement <- data.frame(
  x = c(78, 80, 80, 80, 75.5, 74.5, 73.5, 73, 73),   #75, 74, 73
  y = c(30, 30, 27, 25,   10,    9, 15, 15, 15),
  label = "Golovin",
  time = c(1, 2, 3,  4,  5,  6,  7,  8,  9)
)

zhirkov_movement <- data.frame(
  x = c(98, 90, 84, 84, 84, 84, 84, 84, 84),
  y = c( 0,  2,  2,  2,  2,  2,  2,  2,  2),
  label = "Zhirkov",
  time = c(1, 2, 3, 4, 5, 6, 7, 8, 9)
)

gazinsky_movement <- data.frame(
  x = c(92),
  y = c(66.8),
  label = "Gazinsky",
  time = c(6, 7, 8, 9)
)

# saudi defender
saudi_data <- data.frame(
  x = c(95),
  y = c(35),
  label = "M. Al-Breik"
)

### soccer ball
ball_data <- tribble(
  ~x,  ~y, ~time,
  100,   0,   1,
  94,   35,   2,
  82,   31,   3,
  82.5, 25,   4,
  84,    6,   5, 
  77,   13,   6,
  76,   19,   7,
  94,   60,   8,
  99.2, 47.5, 9,
  
) 

### ball movement
b_list <- ball_data %>% pmap(data.frame)

ball_tween <- b_list %>% 
  tween_states(tweenlength = 0.5, statelength = 0.00000001, ease = "linear", nframes = 75)

### Golovin

golovin_movement_list <- golovin_movement %>% pmap(data.frame)
  
golovin_tween <- golovin_movement_list %>% 
  tween_states(tweenlength = 0.5, statelength = 0.00000001, ease = "linear", nframes = 75)

golovin_tween <- golovin_tween %>% mutate(label = "Golovin")

### Zhirkov
zhirkov_movement_list <- zhirkov_movement %>% pmap(data.frame)
  
zhirkov_tween <- zhirkov_movement_list %>% 
  tween_states(tweenlength = 0.5, statelength = 0.00000001, ease = "linear", nframes = 75)

zhirkov_tween <- zhirkov_tween %>% mutate(label = "Zhirkov")

### PLOT

gazin_move <- ggplot(pass_data) +
  annotate_pitch() +
  theme_pitch() +
  coord_flip(xlim = c(49, 101),
             ylim = c(-1, 101)) +
  geom_label(
    data = saudi_data,
    aes(x = x, y = y,
        label = label),
    color = "darkgreen") +
  geom_label(data = zhirkov_tween,
    aes(x = x, y = y,
        frame = .frame,
        label = label),
    color = "red") +
  geom_label(data = golovin_tween,
    aes(x = x, y = y,
        frame = .frame,
        label = label),
    color = "red") +
  geom_label(
    data = gazinsky_movement,
    aes(x = x, y = y,
        label = label),
    color = "red") +
  ggimage::geom_emoji(
    data = ball_tween,
    aes(x = x, y = y, frame = .frame),   
    image = "26bd", size = 0.035) +
  ggtitle(label = "Russia (5) vs. (0) Saudi Arabia", 
          subtitle = "First goal, Yuri Gazinsky (12th Minute)") +
  labs(caption = "By Ryo Nakagawara (@R_by_Ryo)") +
  annotate("text", x = 69, y = 65, family = "Dusha V5",
           label = "After a poor corner kick clearance\n from Saudi Arabia, Golovin picks up the loose ball, \n exchanges a give-and-go pass with Zhirkov\n before finding Gazinsky with a beautiful cross!") +
  theme(text = element_text(family = "Dusha V5"))

gganimate(gazin_move, 
          width = 8, height = 6, 
          title_frame = FALSE, interval = 0.25, 
          "gazinsky_goal_final.gif")

gganimate(gazin_move, 
          width = 8, height = 6, 
          title_frame = FALSE, interval = 0.25, 
          "gazin_move.mp4")

```









## Cristiano Hat Trick

NOTE: you need to play around with the margins in `theme()` and the `width` or `height` arguments in `ggsave()` to remove the extraneous white space that appears.

```{r cristiano hat trick, fig.height=5, fig.width=7}
library(ggplot2)
library(ggsoccer)
library(extrafont)
library(emoGG)
library(ggimage)
# loadfonts()
# Official WC 2018 Font: "Dusha"
# http://fifa2018wiki.com/fifa-2018-font-typeface-download-dusha-font-ttf/509/

emoji_search("soccer")  # "26bd"

goals_data <- data.frame(x = c(88, 80, 71),
                         y = c(50, 48, 54),
                         label = c(1, 2, 3))

curve_data <- data.frame(x = c(88, 71), y = c(50, 54),
                         xend = c(100, 100), yend = c(54, 54))

annotation_data <- data.frame(
  hjust = c(0.5, 0.5, 0.5, 0, 0, 0),
  label = c("Portugal             (3) vs. Spain             (3)",
            "Cristiano's Hattrick (4', 44', 88')",
            "by Ryo Nakagawara (@R_by_Ryo)",
            "1. Fouled by Nacho in the box,\nCristiano confidently strokes the ball\ninto the right corner from the spot.",
            "2. Guedes lays it off to Cristiano whose\nstrong shot is uncharacteristically\nfumbled by De Gea into the net.",
            "In the final minutes of the game,\nCristiano wins a freekick against Pique\nand curls it beautifully over the wall."),
  x = c(110, 105, 53, 76, 66, 66), 
  y = c(30, 20, 85, 5, 5, 55)
)

flag_data <- data.frame(
  image = c("PT", "ES"),
  x = c(110, 110),
  y = c(19.1, 51.1)
)

# PLOT

cr <- ggplot(goals_data) +
  annotate_pitch() +
  theme_pitch() +
  theme(text = element_text(family = "Dusha V5"),
        legend.position = "none") +
  coord_flip(xlim = c(55, 112),
             ylim = c(-1, 101)) +
  geom_segment(x = 80, y = 48, 
               xend = 97, yend = 48) +  # 2nd 
  geom_segment(x = 97, y = 48, 
               xend = 100, yend = 45.5,
               arrow = arrow(length = unit(0.25, "cm"),
                             type = "closed")) +        # degea fumble
  geom_curve(data = curve_data,
             aes(x = x, y = y, 
                 xend = xend, yend = yend),     # FREEKICK
             curvature = 0.3, 
             arrow = arrow(length = unit(0.25, "cm"), type = "closed")) +
  geom_text(data = annotation_data,
            family = "Dusha V5", 
            aes(x = x, y = y,
                hjust = hjust, label = label),
            size = c(6.5, 4.5, 3, 3.5, 3.5, 3.5)) +
  geom_flag(data = flag_data,
            aes(x = x, y = y,
                image = image), size = c(0.08, 0.08)) +       # Portugal + Spain Flag
  ggimage::geom_emoji(aes(x = 105, 
                 y = c(45, 50, 55)),
             image = "26bd", size = 0.035) +
  geom_point(aes(x = x, y = y), 
             shape = 21, size = 7, color = "black", fill = "white") +
  geom_text(aes(x = x, y = y, label = label, family = "Dusha V5"))

ggsave(cr, filename = "cr_hattrick.png", height = 5, width = 7)
```


## Osako's Winner against Colombia!


```{r osako winner, fig.height = 5, fig.width = 7}
library(ggplot2)
library(dplyr)
library(ggsoccer)
library(extrafont)
library(ggimage)


cornerkick_data <- data.frame(x = 99, y = 0.3,
                              x2 = 94, y2 = 47)

osako_gol <- data.frame(x = 94, y = 49,
                        x2 = 100, y2 = 55.5)

player_label <- data.frame(x = c(92, 99), 
                           y = c(49, 2))

wc_logo <- data.frame(x = 107,
                       y = 85) %>% 
  mutate(image = "https://upload.wikimedia.org/wikipedia/en/thumb/6/67/2018_FIFA_World_Cup.svg/1200px-2018_FIFA_World_Cup.svg.png")


g <- ggplot(osako_gol) +
  annotate_pitch() +
  theme_pitch() +
  theme(text = element_text(family = "Dusha V5")) +
  coord_flip(xlim = c(55, 112),
             ylim = c(-1, 101)) +
  geom_curve(data = cornerkick_data,
             aes(x = x, y = y, xend = x2, yend = y2),
             curvature = -0.15, 
             arrow = arrow(length = unit(0.25, "cm"),
                             type = "closed")) +
  geom_segment(aes(x = x, y = y, xend = x2, yend = y2),
               arrow = arrow(length = unit(0.25, "cm"),
                             type = "closed")) +
  geom_label(data = player_label, 
             aes(x = x, y = y),
             label = c("Osako", "Honda"), family = "Dusha V5") +
  geom_point(aes(x = 98, y = 50), size = 3, color = "green") +
  geom_text(aes(x = 99.7, y = 50), size = 5, label = "???", family = "Dusha V5") +
  annotate(geom = "text", family = "Dusha V5", 
           hjust = c(0.5, 0.5, 0.5, 0.5, 0.5),
           size = c(6.5, 4.5, 4, 3.5, 3),
           label = c("Japan             (2) vs. Colombia             (1)",
                     "Kagawa (PEN 6'), Quintero (39'), Osako (73')",
                     "Japan press their man advantage, substitute Honda\ndelivers a delicious corner kick for Osako to (somehow) tower over\nColombia's defense and flick a header into the far corner!",
                     "Bonus: Ospina looking confused and\ndoing a lil' two-step-or-god-knows-what.",
                     "by Ryo Nakagawara (@R_by_Ryo)"),
           x = c(110, 105, 70, 92, 53), 
           y = c(30, 30, 45, 81, 85)) +
  ggimage::geom_flag(aes(image = "JP"),       # Japan Flag
            x = 110, y = 13, size = 0.08) +
  ggimage::geom_flag(aes(image = "CO"),       # Colombia Flag
            x = 110, y = 53, size = 0.08) +
  ggimage::geom_emoji(aes(x = 95, 
                          y = 50),
             image = "26bd", size = 0.035) +
  geom_image(data = wc_logo,
             aes(x = x, y = y,
                 image = image), size = 0.17) +
  theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
  
ggsave(g, filename = "osako_winner.png", height = 5, width = 7)
```

## Animated version:

```{r osako anim}
library(ggplot2)
library(dplyr)
library(ggsoccer)
library(extrafont)
library(emoGG)
library(ggimage)
library(gganimate)


ball_data <- data.frame(x = c(99, 94, 100),
                        y = c(0.3, 47, 55.5),
                        time = c(1, 2, 3))

player_label <- data.frame(x = c(92, 97), 
                           y = c(48, 0))

wc_logo <- data.frame(x = 107,
                       y = 85) %>% 
  mutate(image = "https://upload.wikimedia.org/wikipedia/en/thumb/6/67/2018_FIFA_World_Cup.svg/1200px-2018_FIFA_World_Cup.svg.png")


g <- ggplot(ball_data) +
  annotate_pitch() +
  theme_pitch() +
  theme(text = element_text(family = "Dusha V5")) +
  coord_flip(xlim = c(55, 112),
             ylim = c(-1, 101)) +
  geom_label(data = player_label, 
             aes(x = x, y = y),
             label = c("Osako", "Honda"), family = "Dusha V5") +
  geom_point(aes(x = 98, y = 50), size = 3, color = "green") +
  geom_text(aes(x = 99.7, y = 50), size = 5, label = "???", family = "Dusha V5") +
  annotate(geom = "text", family = "Dusha V5", 
           hjust = c(0.5, 0.5, 0.5, 0.5, 0.5),
           size = c(6.5, 4.5, 4, 3.5, 3),
           label = c("Japan             (2) vs. Colombia             (1)",
                     "Kagawa (PEN 6'), Quintero (39'), Osako (73')",
                     "Japan press their man advantage, substitute Honda\ndelivers a delicious corner kick for Osako to (somehow) tower over\nColombia's defense and flick a header into the far corner!",
                     "Bonus: Ospina looking confused and\ndoing a lil' two-step-or-god-knows-what.",
                     "by Ryo Nakagawara (@R_by_Ryo)"),
           x = c(110, 105, 70, 92, 53), 
           y = c(30, 30, 45, 81, 85)) +
  ggimage::geom_flag(aes(image = "JP"),       # Japan Flag
            x = 110, y = 13, size = 0.08) +
  ggimage::geom_flag(aes(image = "CO"),       # Colombia Flag
            x = 110, y = 53, size = 0.08) +
  ggimage::geom_emoji(aes(x = x, 
                          y = y,
                          frame = time),
             image = "26bd", size = 0.035) +
  geom_image(data = wc_logo,
             aes(x = x, y = y,
                 image = image), size = 0.17) +
  theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

g

gganimate(g, "osako_ani.gif") 

```

```{r osako tween, fig.height = 5, fig.width = 7}
# TWEEN
library(ggplot2)
library(dplyr)
library(ggsoccer)
library(extrafont)
library(emoGG)
library(ggimage)
library(gganimate)
library(purrr)
library(tweenr)

player_label <- data.frame(x = c(92, 97), 
                           y = c(48, 0))

wc_logo <- data.frame(x = 107,
                       y = 85) %>% 
  mutate(image = "https://upload.wikimedia.org/wikipedia/en/thumb/6/67/2018_FIFA_World_Cup.svg/1200px-2018_FIFA_World_Cup.svg.png")

# tweenr the ball movement data
ball_data <- data.frame(x = c(99, 94, 100),
                        y = c(0.3, 47, 55.5))

ball_list <- ball_data %>% pmap(data.frame)
  
osako_tween <- ball_list %>% 
  tween_states(tweenlength = 1.5, statelength = 0.01, ease = "quadratic-out", nframes = 50)

g2 <- ggplot(osako_tween) +
  annotate_pitch() +
  theme_pitch() +
  theme(text = element_text(family = "Dusha V5")) +
  coord_flip(xlim = c(55, 112),
             ylim = c(-1, 101)) +
  geom_label(data = player_label, 
             aes(x = x, y = y),
             label = c("Osako", "Honda"), family = "Dusha V5") +
  geom_point(aes(x = 98, y = 50), size = 3, color = "green") +
  annotate(geom = "text", family = "Dusha V5", 
           hjust = c(0.5, 0.5, 0.5, 0.5),
           size = c(6.5, 4.5, 5, 3),
           label = c("Japan             (2) vs. Colombia             (1)",
                     "Kagawa (PEN 6'), Quintero (39'), Osako (73')",
                     "Japan press their man advantage, substitute Honda\ndelivers a delicious corner kick for Osako to (somehow) tower over\nColombia's defense and flick a header into the far corner!",
                     "by Ryo Nakagawara (@R_by_Ryo)"),
           x = c(110, 105, 70, 53), 
           y = c(30, 30, 47, 85)) +
  ggimage::geom_emoji(aes(x = x, 
                          y = y,
                          frame = .frame),
             image = "26bd", size = 0.035) +
  ggimage::geom_flag(aes(image = "JP"),       # Japan Flag
            x = 110, y = 13, size = 0.08) +
  ggimage::geom_flag(aes(image = "CO"),       # Colombia Flag
            x = 110, y = 53, size = 0.08) +
  geom_image(data = wc_logo,
             aes(x = x, y = y,
                 image = image), size = 0.17) +
  theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

g2

gganimate(g2, 
          ani.width = 800, ani.height = 500, 
          interval = 0.5,
          "osako_tween.gif") 

gganimate(g2, title_frame = FALSE,
          width = 700, height = 500, 
          interval = 0.01,
          "osako_tween_final.gif") 

```

## Japan's Offside Trap!

```{r offside data, fig.height=6, fig.width=4}
library(ggplot2)
library(dplyr)
library(ggsoccer)
library(ggimage)
library(extrafont)
library(gganimate)
library(tweenr)
library(purrr)
library(countrycode)
# library(StatsBombR)
# loadfonts()

# flags
flag_data <- data.frame(
  x = c( 48, 87),
  y = c(107, 107),
  team = c("japan", "senegal")
  ) %>% 
  mutate(
    image = team %>% 
           countrycode(., origin = "country.name", destination = "iso2c")
  ) %>% 
  select(-team)

# PLAYERS
# JAPAN: x, y (blue)     Senegal: x2, y2  (lightgreen)
# push 2, 3 frames up above box >>> add 5, 6 as frames??
trap_data <- data.frame(
  
  time = c(1, 2, 3, 4, 5),
  
  # ball trajectory
  x = c(70, 70, 70, 87, 95),       # pass balls
  y = c(85, 85, 85, 52, 33),
  
  # offside bar
  #xo =    c(83, 81.2, 79, 77.5, 70),
  xoend = c(83.8, 81.8, 79, 78.5, 71),
  
  yo =    c( 5,  5,  5,  5, 5),
  yoend = c(95, 95, 95, 95, 95),
  
  # players: japan
  jx  = c(83, 81, 77, 75, 70),
  jy  = c(rep(65, 5)),
  
  jx2 = c(83, 81.8, 78.5, 77, 70),
  jy2 = c(rep(60.5, 5)),
  
  jx3 = c(83, 81, 76.5, 75, 71),
  jy3 = c(rep(55, 5)),
  
  jx4 = c(83, 81.2, 76.3, 75, 70),
  jy4 = c(rep(52, 5)),
  
  jx5 = c(82.8, 81, 77, 74, 70),
  jy5 = c(rep(49, 5)),
  
  jx6 = c(83, 81.8, 77, 74, 70),
  jy6 = c(rep(45, 5)),

  jx7 = c(83.8, 81, 79, 77.5, 70),
  jy7 = c(rep(40, 5)),
  
  # players: senegal
  sx = c(83, 84, 84, 84, 84),
  sy = c(rep(33, 5)),
  
  sx2 = c(83, 85, 87, 92, 95),
  sy2 = c(38, 37, 35, 34, 33),
  
  sx3 = c(83, 84, 84, 83, 83),
  sy3 = c(rep(41, 5)),
  
  sx4 = c(83, 84, 83, 78, 78),
  sy4 = c(rep(45, 5)),
  
  sx5 = c(83, 84, 87, 88, 89),
  sy5 = c(rep(52, 5)),
  
  sx6 = c(83, 85, 84, 84, 83),
  sy6 = c(rep(69, 5))
)

# fix focus field issue with coord_fixed() + aspect_ratio = NULL in theme_pitch()
g <- ggplot(trap_data) +
  annotate_pitch() +
  theme_pitch(aspect_ratio = NULL) +
  coord_fixed(xlim = c(30, 101),
       ylim = c(-5, 131)) +
  # offside line
  geom_segment(aes(x = xoend, y = yo, 
                   xend = xoend, yend = yoend,
                   frame = time), 
               color = "black", size = 1.3) +
  # start at 83      just use geom_segment instead
  # japan
  geom_point(aes(x = jx, y = jy, frame = time), size = 4, color = "blue") +
  geom_point(aes(x = jx2, y = jy2, frame = time), size = 4, color = "blue") +
  geom_point(aes(x = jx3, y = jy3, frame = time), size = 4, color = "blue") +
  geom_point(aes(x = jx4, y = jy4, frame = time), size = 4, color = "blue") +
  geom_point(aes(x = jx5, y = jy5, frame = time), size = 4, color = "blue") +
  geom_point(aes(x = jx6, y = jy6, frame = time), size = 4, color = "blue") +
  geom_point(aes(x = jx7, y = jy7, frame = time), size = 4, color = "blue") +
  # senegal
  geom_point(aes(x = sx, y = sy, frame = time), size = 4, color = "green") +
  geom_point(aes(x = sx2, y = sy2, frame = time), size = 4, color = "green") +
  geom_point(aes(x = sx3, y = sy3, frame = time), size = 4, color = "green") +
  geom_point(aes(x = sx4, y = sy4, frame = time), size = 4, color = "green") +
  geom_point(aes(x = sx5, y = sy5, frame = time), size = 4, color = "green") +
  geom_point(aes(x = sx6, y = sy6, frame = time), size = 4, color = "green") +
  
  # free kick spot (reference)
  geom_point(aes(x = 70, y = 85), color = "blue", size = 1.2) +
  annotate(geom = "text", family = "Dusha V5", 
           hjust = c(0, 0, 0, 0.5),
           size = c(4.5, 3, 5.5, 3),
           label = c("Japan             (2) vs. Senegal             (2)",
                     "Mane (11'), Inui (33'), Wague (71'), Honda (78')",
                     "The Perfect Offside Trap",
                     "by Ryo Nakagawara\n(@R_by_Ryo)"),
           x = c(30, 30, 30, 94), 
           y = c(117, 108, 125, -3)) +
  ggimage::geom_flag(data = flag_data,
                     aes(x = x, y = y,
                         image = image),       
                     size = c(0.08, 0.08)) +
  ggimage::geom_emoji(aes(x = x, y = y, 
                          frame = time),
                      image = "26bd", size = 0.035)

g


```

```{r offside gganimate, fig.height=6, fig.width=4}
# vline for offside line
# x1 Ja, x2 Sen
# sligh twiggle before kick? 
# goalkeeper position

gganimate(g, "g_ani.gif")

```


```{r tweenr offside final, fig.height=10, fig.width=8}
library(ggplot2)
library(dplyr)
library(ggsoccer)
library(ggimage)
library(extrafont)
library(gganimate)
library(tweenr)
library(purrr)
library(countrycode)
# library(StatsBombR)
# loadfonts()

# PLAYERS
# JAPAN: x, y (blue)     Senegal: x2, y2  (lightgreen)
trap_data <- data.frame(
  
  time = c(1, 2, 3, 4, 5),
  
  # ball trajectory
  x = c(70, 70, 70, 87, 95),       # pass balls
  y = c(85, 85, 85, 52, 33),
  
  # offside bar
  #xo =    c(83, 81.2, 79, 77.5, 70),
  xoend = c(83.8, 81.8, 79, 78.5, 71),
  
  yo =    c( 5,  5,  5,  5, 5),
  yoend = c(95, 95, 95, 95, 95),
  
  # players: japan
  jx  = c(83, 81, 77, 75, 70),
  jy  = c(rep(65, 5)),
  
  jx2 = c(83, 81.8, 78.5, 77, 70),
  jy2 = c(rep(60.5, 5)),
  
  jx3 = c(83, 81, 76.5, 75, 71),
  jy3 = c(rep(55, 5)),
  
  jx4 = c(83, 81.2, 76.3, 75, 70),
  jy4 = c(rep(52, 5)),
  
  jx5 = c(82.8, 81, 77, 74, 70),
  jy5 = c(rep(49, 5)),
  
  jx6 = c(83, 81.8, 77, 74, 70),
  jy6 = c(rep(45, 5)),

  jx7 = c(83.8, 81, 79, 77.5, 70),
  jy7 = c(rep(40, 5)),
  
  # players: senegal
  sx = c(83, 84, 84, 84, 84),
  sy = c(rep(33, 5)),
  
  sx2 = c(83, 85, 87, 92, 95),
  sy2 = c(38, 37, 35, 34, 33),
  
  sx3 = c(83, 84, 84, 83, 83),
  sy3 = c(rep(41, 5)),
  
  sx4 = c(83, 84, 83, 78, 78),
  sy4 = c(rep(45, 5)),
  
  sx5 = c(83, 84, 87, 88, 89),
  sy5 = c(rep(52, 5)),
  
  sx6 = c(83, 85, 84, 84, 83),
  sy6 = c(rep(69, 5))
)


# flags
flag_data <- data.frame(
  x = c( 42, 72),
  y = c(107, 107),
  team = c("japan", "senegal")
  ) %>% 
  mutate(
    image = team %>% 
           countrycode(., origin = "country.name", destination = "iso2c")
  ) %>% 
  select(-team)

# extra players:
goalkeeper_data <- data.frame(
  
  x = c(98),
  y = c(50)
  
)

senegal_data <- data.frame(
  
  x = c(55, 55, 68.5),
  y = c(50, 60, 87)
  
)



# create list of dfs
offside_list <- trap_data %>% pmap(data.frame)
  
# tweenr
offside_tween <- offside_list %>% 
  tween_states(tweenlength = 0.5, statelength = 0.00000001, ease = "linear", nframes = 50)

# PLOT

g2 <- ggplot(offside_tween) +
  annotate_pitch() +
  theme_pitch(aspect_ratio = NULL) +
  coord_fixed(xlim = c(30, 101),
              ylim = c(-5, 117)) +
  # offside line
  geom_segment(aes(x = xoend, y = yo, 
                   xend = xoend, yend = yoend,
                   frame = .frame), 
               color = "black", size = 1.3) +
  # start at 83      just use geom_segment instead
  # japan
  geom_point(aes(x = jx, y = jy, frame = .frame), size = 4, color = "blue") +
  geom_point(aes(x = jx2, y = jy2, frame = .frame), size = 4, color = "blue") +
  geom_point(aes(x = jx3, y = jy3, frame = .frame), size = 4, color = "blue") +
  geom_point(aes(x = jx4, y = jy4, frame = .frame), size = 4, color = "blue") +
  geom_point(aes(x = jx5, y = jy5, frame = .frame), size = 4, color = "blue") +
  geom_point(aes(x = jx6, y = jy6, frame = .frame), size = 4, color = "blue") +
  geom_point(aes(x = jx7, y = jy7, frame = .frame), size = 4, color = "blue") +
  # senegal
  geom_point(aes(x = sx, y = sy, frame = .frame), size = 4, color = "green") +
  geom_point(aes(x = sx2, y = sy2, frame = .frame), size = 4, color = "green") +
  geom_point(aes(x = sx3, y = sy3, frame = .frame), size = 4, color = "green") +
  geom_point(aes(x = sx4, y = sy4, frame = .frame), size = 4, color = "green") +
  geom_point(aes(x = sx5, y = sy5, frame = .frame), size = 4, color = "green") +
  geom_point(aes(x = sx6, y = sy6, frame = .frame), size = 4, color = "green") +
  
  # free kick spot (reference)
  geom_point(aes(x = 70, y = 85), color = "black", size = 1.2) +
  # goalkeeper
  geom_point(data = goalkeeper_data,
             aes(x = x, y = y), size = 4, color = "blue") +
  # senegal defenders
  geom_point(data = senegal_data,
             aes(x = x, y = y), size = 4, color = "green") +
  annotate(
    geom = "text", family = "Dusha V5", 
    hjust = c(0, 0, 0.5),
    size = c(6, 6.5, 3),
    label = c("Japan             (2) vs. Senegal             (2)",
              "The Perfect Offside Trap",
              "by Ryo Nakagawara\n(@R_by_Ryo)"),
    x = c(30,  30, 94), 
    y = c(107, 115, -3)) +
  ggimage::geom_flag(data = flag_data,
                     aes(x = c(48, 90), y = c(107, 107),
                         image = image),       
                     size = c(0.07, 0.07)) +
  ggimage::geom_emoji(aes(x = x, y = y, 
                          frame = .frame),
                      image = "26bd", size = 0.035)

gganimate(g2, 
          interval = 0.001, height = 10, width = 8,
          "offside_final.gif", 
          title_frame = FALSE) 

```






+ the meme generator with R

```{r meme}
library(memery)
img <- ("https://imgflip.com/s/meme/Roll-Safe-Think-About-It.jpg")

meme_labs <- c("you can't lose the aerial battle", "if you set an offside trap")

meme(img, meme_labs, "offside_meme.png")

```

