B Data visualization with ggplot
First let’s prep our data set using our code from the last section.
Code
knitr::opts_chunk$set(class.source = "fold-show")
library(tidyverse)
library(pubtheme)
restore.ggplot.defaults()
d = readRDS('data/games.rds')
d = d %>%
filter(lg == 'nba',
season %in% 2022) %>%
select(date, gid,
away, home,
ascore, hscore,
season, season.type) %>%
mutate(season.type = factor(season.type,
levels = c('reg', 'post')))
da = d %>% select(date, gid,
away, ascore,
home, hscore,
season, season.type) %>%
mutate(ha = 'away')
dh = d %>% select(date, gid,
home, hscore,
away, ascore,
season, season.type) %>%
mutate(ha = 'home')
cols = c('date', 'gid',
'team', 'score',
'opp', 'opp.score',
'season', 'season.type', 'ha')
colnames(da) = cols
colnames(dh) = cols
dd = bind_rows(da, dh) %>%
arrange(date)
head(dd,2)
date gid team score opp opp.score season season.type ha
1 2021-10-19 22100001 BKN 104 MIL 127 2022 reg away
2 2021-10-19 22100002 GSW 121 LAL 114 2022 reg away