-
Notifications
You must be signed in to change notification settings - Fork 25
/
jleague_2020_review_code.Rmd
681 lines (578 loc) · 24.6 KB
/
jleague_2020_review_code.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
---
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
```