Created
July 5, 2022 11:41
-
-
Save jvieroe/3aed13e4b46e27b1e152a6224b023b2a to your computer and use it in GitHub Desktop.
tennis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list = ls()) | |
library(tidyverse) | |
library(lubridate) | |
players <- read_csv("https://raw.githubusercontent.com/JeffSackmann/tennis_atp/master/atp_players.csv") | |
decades <- c("70", "80", "90", "00", "10", "20") | |
paths <- paste0("https://raw.githubusercontent.com/JeffSackmann/tennis_atp/master/atp_rankings_", | |
decades, | |
"s.csv") %>% | |
as.list() | |
rm(decades) | |
paths | |
atp <- map_dfr(.x = paths, | |
.f = read_csv) | |
rm(paths) | |
atp <- atp %>% | |
mutate(date = parse_date_time(as.character(ranking_date), | |
orders = "ymd")) | |
atp | |
atp <- atp %>% | |
tidylog::left_join(., | |
players, | |
by = c("player" = "player_id")) | |
rm(players) | |
atp <- atp %>% | |
mutate(name_full = paste(name_first, name_last)) | |
min_rank <- min(atp$rank) | |
max_rank <- max(atp$rank) | |
atp %>% | |
filter(name_full == "Novak Djokovic") %>% | |
ggplot(., | |
aes(x = date, | |
y = rank)) + | |
geom_line() + | |
ylim(max_rank, min_rank) | |
atp <- atp %>% | |
group_by(player) %>% | |
mutate(min_rank = min(rank), | |
max_rank = max(rank)) %>% | |
ungroup() | |
atp %>% | |
filter(min_rank == 10) %>% | |
ggplot(., | |
aes(x = date, | |
y = rank)) + | |
geom_line(aes(color = factor(name_full))) + | |
ylim(100, min_rank) | |
atp %>% | |
filter(date >= "2000-01-01") %>% | |
filter(name_full %in% c("Novak Djokovic", | |
"Rafael Nadal", | |
"Roger Federer")) %>% | |
ggplot(., | |
aes(x = date, | |
y = rank)) + | |
geom_line(aes(color = name_full)) + | |
ylim(max_rank, min_rank) | |
big_three <- atp %>% | |
filter(date >= "2000-01-01") %>% | |
filter(name_full %in% c("Novak Djokovic", | |
"Rafael Nadal", | |
"Roger Federer")) | |
others <- atp %>% | |
filter(date >= "2000-01-01") %>% | |
filter(!name_full %in% c("Novak Djokovic", | |
"Rafael Nadal", | |
"Roger Federer")) %>% | |
filter(min_rank <= 100) | |
min_rank <- 1 | |
max_rank <- max(others$rank) | |
ggplot() + | |
geom_line(data = others, aes(x = date, | |
y = rank, | |
group = player), | |
size = .001, color = "grey90") + | |
ylim(max_rank, min_rank) + | |
theme_bw() | |
top10 <- atp %>% | |
filter(rank < 11) %>% | |
filter(date >= "2000-01-01") | |
min_rank <- 1 | |
max_rank <- max(top10$rank) | |
ggplot() + | |
geom_line(data = top10, aes(x = date, | |
y = rank, | |
group = player), | |
size = .001, color = "grey90") + | |
ylim(max_rank, min_rank) + | |
theme_bw() | |
top10 <- top10 %>% | |
arrange(player, date) %>% | |
group_by(player) %>% | |
mutate(rank_roll = zoo::rollmean(rank, | |
k = 5, | |
fill = NA, | |
align = "right")) %>% | |
ungroup() | |
top10 %>% | |
filter(date >= "2000-01-01") %>% | |
ggplot() + | |
geom_line(data = top10, aes(x = date, | |
y = rank_roll, | |
group = player), | |
size = .001, color = "grey90") + | |
ylim(max_rank, min_rank) + | |
theme_bw() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment