forked from toddwschneider/ballr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lebron.R
80 lines (72 loc) · 3.21 KB
/
lebron.R
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
73
74
75
76
77
78
79
80
lebron = find_player_by_name("LeBron James")
seasons = season_strings[as.character(2003:2015)]
court_theme = court_themes$dark
all_shots = bind_rows(lapply(seasons, function(season) {
fetch_shots_by_player_id_and_season(lebron$person_id, season)$player %>%
mutate(season = season)
}))
simplified_areas = c(
"Center(C)" = "Center",
"Left Side(L)" = "Left",
"Left Side Center(LC)" = "Left",
"Right Side(R)" = "Right",
"Right Side Center(RC)" = "Right",
"Back Court(BC)" = "Backcourt"
)
all_shots$simplified_area = simplified_areas[all_shots$shot_zone_area]
area = all_shots %>%
filter(simplified_area != "Backcourt") %>%
mutate(season = as.numeric(substr(season, 1, 4))) %>%
group_by(season, simplified_area) %>%
summarize(attempts = n(), .groups = "drop") %>%
group_by(season) %>%
mutate(frac = attempts / sum(attempts))
label_text = c("Center" = "Center", "Left" = "Left Side\n(his right)", "Right" = "Right Side\n(his left)")
ggplot(data = area, aes(x = season, y = frac, color = simplified_area)) +
geom_line(size = 1) +
scale_x_continuous("Season", breaks = seq(2003, 2015, by = 2)) +
scale_y_continuous("% of shot attempts\n", labels = scales::percent) +
scale_color_discrete("", guide = FALSE) +
expand_limits(y = 0) +
coord_cartesian(xlim = c(2003, 2017.5)) +
ggtitle("LeBron James Shot Attempts by Court Area") +
geom_text(data = filter(area, season == max(area$season)),
aes(x = season + 0.25, y = frac, label = label_text[simplified_area]),
size = 7, hjust = 0) +
theme_bw(base_size = 22) +
theme(
text = element_text(color = "#f0f0f0"),
panel.grid.major = element_line(size = 0.25, color = "#555555"),
panel.grid.minor = element_line(size = 0.25, color = "#555555"),
plot.background = element_rect(fill = court_theme$court, color = court_theme$court),
panel.background = element_rect(fill = court_theme$court, color = court_theme$court)
)
dist_fgp = all_shots %>%
mutate(
season = as.numeric(substr(season, 1, 4)),
group = ifelse(season == 2015, "2015-16", "Prev 5 Seasons"),
dist_bucket = cut(shot_distance, breaks = c(0, 1, 2, 4, 8, 12, 16, 20, 24, 32, 100), right = FALSE)
) %>%
filter(season >= 2010) %>%
group_by(group, dist_bucket) %>%
summarize(fgp = mean(shot_made_numeric), attempts = n(), dist = mean(shot_distance)) %>%
filter(attempts > 50)
ggplot(data = dist_fgp, aes(x = dist, y = fgp, color = group)) +
geom_line(size=1) +
scale_x_continuous("Shot distance (feet)") +
scale_y_continuous("FG%", labels = scales::percent) +
scale_color_discrete("", guide = FALSE) +
expand_limits(y = 0.2) +
coord_cartesian(xlim = c(0, 35)) +
ggtitle("LeBron James FG% by Shot Distance") +
geom_text(data = filter(dist_fgp, dist_bucket == "[24,32)"),
aes(x = 26, y = fgp, label = group),
size = 7, hjust = 0) +
theme_bw(base_size = 22) +
theme(
text = element_text(color = "#f0f0f0"),
panel.grid.major = element_line(size = 0.25, color = "#555555"),
panel.grid.minor = element_line(size = 0.25, color = "#555555"),
plot.background = element_rect(fill = court_theme$court, color = court_theme$court),
panel.background = element_rect(fill = court_theme$court, color = court_theme$court)
)