Last active
January 21, 2024 07:11
-
-
Save joelnitta/a865b16596ba1a821746ef9d761df24e to your computer and use it in GitHub Desktop.
Calculate 17lands win-rate by rank across sets
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
# To install magicr | |
# pak::pkg_install("joelnitta/magicr") | |
library(tidyverse) | |
library(magicr) | |
library(ggrepel) | |
# Win-rate by rank and set ---- | |
# Define function to calculate win-rate by rank for a particular set | |
get_wr_by_rank <- function(set_get) { | |
cli::cli_inform("Working on {set_get}") | |
mr_get_17lands_data( | |
set = set_get, "game", "premier", select = c("rank", "won")) %>% | |
filter(rank != "") %>% | |
filter(rank != "none") %>% | |
group_by(rank) %>% | |
summarize( | |
n_won = sum(won), | |
n_total = n() | |
) %>% | |
mutate(win_rate = n_won / n_total) %>% | |
arrange(desc(win_rate)) %>% | |
mutate(rank = as.factor(rank) %>% | |
fct_relevel( | |
c("bronze", "silver", "gold", "platinum", "diamond", "mythic")) | |
) %>% | |
mutate(set = set_get) | |
} | |
# Define vector of sets to analyze | |
sets_to_get <- c( | |
"KTK", "LCI", "WOE", "LTR", "MOM", | |
"SIR", "ONE", "BRO", "DMU", "HBG", | |
"SNC", "NEO" | |
) | |
# Calculate win-rank by set | |
wr_by_rank <- map_df(sets_to_get, get_wr_by_rank) | |
# Define where to plot set labels | |
set_labs <- wr_by_rank %>% | |
filter(rank == "mythic") | |
# Make plot | |
ggplot(wr_by_rank, aes(x = rank, y = win_rate, color = set, group = set)) + | |
geom_line() + | |
geom_point(aes(size = n_total)) + | |
geom_label_repel( | |
data = set_labs, | |
aes(label = set), | |
nudge_x = .5, direction = "y", hjust = "right" | |
) + | |
guides(color = "none") + | |
scale_size_continuous(label = scales::label_number(big.mark = ",")) + | |
scale_y_continuous(label = scales::label_percent()) + | |
labs(x = "Rank", y = "Win Rate", size = "Num. games") + | |
theme_grey(base_size = 24) | |
# Win rate by rank and day ---- | |
# Define function to calculate win-rate by rank and add day of the month | |
get_wr_by_rank_day <- function(set_get) { | |
cli::cli_inform("Working on {set_get}") | |
game_data <- mr_get_17lands_data( | |
set = set_get, "game", "premier", select = c("rank", "won", "draft_time")) | |
if (!"draft_time" %in% colnames(game_data)) return(tibble()) | |
game_data %>% | |
filter(rank != "") %>% | |
filter(rank != "none") %>% | |
mutate(day = lubridate::day(draft_time)) %>% | |
group_by(rank, day) %>% | |
summarize( | |
n_won = sum(won), | |
n_total = n(), | |
.groups = "drop" | |
) %>% | |
mutate( | |
win_rate = n_won / n_total, | |
set = set_get) | |
} | |
# Calculate win-rank by rank and day | |
wr_by_day <- map_df(sets_to_get, get_wr_by_rank_day) | |
# Aggregate across sets | |
wr_by_day_totals <- wr_by_day %>% | |
group_by(rank, day) %>% | |
summarize( | |
n_won = sum(n_won), | |
n_total = sum(n_total), | |
.groups = "drop" | |
) %>% | |
mutate(win_rate = n_won/n_total) %>% | |
mutate(rank = as.factor(rank) %>% | |
fct_relevel(c("bronze", "silver", "gold", "platinum", "diamond", "mythic")) %>% | |
fct_rev() | |
) | |
# Set labels for plot | |
rank_labs <- wr_by_day_totals %>% | |
filter(day == max(day)) | |
# Make plot | |
ggplot(wr_by_day_totals, aes(x = day, y = win_rate, color = rank, group = rank)) + | |
geom_point(aes(size = n_total)) + | |
geom_line() + | |
geom_label_repel( | |
data = rank_labs, | |
aes(label = rank), | |
nudge_x = 5, direction = "y", hjust = "right", size = 6 | |
) + | |
theme_grey(base_size = 24) + | |
scale_size_continuous(label = scales::label_number(big.mark = ",")) + | |
scale_y_continuous(label = scales::label_percent()) + | |
guides(color = "none") + | |
labs(x = "Day of the Month", y = "Win Rate", size = "Num. games") + | |
theme(legend.position = "bottom") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment