Skip to content

Instantly share code, notes, and snippets.

@kevindesai777
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevindesai777/5f13826cc9293aa0f245 to your computer and use it in GitHub Desktop.
Save kevindesai777/5f13826cc9293aa0f245 to your computer and use it in GitHub Desktop.
The entire code for the blog post
#Grouping the matches by season
df_season <- df %>% #%>% is a special operator which translates to 'then' in English.
group_by(Season) %>%
summarise(HomeGoals = sum(hgoal), AwayGoals = sum(vgoal), HomeWins = sum(result == 'H'), AwayWins = sum(result =='A'), Draws = sum(result == 'D'))
#Grouping the matches by full time scores
df_scoreline <- df %>%
group_by(FT) %>%
summarise(count=n()) %>%
arrange(desc(count))
#Calculating the total, home and away goals
total_goals <- sum(df$totgoal)
home_goals <- sum(df$hgoal)
away_goals <- sum(df$vgoal)
# Making plots for Home,Away win percentage and draw percentage
ggplot(df_season, aes(x=Season, y=(HomeWins*100)/(HomeWins+AwayWins+Draws))) +
geom_point(size = 3,position="jitter") +
geom_smooth(stat = "smooth", position = "identity", color="red", size=1.14) +
ggtitle("Home Win Percentage")+
xlab("Year")+
ylab("Win Percentage") +
theme_gray()
ggplot(df_season, aes(x=Season, y=(AwayWins*100)/(HomeWins+AwayWins+Draws))) +
geom_point(size = 3,position="jitter") +
geom_smooth(stat = "smooth", position = "identity", color="red", size=1.14) +
scale_x_continuous(limits=c(1890,2010)) +
scale_y_continuous(limits=c(15,35))+
ggtitle("Away Win Percentage")+
xlab("Year")+
ylab("Win Percentage") +
theme_gray()
ggplot(df_season, aes(x=Season, y=(Draws*100)/(HomeWins+AwayWins+Draws))) +
geom_point(size = 3,position="jitter", color="black") +
geom_smooth(stat = "smooth", position = "identity", color="red", size=1.14) +
scale_x_continuous(limits=c(1890,2010)) +
scale_y_continuous(limits=c(15,35))+
ggtitle("Draw Percentage")+
xlab("Year")+
ylab("Draw Percentage")
# Calculating home and away goals
df_season$AverageHomeGoalsPerGame = df_season$HomeGoals/(df_season$HomeWins + df_season$AwayWins + df_season$Draws)
df_season$AverageAwayGoalsPerGame = df_season$AwayGoals/(df_season$HomeWins + df_season$AwayWins + df_season$Draws)
# Plotting the average home and away goals
ggplot(df_season, aes(x=Season, y=AverageHomeGoalsPerGame)) +
ggtitle("Average Home Goals")+
xlab("Year") +
ylab("Average Goals Per Game") +
geom_smooth(stat= "identity", color="orange") +
scale_x_continuous(limits=c(1890,2010))
ggplot(df_season, aes(x=Season, y=AverageAwayGoalsPerGame)) +
ggtitle("Average Away Goals")+
xlab("Year") +
ylab("Average Goals Per Game") +
geom_smooth(stat= "identity", color="orange") +
scale_x_continuous(limits=c(1890,2010))
#Average Goals per Game
ggplot(df_season,aes(x=Season, y=AGPG)) +
scale_x_continuous(limits=c(1890,2010))+
geom_line(size=2, color="purple") +
ggtitle("Average Goals Over The Years")+
ylab("Average Goals Per Game")+
xlab("Years")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment