Created
December 29, 2015 03:54
-
-
Save jalapic/2551119da5546283a2f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
### Make Win / Winless Streaks plot | |
#Load packages | |
library(engsoccerdata) | |
library(dplyr) | |
library(ggplot2) | |
engsoccerdata2$Date <- as.Date(engsoccerdata2$Date, format="%Y-%m-%d") #make Date format | |
#Get MUFC games: | |
manutds <- rbind( | |
engsoccerdata2 %>% filter(Season!=1939) %>% filter(home=="Manchester United") %>% select(team=home, gf=hgoal, ga=vgoal, opp=visitor, tier, Date) %>% mutate(venue="H") , | |
engsoccerdata2 %>% filter(Season!=1939) %>% filter(visitor=="Manchester United") %>% select(team=visitor, gf=vgoal, ga=hgoal, opp=home, tier, Date) %>% mutate(venue="A") | |
) | |
manutds <- rbind(manutds, mu2015) #from separate file adding in 2015 data (scraped as current season not yet in engsoccerdata package) | |
#get a win/draw/loss variable and a win/no-win variable, and a year (for labels of plot) | |
manutds <- manutds %>% arrange(Date) %>% mutate(result = ifelse(gf>ga, "W", ifelse(ga>gf, "L", "D")), | |
nowin = ifelse(gf>ga, "W", "NoWin"), | |
gameno = row_number(), | |
year = lubridate::year(Date)) | |
# Get streaks | |
manutds$nowin1 <- sequence(rle(as.character(manutds$nowin))$lengths) | |
manutds$nowin1id <- rep(1:length(rle(as.character(manutds$nowin))$lengths), times=rle(as.character(manutds$nowin))$lengths) | |
# Only want the max value of each streak | |
manutds.sum <- manutds %>% group_by(nowin1id) %>% filter(nowin1==max(nowin1)) | |
# Plot - don't want to plot less than 6 game streaks | |
ggplot(data = manutds.sum %>% filter(nowin1>=6), aes(x = gameno, xend = gameno, y = 1, yend = nowin1, color=nowin)) + | |
geom_segment(lwd=1) + | |
scale_color_manual(values=c("royalblue1", "red3"))+ | |
coord_polar() + | |
scale_x_continuous(breaks = seq(min(manutds.sum$gameno), max(manutds.sum$gameno)+50, 500), | |
labels = manutds$year[seq(min(manutds.sum$gameno), max(manutds.sum$gameno)+50, 500)]) + | |
scale_y_continuous(breaks = 0:16) + ylab("")+ | |
theme_bw() + theme(panel.grid.major.x = element_line(size=1, color="gray77")) |
Author
jalapic
commented
Dec 29, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment