Skip to content

Instantly share code, notes, and snippets.

@kevinsoo
Last active August 19, 2018 01:47
Show Gist options
  • Select an option

  • Save kevinsoo/ac6f9575f719bcfe544e21bb5c2c116e to your computer and use it in GitHub Desktop.

Select an option

Save kevinsoo/ac6f9575f719bcfe544e21bb5c2c116e to your computer and use it in GitHub Desktop.
Script to randomize and visualize draft order for Fantasy Football League
#### Load stuff
library(tidyverse)
library(gganimate)
#### Generate data
n <- 20 # Number of iterations/steps
# Random walk for 4 players, add players as needed
df <- tibble(
ite = 0:n,
Player1 = c(0, rnorm(n, mean = 1)),
Player2 = c(0, rnorm(n, mean = 1)),
Player3 = c(0, rnorm(n, mean = 1)),
Player4 = c(0, rnorm(n, mean = 1))) %>%
gather(player, score, Player1:Player4) %>%
arrange(player, ite) %>%
group_by(player) %>%
mutate(totalScore = cumsum(score),
size = ifelse(ite == n, 6, 4)) # This is used to size the labels
#### Plot data
ggplot(df, aes(x = player, y = totalScore)) +
geom_hline(yintercept = 0, linetype ="dashed") +
geom_label(aes(label = player, fill = player, size = size)) +
theme_minimal() +
theme(legend.position = 'none') +
scale_fill_brewer(palette = "Spectral") +
labs(title = "Random walk race",
subtitle = "Iteration: {closest_state} of 20",
y = "Score total",
x = "Entity",
caption = "Random walk simulated for 20 iterations") +
transition_states(ite, transition_length = 1, state_length = 1, wrap = FALSE) +
ease_aes('linear') +
shadow_wake(wake_length = .1, wrap = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment