-
Notifications
You must be signed in to change notification settings - Fork 0
/
R Code for Rookie Avg GIFS
73 lines (63 loc) · 3.15 KB
/
R Code for Rookie Avg GIFS
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
library (ggplot2)
library (gganimate)
library (dplyr)
library (readxl)
# loading in data
nbaRookieStats <- read_xlsx('2009-2018 rookie stats data.xlsx')
# creating average field columns for each season
nbaRookieStats
str(nbaRookieStats)
str(g)
g <- nbaRookieStats %>%
group_by(DraftYear) %>%
mutate(mean_threesAttempted = mean(threePointersAttempted),
mean_threesMade = mean(threePointersMade),
mean_TS = mean(TSPercentage),
mean_PPG = mean(PPG))
g
# building the traditional point gif - 3PA
j <- ggplot(g, aes(x = DraftYear, 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('Average Three Pointers Attempted per Game') + labs(color = 'AVG TS%') +
labs(caption = 'Data via basketball-reference.com', title = 'Rookie Three Pointers Attempted by Season')
plot(j)
animated_j <- j + transition_time(as.integer(DraftYear)) + labs(subtitle = 'Draft Year: {frame_time}') + shadow_mark(past = TRUE)
animate(animated_j, nframes = 300)
anim_save("Rookie Average Threes Taken per Game.gif", animated_j, nframes = 150)
# line graph animation
p <- ggplot(g, aes(x = DraftYear, y = mean_threesAttempted, col = mean_TS)) +
geom_line() +
geom_segment(aes(xend = 2018, yend = mean_threesAttempted), linetype = 2, color = 'grey') +
geom_point(size = 2) +
scale_color_gradient(low = 'green', high = 'red') +
transition_reveal(DraftYear) +
coord_cartesian(clip = 'off') +
labs(title = 'Rookie Average Threes Taken per Game by Season', y = 'Average Three Pointers Attempted per Game',
color = 'AVG TS%', x = 'Season') +
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))
p
anim_save('Rookie Average Threes Taken line Graph.gif', p)
# building the traditional point gif - PPG
k <- ggplot(g, aes(x = DraftYear, y = mean_PPG, 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('Average Rookie PPG') + labs(color = 'AVG TS%') +
labs(caption = 'Data via basketball-reference.com', title = 'Average Rookie Points per Game by Season')
plot(k)
animated_k <- k + transition_time(as.integer(DraftYear)) + labs(subtitle = 'Draft Year: {frame_time}') + shadow_mark(past = TRUE)
animate(animated_k, nframes = 300)
anim_save("Rookie Average PPG by Season.gif", animated_k, nframes = 150)