-
Notifications
You must be signed in to change notification settings - Fork 13
/
script_2019-04-09.R
68 lines (54 loc) · 1.77 KB
/
script_2019-04-09.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
library(tidyverse)
library(gganimate)
#### Data ####
grand_slam_timeline <- read_csv("data/data_2019-04-09.csv", col_types = "cdccc")
#### Tables ####
ordered_outcomes <-c("Absent", "Lost Qualifier", "Qualification Stage 1",
"Qualification Stage 2", "1st Round", "2nd Round",
"3rd Round", "4th Round", "Quarterfinalist",
"Semi-finalist", "Finalist", "Won")
df_frenchOpen <-
grand_slam_timeline %>%
mutate(outcome = as_factor(outcome)) %>%
mutate(player = str_remove_all(player, "^/* ")) %>%
filter(tournament == "French Open") %>%
group_by(player) %>%
mutate(
winner = "Won" %in% outcome,
begining = min(year),
median = median(year)
) %>%
ungroup() %>%
filter(winner == TRUE) %>%
mutate(
outcome = fct_explicit_na(outcome, "Absent"),
outcome = fct_collapse(outcome, Absent = c("Retired")),
outcome = fct_relevel(outcome, ordered_outcomes)
)
ordered_players <-
df_frenchOpen %>%
arrange(median, begining) %>%
pull(player) %>%
unique()
df_frenchOpen <- mutate(df_frenchOpen,
player = factor(player, level = ordered_players))
#### Plot ####
p <-
df_frenchOpen %>%
ggplot() +
aes(x = year, y = outcome, group = player) +
geom_line(aes(color = gender), show.legend = FALSE) +
geom_point() +
labs(x = "Year", y = "Outcome",
title = "Outcomes at Roland-Garros for {closest_state}",
caption = "Source: Wikipedia\n@_abichat for #TidyTuesday") +
theme_minimal() +
theme(plot.margin = margin(5.5, 9, 5.5, 5.5),
plot.title = element_text(face = "bold"))
anim <-
p +
transition_states(player) +
enter_fade() +
exit_fade()
animate(anim, nframes = 12 * length(ordered_players))
anim_save("plots/plot_2019-04-09.gif")