Here's what I'm trying to fix...
This code is supposed to generate an NBA shot chart; however, I have been trying to correct this error for close to an hour now. Any help would be greatly appreciated. Attached below is the code for creating the court, along with the code (below that) for creating the chart.
plot_court = function(court_theme = court_themes$light, use_short_three = FALSE) {
if(use_short_three) {
three_point_radius = 22
three_point_side_height = 0
}
court_points = data_frame(
x = c(width / 2, -width / 2, -width / 2, width / 2),
y = c(height, 0, 0, height, height),
desc = "perimeter"
)
court_points = bind_rows(court_points , data_frame(
x = c(outer_key_width / 2, outer_key_width / 2, -outer_key_width / 2, -outer_key_width / 2),
y = c(0, key_height, key_height, 0),
desc = "outer_key"
))
court_points = bind_rows(court_points, data_frame(
x = c(-backboard_width / 2, backboard_width / 2),
y = c(backboard_offset, backboard_offset),
desc = "backboard"
))
court_points = bind_rows(court_points , data_frame(
x = c(0, 0), y = c(backboard_offset, backboard_offset + neck_length), desc = "neck"
))
foul_circle = circle_points(center = c(0, key_height), radius = inner_key_width / 2)
foul_circle_top = filter(foul_circle, y > key_height) %>%
mutate(desc = "foul_circle_top")
foul_circle_bottom = filter(foul_circle, y < key_height) %>%
mutate(
angle = atan((y - key_height) / x) * 180 / pi,
angle_group = floor((angle - 5.625) / 11.25),
desc = paste0("foul_circle_bottom", angle_group)
) %>%
filter(angle_group %% 2 == 0) %>%
select(x, y, desc)
hoop = circle_points(center = c(0, hoop_center_y), radius = hoop_radius) %>%
mutate(desc = "hoop")
restricted = circle_points(center = c(0, hoop_center_y), radius = 4) %>%
filter(y >= hoop_center_y) %>%
mutate(desc = "restricted")
three_point_circle = circle_points(center = c(0, hoop_center_y), radius = three_point_side_radius) %>%
filter(y >= three_point_side_height, y >= hoop_center_y)
three_point_line = data_frame(
x = c(three_point_side_radius, three_point_side_radius, three_point_circle$x, -three_point_side_radius, -three_point_side_radius),
y = c(0, three_point_side_height, three_point_circle$y, three_point_side_height, 0),
desc = "three_point_line"
)
court_points = bind_rows(
court_points,
foul_circle_top,
foul_circle_bottom,
hoop,
restricted,
three_point_line
)
court_points < court_points
#Final Plot Creation
ggplot() +
geom_path(
data = court_points,
aes(x = x, y = y, group = desc),
color = court_theme$lines
) +
coord_fixed(ylim = c(0, 45), xlim = c(-25, 25))
theme_minimal(base_size = 22) +
theme(
text = element_text(color = court_theme$text),
plot.background = element_rect(fill - 'gray20', color = 'gray20'),
panel.background = element_rect(fill = court_theme$court, color = color_theme$court),
panel.grid = element_blank(),
panel.border = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.background = element_rect(fill = court_theme$court, color = court_theme$court),
legend.margin = margin(-1, 0, 0, 0, unit = "lines"),
legend_position = "bottom",
legend.key = element_blank(),
legend.text = element_text(size = rel(1,0))
plot_court(court_themes$ppt, use_short_three = F) +
geom_point(data = final_durant, aes(x = x, y = y, color = final_durant$isShotMade, fill = final_durant$isShotMade),
size = 3, shape = 21, stroke = 5) +
scale_color_manual(values = c("green","red"), aesthetics = "color", breaks = c("TRUE", "FALSE"), labels = c("Made", "Missed")) +
scale_fill_manual(values = c("green2", "gray20"), aesthetics = "fill", breaks = c("TRUE", "FALSE"), labels = c("Made", "Missed")) +
scale_x_continuous(limits = c(-27.5, 27.5)) +
scale_y_continuous(limits = c(0, 45)) +
theme(plot.title = element_text(hjust = .5, size = 22, family = "Comic Sans MS", face = "bold", vjust = -4),
plot.subtitle = element_text(hjust = .5, size = 10, family = "Comic Sans MS", face = "bold", vjust = -8),
legend.position = c(.5, .85),
legend.direction = "horizontal",
legend.title = element_blank(),
legend.text = element_text(hjust = .5, size = 10, family = "Comic Sans MS", face = "bold", colour = "white")) +
ggtitle(label = "Kevin Durant vs. Milaukee",
subtitle = "30 PTS | 4 REB | 7-10 3PT - 5/2/21")
ggdraw(p1) + theme(plot.background = element_rect(fill = "gray20", color = NA))
ggsave("Durant.png", height = 6, width = 6, dpi = 300)
rlang::last_error()