-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2022_11_29_fifa_world_cup.Rmd
144 lines (100 loc) · 3.64 KB
/
2022_11_29_fifa_world_cup.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
---
title: "FIFA World Cup"
date: 2022-11-29
output: html_document
---
# TidyTuesday
Join the R4DS Online Learning Community in the weekly #TidyTuesday event! Every week we post a raw dataset, a chart or article related to that dataset, and ask you to explore the data. While the dataset will be "tamed", it will not always be tidy! As such you might need to apply various R for Data Science techniques to wrangle the data into a true tidy format. The goal of TidyTuesday is to apply your R skills, get feedback, explore other's work, and connect with the greater #RStats community! As such we encourage everyone of all skills to participate!
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(lubridate)
library(tidytuesdayR)
library(scales)
theme_set(theme_light())
```
# Load the weekly Data
Download the weekly data and make available in the `tt` object.
```{r Load}
tt <- tt_load("2022-11-29")
```
# Readme
Take a look at the readme for the weekly data to get insight on the dataset. This includes a data dictionary, source, and a link to an article on the data.
```{r Readme, eval = interactive()}
tt
```
# Glimpse Data
Take an initial look at the format of the data available.
```{r Glimpse}
tt %>%
map(glimpse)
```
# Wrangle
Explore the data and process it into a nice format for plotting! Access each dataset by name by using a dollarsign after the `tt` object and then the name of the data set.
```{r Wrangle}
wcmatches <- tt$wcmatches %>%
mutate(stage = str_trim(stage))
worldcups <- tt$worldcups
```
```{r}
wcmatches %>%
distinct(year) # 21 rows
(2018 - 1930) / 4 + 1 # 23, two missing years 1942 and 1946
wcmatches %>%
count(outcome, sort = TRUE)
wcmatches %>%
count(stage) %>% View()
wcmatches %>%
count(home_team, sort = TRUE)
wcmatches %>%
count(away_team, sort = TRUE)
```
# Visualize
Using your processed dataset, create your unique visualization.
```{r Visualize}
wcmatches2 <- wcmatches %>%
mutate(
id = row_number(),
across(ends_with("_team"), ~ if_else(.x == "West Germany", "Germany", .x)),
home_team_bk = home_team
) %>%
pivot_longer(home_team:away_team, names_to = "home_away", values_to = "team") %>%
group_by(id) %>%
arrange(team) %>%
mutate(home_away = paste0("team", 1:2)) %>%
ungroup() %>%
pivot_wider(names_from = home_away, values_from = team) %>%
unite("match", team1:team2, sep = " - ", remove = FALSE) %>%
mutate(
team1_score = if_else(team1 == home_team_bk, home_score, away_score),
team2_score = if_else(team2 == home_team_bk, home_score, away_score),
team2_diff = team2_score - team1_score
)
match_freq <- wcmatches2 %>%
count(match, sort = TRUE) %>%
filter(n >= 5) %>%
pull(match)
p <- wcmatches2 %>%
filter(match %in% match_freq) %>%
ggplot(aes(team2_diff)) +
geom_histogram() +
geom_vline(xintercept = 0, color = "red", lty = 3) +
scale_x_continuous(breaks = -6:4) +
scale_y_continuous(breaks = c(0, 5)) +
facet_wrap(vars(match), ncol = 3) +
labs(x = "RHS score - LHS score", y = "Count",
title = "Argentina always beat Nigeria by 1 point margin in the past",
subtitle = ">=5 matches in the World Cup. Teams are alphabetical order.",
caption = "Note: Germany includes West Germany\nSource: Kaggle FIFA World Cup") +
theme(
panel.grid = element_blank(),
strip.background = element_blank(),
strip.text = element_text(color = "black")
)
```
# Save Image
Save your image for sharing. Be sure to use the `#TidyTuesday` hashtag in your post on twitter!
```{r}
# This will save your most recent plot
ggsave("image/fifa_world_cup.png", p, width = 6, height = 6)
```