-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreus-stats.R
137 lines (125 loc) · 4.81 KB
/
reus-stats.R
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
library(tidyverse)
library(ggtext)
library(worldfootballR)
library(here)
base_path <- here("marco-reus")
reus_url <- "https://fbref.com/en/players/36a3ff67/Marco-Reus"
stats <- fb_player_season_stats(reus_url, stat_type = "standard")
unique(stats$Squad)
# Prepare data for the plot
goals_assists <- stats |>
mutate(
season_end_year = str_extract(Season, "-(\\d{4})", group = 1),
season_end_year = as.numeric(season_end_year),
Squad = fct_reorder(Squad, season_end_year)) |>
group_by(season_end_year, Squad) |>
summarize(
Gls = sum(Gls),
Ast = sum(Ast),
.groups = "drop") |>
pivot_longer(cols = c(Gls, Ast), names_to = "stat_type", values_to = "value")
# Titles and labels
plot_titles <- list(
"en" = list(
title = "Marco Reus Career Goals and Assists",
subtitle = "**Goals** and <b style='color:grey70'>assists</b> in all
competitions for<br>
<b style='color:#E20024'>Rot Weiss Ahlen</b>,
<b style='color:white'>Borussia M'Gladbach</b>, and
<b style='color:#FDE100'>Borussia Dortmund</b>",
caption = "2023-2024 figures as of matchday 32.<br>
Source: FBRef.com.
Image credit: Tim Reckmann (CC BY-SA 3.0).
Visualization: Ansgar Wolsing",
x = NULL, y = "# of goals and assists",
annotation_goals_assists = c("Goals", "Assists")
),
"de" = list(
title = "Marco Reus' Tore und Assists",
subtitle = "**Tore** und <b style='color:grey70'>Torvorlagen</b> in allen
Wettbewerben für<br>
<b style='color:#E20024'>Rot Weiss Ahlen</b>,
<b style='color:white'>Borussia M'Gladbach</b> und
<b style='color:#FDE100'>Borussia Dortmund</b>",
caption = "Saison 2023-24 bis 32. Spieltag<br>
Daten: FBRef.com.
Bild: Tim Reckmann (CC BY-SA 3.0).
Visualisierung: Ansgar Wolsing",
x = NULL, y = "Anzahl Tore und Torvorlagen",
annotation_goals_assists = c("Tore", "Assists")
)
)
# Gradient fill for the plot background
gradient_fill <- grid::linearGradient(c("grey6", "grey14"))
# Creates the plot with labels in the given language defined in plot_titles
plot_with_labels <- function(lang) {
stopifnot(lang %in% names(plot_titles))
goals_assists |>
ggplot(aes(season_end_year, value, group = stat_type, fill = Squad)) +
geom_col(aes(alpha = stat_type, col = Squad), width = 0.8, linewidth = 0.3) +
# insert picture
annotate(
GeomRichtext,
x = 2024, y = 35,
label = sprintf(
"<img src='%s' width='40'>",
here(base_path, "reus-picture.png")),
fill = NA, label.size = 0
) +
# annotations for goals and assists
annotate(
"text",
x = 2018,
y = c(26, 32),
label = plot_titles[[lang]]$annotation_goals_assists,
color = "grey76", hjust = 1, family = "Avenir", size = 3
) +
annotate(
GeomCurve,
x = 2017.5, xend = 2018.5,
y = c(25, 31), yend = c(18, 29),
color = "grey76", linewidth = 0.2, curvature = 0.3,
arrow = arrow(angle = 20, length = unit(1, "mm"), type = "open")
) +
scale_fill_manual(
values = c("Rot Weiss Ahlen" = "#E20024", "M'Gladbach" = "white",
"Dortmund" = "#FDE100"),
aesthetics = c("fill", "color")) +
scale_x_continuous(
breaks = seq(min(goals_assists$season_end_year),
max(goals_assists$season_end_year), 2)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
scale_alpha_manual(values = c(0.6, 0.9)) +
coord_cartesian(clip = "off") +
guides(fill = "none", color = "none", alpha = "none") +
labs(
title = plot_titles[[lang]]$title,
subtitle = plot_titles[[lang]]$subtitle,
caption = plot_titles[[lang]]$caption,
x = plot_titles[[lang]]$x, y = plot_titles[[lang]]$y
) +
theme_minimal(base_family = "Avenir", base_size = 10) +
theme(
plot.background = element_rect(color = gradient_fill, fill = gradient_fill),
text = element_text(color = "grey99"),
axis.title = element_text(color = "grey76"),
axis.text = element_text(color = "grey76"),
plot.title = element_text(family = "Bangers", hjust = 0.5, size = 18),
plot.subtitle = element_textbox(
lineheight = 1.25, width = 1.1, hjust = 0.5, halign = 0.5,
margin = margin(t = 6, b = 2)),
plot.caption = element_markdown(
hjust = 0, color = "grey76", size = 6, lineheight = 1.25),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(color = "grey30", linewidth = 0.2),
panel.grid.minor.y = element_line(color = "grey30", linewidth = 0.1),
legend.position = "bottom"
)
}
plot_with_labels("en")
ggsave(here(base_path, "reus-goals-assists-career-en.png"),
width = 5, height = 5)
plot_with_labels("de")
ggsave(here(base_path, "reus-goals-assists-career-de.png"),
width = 5, height = 5)