-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformula 1 export.Rmd
804 lines (716 loc) · 32.9 KB
/
formula 1 export.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
---
title: |-
F1 results
\vspace{0.5in}
author:
- name: Christoffer Trier Månsson
output:
BiocStyle::html_document:
toc: yes
toc_depth: 2
number_sections: yes
highlight: haddock
date: "`r format(Sys.time(), '%d %b %Y')`"
---
```{r, include = FALSE}
knitr::opts_chunk$set(
tidy.opts = list(width.cutoff=100),
tidy = FALSE,
message = FALSE,
collapse = TRUE,
comment = "#>",
root.dir = "C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive"
)
knitr::opts_knit$set(root.dir = "C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive")
```
\newpage
# Bets
<img src="download.png" width="200" align="right">
```{r, echo=FALSE,warning=F}
library(dplyr)
`%ni%` <- Negate(`%in%`)
bet_df <- function(Christoffer, Emil, Mikkel){
drivers <- df_drivers_2022 <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive/drivers.csv") %>% select(driverId, forename, surname)
if(any(Christoffer %ni% drivers$surname)){
stop("Én eller flere af Christoffers bud er ikke gyldige")
}
if(any(Emil %ni% drivers$surname)){
stop("Én eller flere af Emils bud er ikke gyldige")
}
if(any(Mikkel %ni% drivers$surname)){
stop("Én eller flere af Mikkels bud er ikke gyldige")
}
if(length(unique(Christoffer[1:3]))!= 3){
stop("Christoffer har ikke givet 3 forskellige bud")
}
if(length(unique(Emil[1:3]))!= 3){
stop("Emil har ikke givet 3 forskellige bud")
}
if(length(unique(Mikkel[1:3]))!= 3){
stop("Mikkel har ikke givet 3 forskellige bud")
}
df <- data.frame(Bets = c("Vinder", "Andenplads", "Tredjeplads","Joker"),
Christoffer = Christoffer,
Emil = Emil,
Mikkel = Mikkel)
return(df)
}
bet_df_w <- bet_df(Christoffer = c("Verstappen", "Alonso", "Sainz","Pérez"),
Emil = c("Verstappen","Alonso","Hamilton", "Pérez"),
Mikkel = c("Verstappen", "Russell", "Alonso","Pérez"))
```
## Seneste top 10
Her præsenterer jeg seneste løbs top 10.
```{r, echo=FALSE,warning=F}
library(dplyr)
library(ggplot2)
library(viridis)
library(RCurl)
df_race <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv")
df_race <- df_race[nrow(df_race),]
df_results <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv") %>% filter(Track == df_race$Track) %>%
filter(Points > 0) %>%
arrange(Points)
df_results <- df_results %>%
mutate(Driver = factor(Driver, levels = df_results$Drive))
ggplot(df_results,aes(x = Driver, y = Points,
fill = Driver)) +
scale_fill_viridis("magma",discrete = T)+
geom_bar(stat = "identity")+
theme_minimal()+
labs(x = "",
y = "Points",
title = paste(df_race$name,df_race$date))+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1,
size = 12, face = "bold"),
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 10, vjust = 1, hjust = 0.37),
axis.text.y = element_text(angle = 0, size = 12),
axis.title = element_text(size = 14, face = 'bold'),
axis.title.x = element_text(size = 14, face = 'bold'),
axis.title.y = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 12),
title = element_text(size = 14, face = "bold"),
panel.grid.major.x = element_blank())+
guides(fill="none")
```
## Seneste bet resultat
```{r, echo=FALSE,warning=F}
library(dplyr)
point_calculator <- function(bet_df){
df_race <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv")
df_race <- df_race[nrow(df_race),]
df_results <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv") %>% filter(Track == df_race$Track)
df_drivers <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive/drivers.csv") %>%
mutate(tot_name = paste(forename, surname)) %>%
select(tot_name, surname) %>%
mutate(new_names = ifelse(surname == "Pérez","Sergio Perez",tot_name)) %>%
mutate(new_names = ifelse(surname == "Hülkenberg","Nico Hulkenberg",new_names)) %>%
mutate(new_names = ifelse(surname == "de Vries","Nyck De Vries",new_names))
df_results <- df_results %>% left_join(df_drivers, by = c("Driver" = "new_names"))
#Christoffer
bet_Christoffer <- bet_df %>% select(Christoffer)
res_Christoffer <- match(bet_Christoffer$Christoffer[1:3], df_results$surname)
for(i in 1:length(res_Christoffer)){
ddf <- df_results[res_Christoffer[i],]
if(ddf$Time.Retired == "DNF"){
res_Christoffer[i] <- 20
}
}
Christoffer_points <- sum(abs(res_Christoffer-c(1,2,3)))
joker_Christoffer <- df_results %>%
filter(surname == bet_Christoffer$Christoffer[4])
joker_res_Christoffer <- ifelse(joker_Christoffer$Time.Retired == "DNF",
20-joker_Christoffer$Starting.Grid,
as.numeric(joker_Christoffer$Position)-
joker_Christoffer$Starting.Grid)
bet_Emil <- bet_df %>% select(Emil)
res_Emil <- match(bet_Emil$Emil[1:3], df_results$surname)
joker_Emil <- df_results %>%
filter(surname == bet_Emil$Emil[4])
joker_res_Emil <- ifelse(joker_Emil$Time.Retired == "DNF",
20-joker_Emil$Starting.Grid,
as.numeric(joker_Emil$Position)-
joker_Emil$Starting.Grid)
for(i in 1:length(res_Emil)){
ddf <- df_results[res_Emil[i],]
if(ddf$Time.Retired == "DNF"){
res_Emil[i] <- 20
}
}
Emil_points <- sum(abs(res_Emil-c(1,2,3)))
#Mikkel
bet_Mikkel <- bet_df %>% select(Mikkel)
res_Mikkel <- match(bet_Mikkel$Mikkel[1:3], df_results$surname)
for(i in 1:length(res_Mikkel)){
ddf <- df_results[res_Mikkel[i],]
if(ddf$Time.Retired == "DNF"){
res_Mikkel[i] <- 20
}
}
Mikkel_points <- sum(abs(res_Mikkel-c(1,2,3)))
joker_Mikkel <- df_results %>%
filter(surname == bet_Mikkel$Mikkel[4])
joker_res_Mikkel <- ifelse(joker_Mikkel$Time.Retired == "DNF",
20-joker_Mikkel$Starting.Grid,
as.numeric(joker_Mikkel$Position)-
joker_Mikkel$Starting.Grid)
collected_points <- c(Christoffer_points,Emil_points,Mikkel_points)
if(any(collected_points == 0)){
if(sum(collected_points == 0) == 1){
winner_all3 <- colnames(bet_df)[c(2,3,4)][collected_points == 0]
}
}
else{
winner_all3 <- NULL
}
collected_winners <- c(res_Christoffer[1]-1,res_Emil[1]-1,res_Mikkel[1]-1)
if(is.null(winner_all3)){
if(any(collected_winners == 0)){
if(sum(collected_winners == 0) == 1){
winner <- colnames(bet_df)[c(2,3,4)][collected_winners == 0]
}
else{
winner <- NULL
}
}
else{
winner <- NULL
}
}
else{
winner <- NULL
}
res_df <- data.frame(Bets = "Point",
Christoffer = Christoffer_points,
Emil = Emil_points,
Mikkel = Mikkel_points)
if(!is.null(winner_all3)){
res_df[winner_all3] <- -15
}
if(!is.null(winner)){
res_df[winner] <- -5
}
res_df$Christoffer <- res_df$Christoffer + joker_res_Christoffer
res_df$Emil <- res_df$Emil + joker_res_Emil
res_df$Mikkel <- res_df$Mikkel + joker_res_Mikkel
df <- rbind(bet_df,res_df)
return(df)
}
```
Her kan man se hvor mange point vi fik efter seneste grandprix.
```{r, echo = F,warning=F}
library(dplyr)
df_race <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv")
df_race <- df_race[nrow(df_race),]
print(paste(df_race$Track))
print(point_calculator(bet_df_w))
```
## Seneste Joker resultat
Her kan man se hvordan jokeren har spillet ind på resultatet
```{r, echo = FALSE,warning=F}
joker_plot <- function(bet_df){
library(ggrepel)
library(viridis)
df_race <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv")
df_race <- df_race[nrow(df_race),]
df_results <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv") %>% filter(Track == df_race$Track)
df_drivers <-
read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive/drivers.csv") %>%
mutate(tot_name = paste(forename, surname)) %>%
select(tot_name, surname) %>%
mutate(new_names = ifelse(surname == "Pérez","Sergio Perez",tot_name)) %>%
mutate(new_names = ifelse(surname == "Hülkenberg","Nico Hulkenberg",new_names)) %>%
mutate(new_names = ifelse(surname == "de Vries","Nyck De Vries",new_names))
df_results <- df_results %>% left_join(df_drivers, by = c("Driver" = "new_names"))
bet_Christoffer <- bet_df %>% select(Christoffer)
bet_Emil <- bet_df %>% select(Emil)
bet_Mikkel <- bet_df %>% select(Mikkel)
joker_Christoffer <- df_results %>%
filter(surname == bet_Christoffer$Christoffer[4])
joker_res_Christoffer <- ifelse(joker_Christoffer$Time.Retired == "DNF",
20-joker_Christoffer$Starting.Grid,
as.numeric(joker_Christoffer$Position)-
joker_Christoffer$Starting.Grid)
joker_Emil <- df_results %>%
filter(surname == bet_Emil$Emil[4])
joker_res_Emil <- ifelse(joker_Emil$Time.Retired == "DNF",
20-joker_Emil$Starting.Grid,
as.numeric(joker_Emil$Position)-
joker_Emil$Starting.Grid)
joker_Mikkel <- df_results %>%
filter(surname == bet_Mikkel$Mikkel[4])
joker_res_Mikkel <- ifelse(joker_Mikkel$Time.Retired == "DNF",
20-joker_Mikkel$Starting.Grid,
as.numeric(joker_Mikkel$Position)-
joker_Mikkel$Starting.Grid)
df <- data.frame(person = c(rep("Christoffer",2),
rep("Emil",2),
rep("Mikkel",2)),
type = factor(rep(c("Start","Slut"),3),
levels = c("Start","Slut")),
Position = c(as.numeric(joker_Christoffer$Starting.Grid),
ifelse(joker_Christoffer$Time.Retired == "DNF",
20,
as.numeric(joker_Christoffer$Position)),
as.numeric(joker_Emil$Starting.Grid),
ifelse(joker_Emil$Time.Retired == "DNF",
20,
as.numeric(joker_Emil$Position)),
as.numeric(joker_Mikkel$Starting.Grid),
ifelse(joker_Mikkel$Time.Retired == "DNF",
20,
as.numeric(joker_Mikkel$Position))),
driver = c(rep(bet_Christoffer$Christoffer[4],2),
rep(bet_Emil$Emil[4],2),
rep(bet_Mikkel$Mikkel[4],2)),
result = c(rep(joker_res_Christoffer,2),
rep(joker_res_Emil,2),
rep(joker_res_Mikkel,2)))
gg <- ggplot(df, aes(x = type,y = Position,group = person,
color = person))+
geom_point(size = 5)+
geom_line(linewidth = 1.5)+
scale_color_viridis("cividis",discrete = T)+
theme_minimal()+
labs(title = paste("Joker -",df_race$Track),
x = "",
y = "Placering")+
scale_y_continuous(limits = c(0,20))+
geom_label_repel(aes(label=ifelse(type == "Slut",
paste(driver,result),"")),
nudge_x = 0.25,
na.rm = TRUE,
label.size = NA,
size = 3.5,
fill = alpha(c("white"),0),
parse = F,
max.overlaps = 100)+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1,
size = 12, face = "bold"),
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 10, vjust = 1, hjust = 0.37),
axis.text.y = element_text(angle = 0, size = 12),
axis.title = element_text(size = 14, face = 'bold'),
axis.title.x = element_text(size = 14, face = 'bold'),
axis.title.y = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 12),
title = element_text(size = 14, face = "bold"),
panel.grid.major.x = element_blank(),
legend.title = element_blank())+
guides(color = guide_legend(override.aes = aes(label = "")))
return(gg)
}
joker_plot(bet_df_w)
```
## Løbende udvikling for bets
Her kan man se hvordan det går i vores interne konkurrence efter hvert
grandprix. Først efter 4. løb vil jeg tage de værste 3 løb ud fra vores
totale antal point.
```{r, echo = FALSE,warning=F}
df_race <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv")
df_race <- df_race[nrow(df_race),]
path <- "C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/bet_results"
recent_results <- point_calculator(bet_df_w)
saveRDS(recent_results,file = paste0(path,"/",df_race$Track,".rds"))
```
```{r, echo = FALSE,warning=F}
library(tidyverse)
setwd("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/bet_results")
list_of_files <- list.files(path = "C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/bet_results")
df_1 <- readRDS("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/bet_results/Bahrain.rds") %>% filter(Bets != "date")
df_2 <- readRDS("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/bet_results/Saudi Arabia.rds")
df_3 <- readRDS("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/bet_results/Australia.rds")
####Change 1####
df_race <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv")
df_results <- rbind(df_1,df_2,df_3)####Change 2####
res_df <- df_results %>% filter(Bets == "Point")
res_df$place <- c("Bahrain", "Saudi Arabia", "Australia") ####Change 3####
res_df$Christoffer <- as.numeric(res_df$Christoffer)
res_df$Emil <- as.numeric(res_df$Emil)
res_df$Mikkel <- as.numeric(res_df$Mikkel)
res_df_gg <- data.frame(place = rep(res_df$place,3),
player = c(rep(colnames(res_df)[2],3),####Change 4####
rep(colnames(res_df)[3],3),####Change 5####
rep(colnames(res_df)[4],3)),####Change 6####
result = c(as.numeric(res_df$Christoffer),
as.numeric(res_df$Emil),
as.numeric(res_df$Mikkel))) %>%
group_by(player) %>%
summarise(sum_result = cumsum(result),
place = place)
res_df_gg <- res_df_gg %>%
mutate(place = factor(place, levels = unique(res_df_gg$place)))
ggplot(res_df_gg,aes(x = place,y = sum_result,group = player,
color = player))+
geom_point(size = 5)+
geom_line(linewidth = 1.5)+
scale_color_viridis("cividis",discrete = T)+
theme_minimal()+
labs(title = paste("Standings"),
x = "Race",
y = "Accumulated points")+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1,
size = 12, face = "bold"),
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 10, vjust = 1, hjust = 0.37),
axis.text.y = element_text(angle = 0, size = 12),
axis.title = element_text(size = 14, face = 'bold'),
axis.title.x = element_text(size = 14, face = 'bold'),
axis.title.y = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 12),
title = element_text(size = 14, face = "bold"),
panel.grid.major.x = element_blank(),
legend.title = element_blank())
```
# Kørernes resultater
## Kørernes udvikling
Her kan man se hvem der fører løbet og hvordan de har fået point gennem sæsonen
Jeg viser top10 + Magnussen
```{r, echo = FALSE,warning=F}
df_race <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv") %>%
group_by(Driver) %>%
reframe(.groups = "Driver",
cum_sums = cumsum(Points),
place = Track) %>%
group_by(Driver) %>%
reframe(.groups = "Driver",
cum_sums = cum_sums,
sums = max(cum_sums),
place = place)
top_10 <- df_race %>%
group_by(Driver) %>%
slice(1) %>%
arrange(desc(sums)) %>%
ungroup() %>%
dplyr::slice(1:10)
df_race_gg <- df_race %>%
filter(Driver %in% top_10$Driver | Driver == "Kevin Magnussen") %>%
mutate(label = ifelse(place == unique(df_race$place)[length(unique(df_race$place))],Driver,NA))
df_race_gg <- df_race_gg %>%
mutate(place = factor(place, levels = unique(df_race_gg$place)))
ggplot(df_race_gg, aes(x = place,
y = cum_sums,
colour = Driver,
group = Driver))+
geom_point()+
scale_color_viridis("magma",discrete = T)+
geom_line()+
theme_minimal()+
labs(y = "Points",
x = "",
title = "2023 drivers")+
geom_label_repel(aes(label=label),
nudge_x = 0.5,
na.rm = TRUE,
label.size = NA,
size = 3.5,
fill = alpha(c("white"),0),
parse = F,
max.overlaps = 100)+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1,size = 8),
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 10, vjust = 1, hjust = 0.37),
axis.text.y = element_text(angle = 0, size = 12),
axis.title = element_text(size = 14, face = 'bold'),
axis.title.x = element_text(size = 14, face = 'bold'),
axis.title.y = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 12),
title = element_text(size = 14, face = "bold"))+
guides(color="none")
```
## Teamets udvikling
Her kan man se hvordan de enkelte teams scorer point samlet. Her forventer vi
jo en klar sejr til Haas ;)
```{r, echo = FALSE,warning=F}
df_race <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv") %>%
group_by(Team,Track)
df_race <- df_race %>%
mutate(Track = factor(Track, levels = unique(df_race$Track))) %>%
reframe(.groups = "Team",
team_sums = sum(Points)) %>%
group_by(Team) %>%
reframe(.groups = "Team",
cum_sums = cumsum(team_sums),
place = Track) %>%
group_by(Team) %>%
reframe(.groups = "Team",
cum_sums = cum_sums,
sums = max(cum_sums),
place = place)
df_race <- df_race %>% mutate(label = ifelse(place == unique(df_race$place)[length(unique(df_race$place))],Team,NA))
ggplot(df_race, aes(x = place,
y = cum_sums,
colour = Team,
group = Team))+
geom_point()+
scale_color_viridis("magma",discrete = T)+
geom_line()+
theme_minimal()+
labs(y = "Points",
x = "",
title = "2023 teams")+
geom_label_repel(aes(label=label),
nudge_x = 0.5,
na.rm = TRUE,
label.size = NA,
size = 3.5,
fill = alpha(c("white"),0),
parse = F,
max.overlaps = 100)+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1,size = 8),
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 10, vjust = 1, hjust = 0.37),
axis.text.y = element_text(angle = 0, size = 12),
axis.title = element_text(size = 14, face = 'bold'),
axis.title.x = element_text(size = 14, face = 'bold'),
axis.title.y = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 12),
title = element_text(size = 14, face = "bold"))+
guides(color="none")
```
## Fold difference from teammate
Det her plot viser hvor mange point den førerne kører fra hvert team har scoret
i forhold til hans holdkammerat. Størrelsen på cirklen indikerer
hvor mange på point køren har fået. På Y-aksen samt på farven
kan man se hvor mange point
han har fået i forhold til sin holdkammerat. Navnet på hans hold og teammate kan
ses på X-aksen.
```{r, echo = FALSE,fig.height= 6,warning=F}
df_race <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/formula1_dataset/Formula1_2023season_raceResults.csv") %>%
group_by(Driver) %>%
reframe(.groups = "Driver",
sums = sum(Points),
Team = Team,
Driver = Driver) %>%
group_by(Driver) %>%
slice(1) %>%
group_by(Team) %>%
reframe(.groups = "Driver",
lowest = Driver[which.min(sums)],
Driver = Driver,
sums = sums+1) %>%
group_by(Team) %>%
reframe(fold = ifelse(Driver == lowest,NA,sums/min(sums)),
Driver = Driver,
Points = sums-1,
Team = Team,
lowest = lowest,
team_driver = paste0(Team,", ",lowest)) %>%
filter(!is.na(fold))
ggplot(df_race, aes(x = team_driver,
y = fold,
color = fold,
size = Points))+
geom_point()+
geom_label_repel(aes(label=Driver),
nudge_x = 0.5,
na.rm = TRUE,
label.size = NA,
size = 3.5,
fill = alpha(c("white"),0),
parse = F,
max.overlaps = 100)+
scale_color_gradient(low = "#6a00fc", high = "#ffa10c")+
theme_minimal()+
labs(y = "Fold points compared to teammate",
x = "Teams",
title = "Driver comparisons")+
theme(axis.text.x = element_text(angle = 90, vjust = 1, hjust=1,size = 8),
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 10, vjust = 1, hjust = 0.37),
axis.text.y = element_text(angle = 0, size = 12),
axis.title = element_text(size = 14, face = 'bold'),
axis.title.x = element_text(size = 14, face = 'bold'),
axis.title.y = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 12),
title = element_text(size = 14, face = "bold"))+
guides(fill = guide_legend(title="Points"))
```
# Regler for spillet
Reglerne er simple. Efter kvallen, men inden løbsdagen skal man have sendt til
Christoffer i chatgruppen, **Formula1**, hvilke 3 kører man forventer bliver
nummer 1, 2 og 3 i det kommende løb.
**Alle hjælper alle med at huske at der skal indsendes bud på det kommende løb!**
Glemmer man at give noget bud får man 50 point i straf.
Efter hvert løb bliver det beregnet hvor mange pladser ens kører er væk fra den
plads man gættede på, at køren blev. Hvis en kører udgår
anses det at han slutter sidst og får position 20.
Altså kan man minimalt få 0 point (før bonuser og joker), mens
man maximalt kan få 54 point (20-1 + 20-2 + 20-3 = 19+18+17 = 54).
Slutter ens kører bedre end man har budt får man stadig plus point
I formel1 sæsonen køres 23 løb. For at give lidt sikkerhed medregnes ens 3
dårligste bud ikke i regnskabet og derfor er hver spillers score baseret på
i alt 20 løb til slut i sæsonen.
## Bonus
Der udlådes bonus på to måder.
1) Er man den eneste der har løbets vinder gives -5 point
2) Er man den eneste der har løbets top 3 gives -15 point
Har man kun eks. løbets nummer 1 og 2 gives -5 point.
Har man kun løbets nummer 2 og 3 gives ingen bonus.
## Joker
Hver uge giver man også et bud på en joker. Det er den kører man forventer
vil køre flest placeringer frem i forhold til startpositionen.
De placeringer ens joker kører frem trækkes fra ens point.
For eksempel vælger man en joker der starter som nummer 17 og der slutter
som nummer 10 får man -7 joker point
Altså scorer man 10 point på ens top 3 og ens joker går fra 17 til 10 slutter
man på 3 point (10 - 7 = 3).
Ender ens joker dårligere end han startede lægger man differencen oven i
pointene fra top 3. Altså havde han sluttet som nummer 20 (eller udgået) fra
forrige eksempel havde man fået 3 point oveni og ville altså ende på 13 point.
## Præmie
Det er blevet besluttet at vi alle skal en tur ud at køre gokart når sæsonen
er slut. Her skal nummer 2 og 3 i bettingspillet betale udgifter for
nummer 1. **Plus ham der slutter sidst giver en øl i fredagsbaren! ;)**
# Resultaterne for 2022 sæsonen
Her finder man et lille overblik over hvordan 2022 sæsonen gik.
## Slut resultat
```{r, echo=FALSE,warning=F}
library(dplyr)
df_races_2022 <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive/races.csv") %>% filter(year == 2022)
df_drivers_2022 <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive/drivers.csv") %>% select(driverId, forename, surname)
df_results_2022 <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive/results.csv") %>% filter(raceId %in% df_races_2022$raceId) %>%
group_by(driverId) %>%
summarise(.groups = "keep",
cumulativePoints = cumsum(points),
raceId = raceId,
totPoints = sum(points))
totResult <- abs(sort(-unique(df_results_2022$totPoints)))
df_results_2022 <- df_results_2022 %>%
left_join(df_drivers_2022, by = "driverId") %>%
left_join(df_races_2022 %>% select(raceId,name,date), by = "raceId") %>%
mutate(driverName = paste(forename, surname)) %>%
mutate(isTop10 = ifelse(totPoints %in% totResult[1:10] | surname == "Magnussen","Yes","No")) %>%
filter(isTop10 == "Yes") %>%
mutate(place = factor(name, level = unique(name))) %>%
mutate(label = ifelse(raceId == max(raceId),
ifelse(isTop10 == "Yes", as.character(surname),NA_character_),
NA_character_))
```
Sidste års klare vinder var Max Verstappen, men det var meget tæt mellem nummer
2 og 3.
```{r, echo=FALSE,warning=F}
library(ggplot2)
library(viridis)
library(ggrepel)
res <- df_results_2022 %>% filter(raceId == 1074) %>%
arrange(totPoints)
res <- res %>% mutate(driverName = factor(driverName, levels = res$driverName))
ggplot(res, aes(x = driverName,
y = totPoints,
fill = driverName))+
scale_fill_viridis("magma",discrete = T)+
geom_bar(stat = "identity")+
theme_minimal()+
labs(x = "",
y = "Accumulated points",
title = "2022")+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1,
size = 12, face = "bold"),
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 10, vjust = 1, hjust = 0.37),
axis.text.y = element_text(angle = 0, size = 12),
axis.title = element_text(size = 14, face = 'bold'),
axis.title.x = element_text(size = 14, face = 'bold'),
axis.title.y = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 12),
title = element_text(size = 14, face = "bold"),
panel.grid.major.x = element_blank())+
guides(fill="none")
```
## Udviklingen gennem 2022
Verstappen havde en dårligere start på sæsonen end nogen af hans konkurrenter
Herimod havde Magnussen en meget stille sidste halvdel af sæsonen.
```{r, echo=FALSE,fig.height= 6,warning=F}
library(ggplot2)
library(viridis)
library(ggrepel)
ggplot(df_results_2022, aes(x = place,
y = cumulativePoints,
colour = driverName,
group = driverName))+
geom_point()+
scale_color_viridis("magma",discrete = T)+
geom_line()+
theme_minimal()+
labs(y = "Points",
x = "",
title = "2022")+
geom_label_repel(aes(label=label),
nudge_x = 2,
na.rm = TRUE,
label.size = NA,
size = 3.5,
fill = alpha(c("white"),0),
parse = F,
max.overlaps = 100)+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1,size = 8),
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 10, vjust = 1, hjust = 0.37),
axis.text.y = element_text(angle = 0, size = 12),
axis.title = element_text(size = 14, face = 'bold'),
axis.title.x = element_text(size = 14, face = 'bold'),
axis.title.y = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 12),
title = element_text(size = 14, face = "bold"))+
guides(color="none")
```
## Team ranking i 2022
Red Bull med Perez (samlet nummer 3) og Verstappen (samlet vinder) blev en
rimelig klar vinder af team konkurrencen. Kampen om andepladsen var tæt mellem
Mercedes (Russell (4) og Hamilton (5)) og Ferrari (Leclerc (2) og Sainz (6)).
Williams slutter sidst med bare 7 point på en hel sæson! Det er sgu en ommer!
```{r, echo=FALSE,fig.height= 6,warning=FALSE}
library(dplyr)
library(scales)
df_constructors <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive/constructors.csv") %>% select(constructorId, name)
df_results_2022_teams <- read.csv("C:/Users/chris/OneDrive/1PhD/formula1/formula1/archive/results.csv") %>% filter(raceId %in% df_races_2022$raceId) %>%
group_by(constructorId) %>%
summarise(.groups = "keep",
raceId = raceId,
totPoints = sum(points)) %>%
left_join(df_constructors, by = "constructorId") %>%
filter(raceId == "1095") %>%
arrange(totPoints) %>%
distinct()
df_results_2022_teams %>%
mutate(name = factor(name, levels = unique(df_results_2022_teams$name))) %>%
ggplot(aes(x = name, y = totPoints,
fill = name)) +
scale_fill_viridis("magma",discrete = T)+
geom_bar(stat = "identity")+
theme_minimal()+
scale_y_continuous(trans = "log10",
labels = label_comma())+
labs(x = "",
y = "Accumulated points",
title = "2022")+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1,
size = 12, face = "bold"),
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 10, vjust = 1, hjust = 0.37),
axis.text.y = element_text(angle = 0, size = 12),
axis.title = element_text(size = 14, face = 'bold'),
axis.title.x = element_text(size = 14, face = 'bold'),
axis.title.y = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 12),
title = element_text(size = 14, face = "bold"),
panel.grid.major.x = element_blank())+
guides(fill="none")
```
# Information om data
Data er taget fra følgende side <https://www.kaggle.com/datasets/rohanrao/formula-1-world-championship-1950-2020?resource=download> samt <https://github.com/toUpperCase78/formula1-datasets> som løbende opdaterer den seneste data fra hele F1
Koden til dette dokument er frit tilgængelig gennem <https://github.com/CTrierMaansson/formula1> hvor der også vil komme løbende
opdateringer. Jeg henviser til **issues** fanen for bugs og andre problemer
man kan have med dokumentet. Her kan man også rette forslag til statistik
man ønsker skal være en del af dokumentet.