---
title: "Australian Football League (AFL) Analysis 2014-2024"
author: "Daniel Cavanagh s4118865"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
---
```{r, include=FALSE}
library(readr)
library(foreign)
library(rvest)
library(knitr)
library(dplyr)
library(stringr)
library(tidyr)
library(plotly)
library(lubridate)
library(ggplot2)
#install.packages("flexdashboard")
library(flexdashboard)
#install.packages("ggalt")
library(ggalt)
```
```{r, include=FALSE}
# Import the dataset
df <- read.csv("afl.csv", skip = 1)
df <- df[, c(1,3,4,6,7,13,14)]
df <- df[c(0:2250), ]
head(df)
df <- df %>%
mutate_if(is.character, ~ str_to_lower(.))
df <- df %>%
mutate_if(is.character, ~ str_replace_all(., "\\s+", ""))
names(df) <- gsub("\\.", "", names(df))
colnames(df) <- tolower(colnames(df))
colnames(df)
df <- na.omit(df)
# Create a new column 'result' based on the condition
df$result <- ifelse(df$homescore > df$awayscore, "homewin", "homelose")
df$result <- ifelse(df$homescore > df$awayscore, "homewin", "homelose")
df$winningteam <- ifelse(df$homescore > df$awayscore, df$hometeam, df$awayteam)
df$losingteam <- ifelse(df$homescore < df$awayscore, df$hometeam, df$awayteam)
df <- na.omit(df)
df$awayodds <- as.numeric(df$awayodds)
df$homeodds <- as.numeric(df$homeodds)
df$favourite <- ifelse(df$homeodds < df$awayodds, df$hometeam, df$awayteam)
df$nonfavourite <- ifelse(df$homeodds > df$awayodds, df$hometeam, df$awayteam)
df <- df %>%
mutate(margin = abs(homescore - awayscore))
value_counts <- table(df$winningteam)
df$favouritewin <- ifelse(df$favourite == df$winningteam, "yes", "no")
value_counts <- table(df$favouritewin)
# Convert the table to a data frame
value_counts_df <- as.data.frame(value_counts)
# Rename the columns for clarity
names(value_counts_df) <- c("favourite", "count")
value_counts_df$percentage <- (value_counts_df$count / sum(value_counts_df$count)) * 100
# Create the plot with percentage of wins
p1 <- ggplot(value_counts_df, aes(x = reorder(favourite, -percentage), y = percentage)) +
geom_bar(stat = "identity", fill = "tan4") +
scale_y_continuous(limits = c(0, 70), breaks = seq(0, 70, by = 10)) +
theme(axis.text.x = element_text(angle = 0, hjust = 1)) + # Adjust x-axis text
labs(x = "Favourite Status", y = "Percentage of Wins", title = "")
p1
# Favourite WIN PERCENTAGE COUNT
p2 <- ggplot(value_counts_df, aes(x = reorder(favourite, -count), y = count)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 0, hjust = 1)) + # Adjust x-axis text
labs(x = "favourite status", y = "Number of Wins", title = "Wins by favourite")
p2
data_encoded <- df %>%
mutate(
favouritewin = as.numeric(factor(favouritewin))
)
value_counts1 <- table(data_encoded$favouritewin)
# Calculate total margins for each year
df$date <- as.Date(df$date, format = "%Y%m%d")
total_margins_by_year <- df %>%
mutate(year = year(date)) %>% # Extract the year from the date
filter(year >= 2014 & year <= 2023) %>% # Filter for the years 2013 to 2023
group_by(year) %>%
summarise(total_margin = sum(margin))
average_margin_per_game_by_year <- df %>%
mutate(year = year(date)) %>% # Extract the year from the date
filter(year >= 2013 & year <= 2023) %>% # Filter for the years 2013 to 2023
group_by(year) %>%
summarise(average_margin = mean(margin)) # Calculate the average margin
```
```{r, include=FALSE}
value_counts <- table(df$result)
# Convert the table to a data frame
value_counts_df <- as.data.frame(value_counts)
names(value_counts_df) <- c("result", "count")
value_counts_df$percentage <- (value_counts_df$count / sum(value_counts_df$count)) * 100
# Create the plot with percentage of wins
p3 <- ggplot(value_counts_df, aes(x = reorder(result, -percentage), y = percentage)) +
geom_bar(stat = "identity", fill = "Chocolate3") + # Set fill color to light orange
scale_y_continuous(limits = c(0, 60), breaks = seq(0, 60, by = 10)) +
theme(axis.text.x = element_text(angle = 0, hjust = 1)) + # Adjust x-axis text
labs(x = "Status", y = "Percentage of Wins", title = "")
p3
```
```{r, include=FALSE}
# graph in descending order by count
value_counts <- table(df$winningteam)
# Count the occurrences of each team in the winningteam column
value_counts <- table(df$winningteam)
# Convert the table to a data frame
value_counts_df <- as.data.frame(value_counts)
# Rename the columns for clarity
names(value_counts_df) <- c("team", "count")
# Create the plot
p4 <- ggplot(value_counts_df, aes(x = reorder(team, -count), y = count)) +
geom_bar(stat = "identity", fill = "blue") + # Set fill color to blue
scale_y_continuous(limits = c(0, 180), breaks = seq(0, 180, by = 30)) + # Set y-axis limits and breaks
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) + # Adjust x-axis text
labs(x = "Team", y = "Number of Wins", title = "")
p4 <- ggplot(value_counts_df, aes(x = reorder(team, -count), y = count, group = 1)) +
geom_line(color = "blue") + # Use geom_line for the line graph
geom_point() + # Optionally add points to the line graph for emphasis
scale_y_continuous(limits = c(0, 175), breaks = seq(0, 175, by = 25)) + # Set y-axis limits and breaks
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) + # Adjust x-axis text
labs(x = "Team", y = "Number of Wins", title = "") # Keep title empty as specified
p4
```
```{r, include=FALSE}
library(dplyr)
library(ggplot2)
# Ensure the homescore column is numeric
df$homescore <- as.numeric(df$homescore)
# Calculate the average points per game for each home team
summary_df <- df %>%
group_by(hometeam) %>%
summarise(
total_points = sum(homescore),
games_played = n(),
average_points_per_game = total_points / games_played
)
# Create the plot
p5 <- ggplot(summary_df, aes(x = reorder(hometeam, -average_points_per_game), y = average_points_per_game)) +
geom_bar(stat = "identity", fill = "green4") + # Set fill color to green
scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 20)) +
labs(x = "Home Team", y = "Average Points per Game", title = "Average Points per Game by Home Team") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
print(p5)
```
```{r, include=FALSE}
df$awayscore <- as.numeric(df$awayscore)
# Ensure the awayscore column is numeric
df$awayscore <- as.numeric(df$awayscore)
# Calculate the away_average points per game for each away team
summary_df <- df %>%
group_by(awayteam) %>%
summarise(
total_points = sum(awayscore),
games_played = n(),
away_average_points_per_game = total_points / games_played
)
# Create the plot
p6 <- ggplot(summary_df, aes(x = reorder(awayteam, -away_average_points_per_game), y = away_average_points_per_game)) +
geom_bar(stat = "identity", fill = "purple2") +
scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 10)) +
labs(x = "Away Team", y = "Away Team Average Points per Game", title = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
# Print the plot
print(p6)
```
```{r, include=FALSE}
library(dplyr)
library(ggplot2)
library(ggalt) # Ensure you have ggalt installed for geom_dumbbell
# Convert necessary columns to numeric
df$homescore <- as.numeric(df$homescore)
df$awayscore <- as.numeric(df$awayscore)
# Calculate average points per game for home and away games
home_summary <- df %>%
group_by(hometeam) %>%
summarise(avg_home_points = mean(homescore))
away_summary <- df %>%
group_by(awayteam) %>%
summarise(avg_away_points = mean(awayscore))
# Combine home and away summaries
summary_df <- full_join(home_summary, away_summary, by = c("hometeam" = "awayteam"))
# Convert team to a factor for correct ordering
summary_df$hometeam <- factor(summary_df$hometeam, levels = as.character(summary_df$hometeam))
# Create the dumbbell plot
p7 <- ggplot(summary_df, aes(x = avg_home_points, xend = avg_away_points, y = hometeam, group = hometeam)) +
geom_dumbbell(color = "#a3c4dc",
size = 0.75,
point.colour.l = "#0e668b") +
scale_x_continuous(limits = c(60, 100), breaks = seq(60, 100, by = 10)) +
labs(x = "Average Points",
y = NULL,
title = "Average Points Per Game",
subtitle = "Comparison of Performance at Home and Away By Team",
caption = "Source: ()") +
theme(plot.title = element_text(hjust = 0.5, face = "bold"),
plot.background = element_rect(fill = "#f7f7f7"),
panel.background = element_rect(fill = "#f7f7f7"),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(),
axis.ticks = element_blank(),
legend.position = "top",
panel.border = element_blank())
p7
```
```{r, include=FALSE}
# Calculate win percentage for each team when they are the away team
# Calculate away wins and total away games
away_wins <- df %>%
filter(awayteam == winningteam) %>%
group_by(awayteam) %>%
summarise(wins = n())
total_away_games <- df %>%
group_by(awayteam) %>%
summarise(total_games = n())
# Calculate win percentage and adjust for plotting
win_percentage <- left_join(away_wins, total_away_games, by = "awayteam") %>%
mutate(win_pct = (wins / total_games) * 100,
win_pct_adjusted = win_pct - 50, # Center around 50%
win_type = ifelse(win_pct > 50, "above", "below"))
# Sort and prepare for plotting
win_percentage <- win_percentage[order(win_percentage$win_pct_adjusted), ]
win_percentage$awayteam <- factor(win_percentage$awayteam, levels = win_percentage$awayteam)
# Create the diverging bar chart
p8 <- ggplot(win_percentage, aes(x = awayteam, y = win_pct_adjusted, label = round(win_pct, 1))) +
geom_bar(stat = 'identity', aes(fill = win_type), width = 0.5) +
scale_fill_manual(name = "Win Percentage",
labels = c("Above 50%", "Below 50%"),
values = c("above" = "#00ba38", "below" = "#f8766d")) +
labs(subtitle = "",
title = "",
y = "Win percentage relative to 50%") + # Change y-axis label
coord_flip() +
geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
theme(legend.position = "none") # Remove the legend
gg1 <- ggplotly(p8, width = 550, height = 480)
gg1
```
```{r, include=FALSE}
# Calculate win percentage for each team when they are the home team
# Calculate home wins and total home games
home_wins <- df %>%
filter(hometeam == winningteam) %>%
group_by(hometeam) %>%
summarise(wins = n())
total_home_games <- df %>%
group_by(hometeam) %>%
summarise(total_games = n())
# Calculate win percentage and adjust for plotting
win_percentage <- left_join(home_wins, total_home_games, by = "hometeam") %>%
mutate(win_pct = (wins / total_games) * 100,
win_pct_adjusted = win_pct - 50, # Center around 50%
win_type = ifelse(win_pct > 50, "above", "below"))
# Sort and prepare for plotting
win_percentage <- win_percentage[order(win_percentage$win_pct_adjusted), ]
win_percentage$hometeam <- factor(win_percentage$hometeam, levels = win_percentage$hometeam)
# Create the diverging bar chart
p8 <- ggplot(win_percentage, aes(x = hometeam, y = win_pct_adjusted, label = round(win_pct, 1))) +
geom_bar(stat = 'identity', aes(fill = win_type), width = 0.5) +
scale_fill_manual(name = "Win Percentage",
labels = c("Above 50%", "Below 50%"),
values = c("above" = "#00ba38", "below" = "#f8766d")) +
labs(subtitle = "",
title = "",
y = "Win percentage relative to 50%") + # Change y-axis label
coord_flip() +
geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
theme(legend.position = "none") # Remove the legend
gg12 <- ggplotly(p8, width = 550, height = 480)
gg12
```
```{r, include=FALSE}
# Calculate home wins and total home games
home_wins <- df %>%
filter(hometeam == winningteam) %>%
group_by(hometeam) %>%
summarise(wins = n())
total_home_games <- df %>%
group_by(hometeam) %>%
summarise(total_games = n())
# Calculate win percentage and adjust for plotting
win_percentage <- left_join(home_wins, total_home_games, by = "hometeam") %>%
mutate(win_pct = (wins / total_games) * 100,
win_pct_adjusted = win_pct - 50, # Center around 50%
win_type = ifelse(win_pct > 50, "above", "below"))
# Sort and prepare for plotting
win_percentage <- win_percentage[order(win_percentage$win_pct_adjusted), ]
win_percentage$hometeam <- factor(win_percentage$hometeam, levels = win_percentage$hometeam)
# Create the diverging bar chart
p9 <- ggplot(win_percentage, aes(x = hometeam, y = win_pct_adjusted, label = round(win_pct, 1))) +
geom_bar(stat = 'identity', aes(fill = win_type), width = 0.5) +
scale_fill_manual(name = "Win Percentage",
labels = c("Above 50%", "Below 50%"),
values = c("above" = "#00ba38", "below" = "#f8766d")) +
labs(subtitle = "Win Percentage when Home Team",
title = "",
y = "Win percentage relative to 50%") + # Change y-axis label
coord_flip() +
geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
theme(legend.position = "none") # Remove the legend
gg2 <- ggplotly(p9, width = 550, height = 480)
gg2
```
```{r, include=FALSE}
# Calculate total wins and total games for each team
home_wins <- df %>%
filter(hometeam == winningteam) %>%
group_by(hometeam) %>%
summarise(wins = n())
away_wins <- df %>%
filter(awayteam == winningteam) %>%
group_by(awayteam) %>%
summarise(wins = n())
total_home_games <- df %>%
group_by(hometeam) %>%
summarise(total_games = n())
total_away_games <- df %>%
group_by(awayteam) %>%
summarise(total_games = n())
# Combine wins and games to calculate overall win percentage
combined_wins <- full_join(home_wins, away_wins, by = c("hometeam" = "awayteam"), suffix = c("_home", "_away")) %>%
mutate(total_wins = coalesce(wins_home, 0) + coalesce(wins_away, 0))
combined_games <- full_join(total_home_games, total_away_games, by = c("hometeam" = "awayteam"), suffix = c("_home", "_away")) %>%
mutate(total_games = total_games_home + total_games_away)
win_percentage <- left_join(combined_wins, combined_games, by = "hometeam") %>%
mutate(win_pct = (total_wins / total_games) * 100,
win_pct_adjusted = win_pct - 50,
win_type = ifelse(win_pct > 50, "above", "below"))
# Sort and prepare for plotting
win_percentage <- win_percentage[order(win_percentage$win_pct_adjusted), ]
win_percentage$hometeam <- factor(win_percentage$hometeam, levels = win_percentage$hometeam)
# Create the diverging bar chart
p10 <- ggplot(win_percentage, aes(x = hometeam, y = win_pct_adjusted, label = round(win_pct, 1))) +
geom_bar(stat = 'identity', aes(fill = win_type), width = 0.5) +
scale_fill_manual(name = "Win Percentage",
labels = c("Above 50%", "Below 50%"),
values = c("above" = "#00ba38", "below" = "#f8766d")) +
labs(subtitle = "Combined Win Percentage (Home and Away)",
title = NULL, # Remove the title
x = "Team", # Change x-axis label to "Team"
y = "Win percentage relative to 50%") + # Change y-axis label
coord_flip() +
geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
theme(legend.position = "none") # Remove the legend
gg3 <- ggplotly(p10, width = 450, height = 480)
gg3
```
```{r, include=FALSE}
# Calculate the total times each team was the favourite
total_homewins <- df %>%
group_by(hometeam) %>%
summarise(total_count = n())
# Calculate the number of wins when the team was the favourite
wins_when_hometeam <- df %>%
filter(winningteam == hometeam) %>%
group_by(winningteam) %>%
summarise(win_count = n())
# Combine the data to calculate win percentage
win_percentage <- wins_when_hometeam %>%
left_join(total_homewins, by = c("winningteam" = "hometeam")) %>%
mutate(win_percentage = (win_count / total_count) * 100)
p11 <- ggplot(win_percentage, aes(x = reorder(winningteam, -win_percentage), y = win_percentage)) +
geom_bar(stat = "identity", fill = "green4") +
geom_hline(yintercept = 50, linetype = "dashed", color = "red") +
scale_y_continuous(limits = c(0, 80), breaks = seq(0, 80, by = 20)) +
labs(x = "Home Team", y = "Win Percentage when home team", title = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
# Print the plot
print(p11)
```
```{r, include=FALSE}
# Calculate the total games for each away team
total_away_games <- df %>%
group_by(awayteam) %>%
summarise(total_count = n())
# Calculate the number of wins when the team was the away team
wins_when_awayteam <- df %>%
filter(winningteam == awayteam) %>%
group_by(awayteam) %>%
summarise(win_count = n())
# Combine the data to calculate win percentage
win_percentage <- left_join(wins_when_awayteam, total_away_games, by = "awayteam") %>%
mutate(win_percentage = (win_count / total_count) * 100)
# Plot the away team win percentage
p12 <- ggplot(win_percentage, aes(x = reorder(awayteam, -win_percentage), y = win_percentage)) +
geom_bar(stat = "identity", fill = "purple2") +
geom_hline(yintercept = 50, linetype = "dashed", color = "red") + # Add horizontal line at 50%
scale_y_continuous(limits = c(0, 80), breaks = seq(0, 80, by = 20)) +
labs(x = "Away Team", y = "Win Percentage when Away Team", title = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
```
```{r, include=FALSE}
# Filter the dataset where winningteam is the same as favourite and count the instances
count_same_team <- df %>%
filter(winningteam == favourite) %>%
summarise(count = n())
# View the count
print(count_same_team)
count_same_team <- df %>%
filter(winningteam == favourite) %>%
group_by(winningteam) %>%
summarise(count = n())
p13 <- ggplot(count_same_team, aes(x = reorder(winningteam, -count), y = count)) +
geom_bar(stat = "identity", fill = "blue") + # Set fill color to blue
scale_y_continuous(limits = c(0, 125), breaks = seq(0, 125, by = 25)) +
labs(x = "Team", y = "Number of Wins", title = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
p13
```
```{r, include=FALSE}
# Calculate the total times each team was the favourite
total_favourite <- df %>%
group_by(favourite) %>%
summarise(total_count = n())
# Calculate the number of wins when the team was the favourite
wins_when_favourite <- df %>%
filter(winningteam == favourite) %>%
group_by(winningteam) %>%
summarise(win_count = n())
# Combine the data to calculate win percentage
win_percentage <- wins_when_favourite %>%
left_join(total_favourite, by = c("winningteam" = "favourite")) %>%
mutate(win_percentage = (win_count / total_count) * 100)
p14 <- ggplot(win_percentage, aes(x = reorder(winningteam, -win_percentage), y = win_percentage)) +
geom_bar(stat = "identity") +
scale_y_continuous(limits = c(0, 80), breaks = seq(0, 80, by = 20)) +
labs(x = "Team", y = "Win Percentage when Favourite", title = "Win Percentage by Favourite Team") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
# Print the plot
print(p14)
```
```{r, include=FALSE}
count_same_team <- df %>%
filter(winningteam == nonfavourite) %>%
summarise(count = n())
# View the count
print(count_same_team)
count_same_team <- df %>%
filter(winningteam == nonfavourite) %>%
group_by(winningteam) %>%
summarise(count = n())
p15 <- ggplot(count_same_team, aes(x = reorder(winningteam, -count), y = count)) +
geom_bar(stat = "identity", fill = "dodgerblue2") + # Use geom_bar and set fill color to blue
scale_y_continuous(limits = c(0, 50), breaks = seq(0, 50, by = 10)) +
labs(x = "Team", y = "Number of Wins", title = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
gg4 <- ggplotly(p15, width = 600, height = 500)
gg4
```
```{r, include=FALSE}
total_nonfavourite <- df %>%
group_by(nonfavourite) %>%
summarise(total_count = n())
# Calculate the number of wins when the team was the favourite
wins_when_nonfavourite <- df %>%
filter(winningteam == nonfavourite) %>%
group_by(winningteam) %>%
summarise(win_count = n())
# Combine the data to calculate win percentage
win_percentage <- wins_when_nonfavourite %>%
left_join(total_nonfavourite, by = c("winningteam" = "nonfavourite")) %>%
mutate(win_percentage = (win_count / total_count) * 100)
p16 <- ggplot(win_percentage, aes(x = reorder(winningteam, -win_percentage), y = win_percentage, group = 1)) +
geom_line(color = "blue") +
geom_point() + # Optionally add points to the line graph for emphasis
scale_y_continuous(limits = c(0, 60), breaks = seq(0, 60, by = 10)) +
labs(x = "Team", y = "Win Percentage (%)", title = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
gg5 <- ggplotly(p16, width = 600, height = 480)
gg5
```
```{r, include=FALSE}
total_favourite <- df %>%
group_by(favourite) %>%
summarise(total_count = n())
# Calculate the number of wins when the team was the favourite
wins_when_favourite <- df %>%
filter(winningteam == favourite) %>%
group_by(winningteam) %>%
summarise(win_count = n())
# Combine the data to calculate win percentage
win_percentage <- wins_when_favourite %>%
left_join(total_favourite, by = c("winningteam" = "favourite")) %>%
mutate(win_percentage = (win_count / total_count) * 100)
p45 <- ggplot(win_percentage, aes(x = reorder(winningteam, -win_percentage), y = win_percentage, group = 1)) +
geom_line(color = "blue") +
geom_point() + # Optionally add points to the line graph for emphasis
scale_y_continuous(limits = c(40, 90), breaks = seq(40, 90, by = 10)) +
labs(x = "Team", y = "Win Percentage (%)", title = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
gg25 <- ggplotly(p45, width = 600, height = 480)
gg25
```
```{r, include=FALSE}
# Convert necessary columns to numeric
df$homescore <- as.numeric(df$homescore)
df$awayscore <- as.numeric(df$awayscore)
# Calculate average points per game when favourite and non-favourite
favourite_summary <- df %>%
filter(hometeam == favourite | awayteam == favourite) %>%
group_by(favourite) %>%
summarise(avg_favourite_points = mean(homescore * (hometeam == favourite) + awayscore * (awayteam == favourite)))
nonfavourite_summary <- df %>%
filter(hometeam == nonfavourite | awayteam == nonfavourite) %>%
group_by(nonfavourite) %>%
summarise(avg_nonfavourite_points = mean(homescore * (hometeam == nonfavourite) + awayscore * (awayteam == nonfavourite)))
# Combine favourite and non-favourite summaries
summary_df <- full_join(favourite_summary, nonfavourite_summary, by = c("favourite" = "nonfavourite"))
# Convert team to a factor for correct ordering
summary_df$favourite <- factor(summary_df$favourite, levels = as.character(summary_df$favourite))
# Create the dumbbell plot
p17 <- ggplot(summary_df, aes(x = avg_favourite_points, xend = avg_nonfavourite_points, y = favourite, group = favourite)) +
geom_dumbbell(color = "#a3c4dc",
size = 0.75,
point.colour.l = "#0e668b") +
scale_x_continuous(limits = c(60, 100), breaks = seq(60, 100, by = 10)) +
labs(x = "Average Points",
y = NULL,
title = "Average Points Per Game",
subtitle = "Comparison of Performance-Favourite and Non-Favourite By Team",
caption = "Source: ()") +
theme(plot.title = element_text(hjust = 0.5, face = "bold"),
plot.background = element_rect(fill = "#f7f7f7"),
panel.background = element_rect(fill = "#f7f7f7"),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(),
axis.ticks = element_blank(),
legend.position = "top",
panel.border = element_blank())
# Print the plot
print(p17)
```
Favourites
=======================================================================
Column
-------------------------------------
### Favourite vs Non-Favourite Overall Record Since 2014
```{r}
gg6 <- ggplotly(p1, width = 600, height = 450)
gg6
```
### Total Wins By Teams When Favourite
```{r}
gg25 <- ggplotly(p45, width = 600, height = 450)
gg25
```
Non-Favourites
=======================================================================
Column
-------------------------------------
### Total Wins when Non-Favourite Team
```{r}
gg4
```
### Winning Percentage when Non-Favourite Team
```{r}
gg5
```
Home Teams
=======================================================================
Column
-------------------------------------
### Overall win-loss Records of Home teams 2014-2024
```{r}
p3
```
-------------------------------------
### Average Points per Game by Home Team 2014-2024
```{r}
p5
```
### Home Ground Advantage Since 2014
```{r}
p11
```
Away Teams
=======================================================================
Column
-------------------------------------
### Average Points per Game by Away Team 2014-2024
```{r}
p6
```
### Away Teams Win Percentage since 2014
```{r}
p12
```
Home and Away Winning records
=======================================================================
Column
-------------------------------------
### Win Percentage when Home Team
```{r}
gg2
```
### Win Percentage when Away Team
```{r}
gg1
```
Point Differentials
=======================================================================
Column {.tabset}
-------------------------------------
### Tab 1
```{r}
p7
```
### Tab 2
```{r}
p17
```
Totals
=======================================================================
Inputs {.sidebar}
-------------------------------------
```{r}
"Between 2014 and 2024, a clear correlation has emerged in the AFL, indicating that home teams tend to win more games compared to teams playing away. This trend underscores the significant advantage that playing on familiar turf provides, potentially due to factors such as crowd support, reduced travel fatigue, and greater familiarity with the playing conditions. Additionally, teams that are favoured to win—often based on expert analyses and betting odds—consistently outperform their non-favoured counterparts. This suggests that pre-game predictions, which consider team strength, player form, and other critical variables, are generally accurate in forecasting game outcomes. Together, these correlations highlight the dual impact of home-field advantage and pre-game favouritism in shaping AFL game results over this decade."
```
Column
-------------------------------------
### Win Percentage By Team For All Games Since 2014
```{r}
gg3
```
### Total Wins By Team Since 2014
```{r}
gg6 <- ggplotly(p4, width = 540, height = 500)
gg6
```