forked from Dhahak-Amin/afcon-homogenisation-eda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualisation.R
More file actions
201 lines (176 loc) · 5.79 KB
/
visualisation.R
File metadata and controls
201 lines (176 loc) · 5.79 KB
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
library(dplyr)
library(ggplot2)
library(readr)
# CHARGEMENT DES DONNÉES PRÊTES
afcon <- read_csv("data/afcon_results.csv", show_col_types = FALSE)
# --- GRAPHIQUE 1 : ÉVOLUTION TEMPORELLE ---
p1 <- ggplot(afcon, aes(x = year, fill = comp_type)) +
geom_histogram(binwidth = 2, position = "stack", color = "white") +
scale_fill_manual(values = c("finals" = "#FF9933", "qualification" = "#009900"),
labels = c("Phase Finale", "Qualifications")) +
labs(title = "1. Expansion historique de la CAN",
x = "Année", y = "Nombre de matchs", fill = "Type") +
theme_minimal() +
theme(legend.position = "top")
print(p1)
# --- GRAPHIQUE 2 : TENDANCE DES BUTS ---
stats_buts <- afcon %>%
group_by(year, comp_type) %>%
summarise(avg_goals = mean(total_goals), .groups = 'drop')
p2 <- ggplot(stats_buts, aes(x = year, y = avg_goals, color = comp_type)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "loess", se = FALSE, linewidth = 1.2) +
scale_color_manual(values = c("finals" = "#FF9933", "qualification" = "#009900"),
labels = c("Phase Finale", "Qualifications")) +
labs(title = "2. Évolution de la moyenne de buts",
x = "Année", y = "Buts par match", color = "") +
theme_minimal()
print(p2)
# --- VARIABLES COMMUNES : les périodes (pour Graph 3 et 4) ---
afcon <- afcon %>%
mutate(
neutral = as.logical(neutral),
period = case_when(
year < 1980 ~ "Avant 1980",
year >= 1980 & year <= 1999 ~ "1980-1999",
year >= 2000 & year <= 2009 ~ "2000-2009",
year >= 2010 ~ "2010+",
TRUE ~ NA_character_
),
period = factor(period, levels = c("Avant 1980", "1980-1999", "2000-2009", "2010+")),
neutral_label = if_else(neutral, "Terrain neutre", "Non neutre")
)
# --- GRAPHIQUE 3 : AVANTAGE DU DOMICILE ---
# Séparé par neutral (TRUE/FALSE) et par période
home_adv <- afcon %>%
filter(!is.na(period), !is.na(result), !is.na(neutral)) %>%
mutate(
outcome = factor(
result,
levels = c("H", "D", "A"),
labels = c("Victoire home", "Nul", "Victoire away")
)
) %>%
group_by(period, neutral_label, outcome) %>%
summarise(n = n(), .groups = "drop") %>%
group_by(period, neutral_label) %>%
mutate(rate = n / sum(n)) %>%
ungroup()
p3 <- ggplot(home_adv, aes(x = period, y = rate, fill = outcome)) +
geom_col(color = "white") +
facet_wrap(~ neutral_label) +
scale_y_continuous(
labels = function(x) paste0(round(100 * x), "%"),
limits = c(0, 1)
) +
scale_fill_manual(
values = c(
"Victoire home" = "#009900",
"Nul" = "#FF9933",
"Victoire away"= "#8B5A2B"
)
) +
labs(
title = "3. Avantage du domicile : répartition des résultats",
x = "Période",
y = "Part des matchs",
fill = ""
) +
theme_minimal() +
theme(legend.position = "top")
print(p3)
# --- GRAPHIQUE 4 : ECART DE NIVEAU ---
afcon <- afcon %>% mutate(abs_goal_diff = abs(home_score - away_score))
gap_dist <- afcon %>%
filter(!is.na(period), !is.na(abs_goal_diff), !is.na(comp_type)) %>%
mutate(
gap_class = case_when(
abs_goal_diff == 0 ~ "0",
abs_goal_diff == 1 ~ "1",
abs_goal_diff == 2 ~ "2",
abs_goal_diff >= 3 ~ "3+",
TRUE ~ NA_character_
),
gap_class = factor(gap_class, levels = c("0", "1", "2", "3+"))
) %>%
count(period, comp_type, gap_class, name = "n") %>%
group_by(period, comp_type) %>%
mutate(
rate = n / sum(n),
pct_raw = 100 * rate,
pct_floor = floor(pct_raw),
frac = pct_raw - pct_floor,
remainder = 100 - sum(pct_floor)
) %>%
arrange(desc(frac), .by_group = TRUE) %>%
mutate(
pct_int = pct_floor + if_else(row_number() <= remainder, 1, 0)
) %>%
arrange(gap_class, .by_group = TRUE) %>%
ungroup()
check_sum <- gap_dist %>%
group_by(period, comp_type) %>%
summarise(sum_rate = sum(rate), sum_pct = sum(pct_int), .groups = "drop")
print(check_sum)
p4 <- ggplot(gap_dist, aes(x = period, y = gap_class, fill = pct_int / 100)) +
geom_tile(color = "white") +
geom_text(aes(label = paste0(pct_int, "%")), size = 3) +
facet_wrap(
~ comp_type, nrow = 1,
labeller = as_labeller(c(finals = "Phase Finale", qualification = "Qualifications"))
) +
scale_fill_gradientn(
colours = c("#0B6623", "#F4C430", "#B22222"),
labels = function(x) paste0(round(100 * x), "%"),
limits = c(0, 1)
) +
labs(
title = "Matchs serrés : écarts de buts (%)",
x = "Période",
y = "|diff| (buts)",
fill = ""
) +
theme_minimal() +
theme(legend.position = "top")
print(p4)
# --- GRAPHIQUE 5 :TOP 10 DES VAINQUEURS ---
victoires <- afcon %>%
mutate(winner = case_when(
result == "H" ~ home_team,
result == "A" ~ away_team,
TRUE ~ NA_character_
)) %>%
mutate(winner = case_when(
winner == "United Arab Republic" ~ "Egypt",
winner == "Ivory Coast" ~ "Côte d'Ivoire",
winner == "Zaire" ~ "DR Congo",
TRUE ~ winner
)) %>%
filter(!is.na(winner)) %>%
count(winner, sort = TRUE) %>%
head(10)
p5 <- ggplot(victoires, aes(x = reorder(winner, n), y = n, fill = n)) +
geom_col() +
coord_flip() +
scale_fill_gradient(low = "#FFE5CC", high = "#FF9933") +
geom_text(aes(label = n), hjust = 1.2, color = "white", fontface = "bold") +
labs(title = "5. Les Rois de la Victoire",
x = "", y = "Nombre de victoires") +
theme_minimal() +
theme(legend.position = "none")
print(p5)
verif_classement <- afcon %>%
mutate(winner = case_when(
result == "H" ~ home_team,
result == "A" ~ away_team,
TRUE ~ NA_character_
)) %>%
mutate(winner = case_when(
winner == "United Arab Republic" ~ "Egypt",
winner == "Ivory Coast" ~ "Côte d'Ivoire",
winner == "Zaire" ~ "DR Congo",
TRUE ~ winner
)) %>%
filter(!is.na(winner)) %>%
count(winner, sort = TRUE)
print(head(verif_classement, 15))