-
Notifications
You must be signed in to change notification settings - Fork 1
/
team-standings-race.Rmd
232 lines (194 loc) · 7.42 KB
/
team-standings-race.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
---
title: "Standings team race"
author: "Flavio Leccese"
date: "`r Sys.Date()`"
output: html_document
---
```{r markdown-set, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
message = FALSE,
warning = FALSE
)
invisible(Sys.setlocale(locale = "en_EN.UTF-8"))
library(devtools)
install_github("teunbrand/elementalist")
library(elementalist)
```
## Goal
We want to create a chart for each team showing the progression of standings and
final competition score.
Data can be obtained through `getCompetitionStandings` function.
Resulted plot can be found at the bottom of this page.
## Setup
```{r setup}
library(euroleaguer)
library(tidyr)
library(dplyr)
library(ggplot2)
library(ggtext)
library(showtext)
library(ggimage)
library(glue)
library(geomtextpath)
library(hexbin)
# Add Lato font (Euroleague official font)
font_add_google("Lato", "Lato")
font_add_google("Inconsolata", "Inconsolata")
# Add Font Awesome for logos
font_add(family = "Font Awesome 6 Brands",
regular = "figures/fa-brands-400.ttf")
showtext_opts(dpi = 200)
showtext_auto()
```
## Data
```{r data}
CompetitionRounds <- getCompetitionRounds("E2023") %>%
filter(Sys.Date() >= MaxGameStartDate)
CompetitionStandings <- getCompetitionStandings("E2023", CompetitionRounds$Round)
MaxRound <- max(CompetitionStandings$Round)
MaxGameDate <- CompetitionRounds %>% filter(Round == MaxRound) %>% pull(MaxGameStartDate) %>%
format(., '%d %b %Y')
NRounds <- 10
MinRound <- MaxRound - NRounds
Teams <- getTeam("E2023", unique(CompetitionStandings$TeamCode))
```
## Data for plot
```{r data for plot}
CompetitionStandings <-
CompetitionStandings %>%
left_join(Teams %>% select(TeamCode, PrimaryColor, SecondaryColor),
by = "TeamCode") %>%
left_join(CompetitionStandings %>% filter(Round == MaxRound) %>%
arrange(desc(Position)) %>%
mutate(
gp = ifelse(max(GamesPlayed) != min(GamesPlayed), glue(" {GamesPlayed} gp"), ""),
y = factor(glue("**#{Position}** ({GamesWon*2} pts{gp}) - {TeamName}"),
levels = glue("**#{Position}** ({GamesWon*2} pts{gp}) - {TeamName}"))) %>%
select(y, Position),
by = "Position") %>%
filter(Round %in% (MaxRound - NRounds):MaxRound) %>%
mutate(x = Round)
LastRound <- CompetitionStandings %>% filter(Round == MaxRound)
PreviousRounds <- CompetitionStandings %>% filter(Round < MaxRound)
```
## Image for plot
```{r images for plot}
TeamImage <- CompetitionStandings %>%
slice_max(order_by = Round) %>%
filter(!is.na(TeamImagesCrest)) %>%
distinct(x, y, TeamName, TeamImagesCrest)
```
## Playoffs and playins
```{r lines for plot}
PlayInsOff = LastRound %>%
filter(Position %in% c(6, 10)) %>%
mutate(Label = ifelse(Position == 10, "Play-In", "Playoffs")) %>%
select(Label, y)
```
## Title, subtitle and caption
```{r title, subtitle and caption}
PlotTitle <- glue(
"<span style = 'font-size: 32px'>Team standings by round</span><br>
<span style = 'font-size: 20px'>All teams | From round {MaxRound - NRounds} to round {MaxRound} | {MaxGameDate}</span>")
PlotSubtitle <- glue(
"<span><img src = 'figures/euroleague-logo-vertical.png'
height='50'></span>")
PlotCaption <- glue(
"<span>Visualization with </span>
<span style = 'font-family:\"Inconsolata\";'>R</span>
<span>and</span>
<span style = 'font-family:\"Inconsolata\";'>ggplot2</span>
<span>by Flavio Leccese |</span>
<span style = 'font-family:\"Font Awesome 6 Brands\";'></span>
<span>flavioleccese92</span>
<span style = 'font-family:\"Font Awesome 6 Brands\";'></span>
<span>flavioleccese</span>")
```
## Plot
```{r plot}
# Initialize
e <- CompetitionStandings %>%
ggplot(aes(x = x, y = y))
# Add vertical lines
e <- e +
geom_vline(data = PreviousRounds, aes(xintercept = x), colour = "#f2f2f2")
# Add standing lines by team
e <- e +
geom_line(stat = "smooth", method = "loess", formula = 'y ~ x',
aes(colour = SecondaryColor, group = TeamName),
alpha = 0.50, linewidth = 1.25, se = FALSE, span = 0.25) +
geom_line(stat = "smooth", method = "loess", formula = 'y ~ x',
aes(colour = PrimaryColor, group = TeamName),
alpha = 0.50, linewidth = 1.25, se = FALSE, span = 0.25)
# Add points by round and team
e <- e +
geom_point(data = PreviousRounds, aes(colour = SecondaryColor), alpha = 0.50, size = 3) +
geom_point(data = PreviousRounds, aes(colour = PrimaryColor), alpha = 0.50, size = 3) +
geom_point(data = PreviousRounds, colour = "#f2f2f2", size = 1) +
geom_point(data = LastRound, aes(colour = SecondaryColor), alpha = 0.50, size = 12) +
geom_point(data = LastRound, aes(colour = PrimaryColor), alpha = 0.50, size = 12) +
geom_point(data = LastRound, colour = "#f2f2f2", size = 10)
# Add lines of Play-In and Playoffs
e <- e +
geom_rect(aes(xmin = -Inf, xmax = MinRound - 0.25,
ymin = -Inf, ymax = +Inf),
fill = "#f2f2f2", color = NA) +
geom_hline(data = PlayInsOff, aes(yintercept = as.numeric(y) - 0.5), colour = "#F47321",
linetype = "dashed") +
geom_text(data = PlayInsOff, angle = 90, hjust = 0, colour = "#F47321",
aes(label = Label, y = as.numeric(y) - 0.25, x = MinRound - 0.5))
# Add images + general theme setting
e <- e +
geom_image(data = TeamImage, aes(y = y, image = TeamImagesCrest), size = 0.03,
image_fun = function(img) { magick::image_crop(img) }) +
scale_colour_identity() +
scale_y_discrete(position = "right") +
scale_x_continuous(breaks = MinRound:MaxRound, labels = MinRound:MaxRound,
min = MinRound, expand = c(0.01, 0.1)) +
theme(
# General
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "#e2e7ea"),
plot.background = element_rect(fill = "#f2f2f2", colour = "transparent"),
plot.margin = margin(28, 15, 28, 17),
text = element_text(color = "#404040", family = "Lato"),
# Axis labels
axis.ticks = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_text(size = 20, vjust = -2.5),
axis.text.y.right = element_markdown(size = 14),
axis.text.x = element_text(size = 14, vjust = 0.5),
# Legend
legend.background = element_blank(),
legend.box.background = element_blank(),
legend.key = element_blank(),
legend.position = 'bottom',
legend.justification = 'left',
legend.direction = 'horizontal',
legend.margin = margin(10, 0, 15, 0),
legend.box.spacing = unit(0, "pt"),
# Title, subtitle, caption
plot.title = element_markdown(
lineheight = 1, size = 24, hjust = 0, vjust = 1, margin = margin(0, 0, -20, 0)),
plot.title.position = "plot",
plot.subtitle = element_markdown(
hjust = 1, margin = margin(-33, 3, -50, 0)),
plot.caption = element_markdown(
size = 12, margin = margin(30, 0, 0, 3)),
plot.caption.position = "plot",
# Facet
strip.background = element_rect(fill = "#F47321"),
strip.text = element_text(colour = "black", hjust = 0)
) +
labs(title = PlotTitle, subtitle = PlotSubtitle, caption = PlotCaption,
x = "Round", y = "")
```
```{r save, include=FALSE}
# Save plot (standard width 4100)
ggsave("team-standings-race.png", plot = e, path = "../../man/figures/",
height = 2000, width = 3000, units = "px", dpi = 200)
```
## Result
![](https://raw.githubusercontent.com/FlavioLeccese92/euroleaguer/main/man/figures/team-standings-race.png)