Skip to content

Instantly share code, notes, and snippets.

@JoGall
Last active September 9, 2017 09:59
Show Gist options
  • Save JoGall/57e419ab2cd3704b041e37a83f612750 to your computer and use it in GitHub Desktop.
Save JoGall/57e419ab2cd3704b041e37a83f612750 to your computer and use it in GitHub Desktop.
Compute net changes in player value for each gameweek in FPL (2016-17, 2017-18)
require(dplyr)
require(ggplot2)
# get week-by-week data for current season and last (data originally taken from official API)
d17 <- read.csv(url("https://github.com/JoGall/FantasyPL/blob/master/17-18.csv"))
d16 <- read.csv(url("https://github.com/JoGall/FantasyPL/blob/master/16-17_formatted.csv"))
# compute price changes
ss16 <- d16 %>%
group_by(player_id) %>%
mutate(price_change = value - lag(value, order_by = round)) %>%
select(name, team, value, round, price_change) %>%
arrange(team, name, round) %>%
group_by(round) %>%
summarise(n_rises = sum(price_change > 0, na.rm=T), n_drops = sum(price_change < 0, na.rm=T))
ss17 <- d17 %>%
group_by(player_id) %>%
mutate(price_change = value - lag(value, order_by = round)) %>%
select(name, team, value, round, price_change) %>%
arrange(team, name, round) %>%
group_by(round) %>%
summarise(n_rises = sum(price_change > 0, na.rm=T), n_drops = sum(price_change < 0, na.rm=T))
# price rise and net price drop each gameweek
ggplot(ss16, aes(x = round, y = n_rises / 10)) +
geom_line(col = "green") +
geom_line(aes(x = round, y = -n_drops / 10), col="red") +
geom_hline(yintercept = 0, lty = 3) +
ylab("Net price change (£m)") +
xlab("Gameweek") +
scale_x_continuous(limits = c(1, 38), breaks = c(1, 10, 20, 30, 38))
# cumulative price rise and net price drop
ggplot(ss, aes(x = round, y = cumsum(n_rises / 10))) +
geom_line(col = "green") +
geom_line(aes(x = round, y = cumsum(-n_drops / 10)), col="red") +
geom_hline(yintercept = 0, lty = 3) +
ylab("Net price change (£m)") +
xlab("Gameweek") +
scale_x_continuous(limits = c(1, 38), breaks = c(1, 10, 20, 30, 38))
# net price change each gameweek
ggplot(ss16, aes(x = round, y = (n_rises - n_drops) / 10)) +
geom_line() +
geom_hline(yintercept = 0, lty = 3) +
ylab("Net price change (£m)") +
xlab("Gameweek") +
scale_x_continuous(limits = c(1, 38), breaks = c(1, 10, 20, 30, 38))
# cumulative net price change
ggplot(ss16, aes(x = round, y = cumsum((n_rises - n_drops) / 10))) +
geom_line() +
geom_hline(yintercept = 0, lty = 3) +
ylab("Net price change (£m)") +
xlab("Gameweek") +
scale_x_continuous(limits = c(1, 38), breaks = c(1, 10, 20, 30, 38))
# compare cumulative net price change for both seasons
# add latest data (not updated API for GW4 at time of writing)
ss17 <- rbind(ss17, data.frame(round = 4, n_rises = 20, n_drops = 136))
ggplot(ss16, aes(x = round, y = cumsum((n_rises - n_drops) / 10))) +
geom_line(lwd = 1.2) +
geom_line(data = ss17, aes(x = round, y = cumsum((n_rises - n_drops) / 10)), col= "red", lwd = 1.2) +
geom_hline(yintercept = 0, lty = 3) +
ylab("Net price change (£m)") +
xlab("Gameweek") +
scale_x_continuous(limits = c(1, 38), breaks = c(1, 10, 20, 30, 38))
@JoGall
Copy link
Author

JoGall commented Sep 9, 2017

rplot2
Cumulative price rises (green) and drops (red) over 2016-17 season

rplot1
Cumulative net price change over 2016-17 (black) and 2017-18 season (red; as of 09/11/17)

rplot3
Total price rises (green) and drops (red) in each gameweek in 2016-17 season

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment