-
Notifications
You must be signed in to change notification settings - Fork 0
/
R Code for League Average Threes Attempted by Season GIF
36 lines (31 loc) · 1.49 KB
/
R Code for League Average Threes Attempted by Season GIF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
library (ggplot2)
library (gganimate)
library (dplyr)
library (readxl)
# loading in data
nbaPerGameStats <- read_xlsx('nbaTeamStatsSeasons.xlsx')
# Creating variables for the horizontal & vertical lines which will show league averages for that specific season.
nbaPerGameStats
str(nbaPerGameStats)
str(g)
g <- nbaPerGameStats %>%
group_by(Season) %>%
mutate(mean_threesAttempted = mean(threesAttempted),
mean_AST = mean(AST),
mean_TS = mean(TS),
mean_PTS = mean(PTS))
# building the graph
threePA <- ggplot(g, aes(x = Season, y = mean_threesAttempted, col = mean_TS)) +
geom_point(size = 3) +
scale_color_gradient(low = 'green', high = 'red') +
theme(legend.title = element_text(), legend.background = element_rect(fill = 'snow2'),
plot.background = element_rect(fill = "snow2"),
panel.background = element_rect(fill = 'snow2'), plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5)) +
xlab('Season') +
ylab('League Average Three Pointers Attempted per Game') + labs(color = 'AVG TS%') +
labs(caption = 'Data via basketball-reference.com', title = 'League Average Three Pointers Attempted by Season')
plot(threePA)
animated_threePA <- threePA + transition_time(as.integer(Season)) + labs(subtitle = 'Season: {frame_time}') + shadow_mark(past = TRUE)
animate(animated_threePA, nframes = 300, end_pause = 30)
anim_save("League Average Three Pointers Attempted by Season.gif", animated_threePA, nframes = 300, end_pause = 30)