This is how I went about doing my assignment #2. blah, blah, blah
This is the 2nd paragraph.
my_path <- "C:/Users/pptallon/Dropbox/G/Teaching/Loyola College/IS470 Sports Analytics/"
setwd(my_path)
library(data.table)
library(ggplot2)
library(dplyr)
library(tidytext)
library(RColorBrewer)
df <- fread("GamesGithub.csv")
cols_to_use <- c("season", "away_team", "away_score", "home_team", "home_score")
df <- subset(df, select = cols_to_use)
df$winner <- ifelse(df$away_score > df$ home_score, df$away_team, df$home_team)
df$loser <- ifelse(df$away_score > df$ home_score, df$home_team, df$away_team)
df <- na.omit(df)
df$score_diff <- abs(df$away_score - df$home_score)
winners <- df %>%
mutate(team = winner) %>%
group_by(season, team) %>%
summarise(win_count = n(), .groups = "keep") %>%
data.frame()
losers <- df %>%
mutate(team = loser) %>%
group_by(season, team) %>%
summarise(lose_count = n(), .groups = "keep") %>%
data.frame()
df1 <- merge(x = winners, y = losers, by=c("season", "team"), all=TRUE)
df1[is.na(df1)] <- 0
df2 <- df1 %>%
mutate(team = recode(team, 'OAK' = 'LV', 'SD' = 'LAC', 'STL' = 'LA') ) %>%
group_by(team) %>%
summarise(wins = sum(win_count), losses = sum(lose_count), .groups = 'keep') %>%
mutate(total_games = wins + losses,
win_pc = round(100*wins/total_games,0),
loss_pc = round(100*losses/total_games,0)) %>%
data.frame()
ggplot(df2, aes(x = reorder(team, -wins), y = wins, fill = win_pc)) +
geom_bar(stat="identity") +
labs(x = "Team", y = "Win Count", title = "Wins by Team", fill = "Win Percentage") +
geom_text(aes(label = paste0(win_pc,"%")), vjust = -0.5 ) +
scale_fill_continuous(
limits = c(31, 80),
labels=paste0(seq(30, 80, 10), "%"),
breaks = seq(30, 80, 10),
low = "red",
high = "dark green") +
theme(plot.title = element_text(hjust=0.5))
More paragraphs of text.
Etc.
df2 <- df1 %>%
filter(season >= 2019 & season <= 2021) %>%
mutate(team = recode(team, 'OAK' = 'LV', 'SD' = 'LAC', 'STL' = 'LA') ) %>%
group_by(season) %>%
mutate( total = win_count + lose_count,
win_pc = round(100*win_count/total,0),
loss_pc = round(100*lose_count/total,0),
team = reorder_within(team, win_count, season),
team_abbr = substr(team, 0, nchar(as.character(team))-7),
rank = rank(-win_pc, ties.method = "random" )) %>%
data.frame()
ggplot(df2, aes(x = reorder(team, -win_count), y = win_count, fill = win_pc)) +
geom_bar(stat="identity") +
geom_text(aes(label = paste0(win_pc,"%")), vjust = -0.5, size = 3 ) +
labs(x = "Team", y = "Win Count", title = "Wins by Team", fill = "Win Percentage") +
scale_y_continuous(limits = c(0, max(df2$win_count)*1.1 ) ) +
scale_x_reordered() +
scale_fill_continuous(
limits = c(1, 90),
labels=paste0(seq(0, 90, 10), "%"),
breaks = seq(0, 90, 10),
low = "red",
high = "dark green") +
facet_wrap(ncol = 1, nrow = 8, ~season, scales='free') +
theme(plot.title = element_text(hjust=0.5))
cols <- colorRampPalette( brewer.pal(8, "Set2") )
myPal <- cols(32)
ggplot(df2, aes(x = season, y = rank, group = team_abbr)) +
geom_line(aes(color = team_abbr), size = 2 ) +
geom_point(shape = 21, size = 4, fill = "white") +
scale_y_reverse( breaks = seq(max(df2$rank), 1, -1)) +
geom_text(data = df2 %>% filter(season == min(season)),
aes(x = season - 0.05,
y = rank,
label = team_abbr),
size=3,
hjust=1) +
geom_text(data = df2 %>% filter(season == max(season)),
aes(x = season + 0.05,
y = rank,
label = team_abbr),
size=3,
hjust=0) +
scale_x_continuous( breaks = min(df2$season):max(df2$season),
labels = as.character(min(df2$season):max(df2$season))) +
scale_color_manual(values = myPal) +
labs( title = "Bump Chart for Teams by Win Percentage",
x = "Season",
y = "Rank",
colour = "Teams") +
theme(plot.title = element_text(hjust=0.5))
More stuff goes here if u want it.
You can add captions at the bottom of images. To add a caption, include the words fig.cap=“blah blah” inside the {….} at the top of the RMarkdown code you are using to include the image. You will notice here that I can also add lines of HTML code directly into the text. Here’s something bold, something italics, something underline, and something blue.
knitr::include_graphics("c:/Users/pptallon/Dropbox/G/Personal/Tallon005.jpg")
Courtesy of your favorite IT professor