I am attempting to create a plot in R, plotting simple X & Y coordinates of individual locations for each pitch on a scatter-plot, categorized for different pitch types (baseball). There are 13 total possible pitch types, but my graph will only show between 3-7 types... not pitcher throws all 13 types. In addition to individual locations for each pitch, I want to plot the average locations, split by pitch type.
I want to assign each pitch type ("CH", "CU", "FF", ...) specific color-hex codes. Bear in mind, I plan to use this for other pitchers, with different pitch types (all coming from the same set of 13 pitch types). I would like the colors to be consistent across these different plots.
Currently, I am able to plot each individual X & Y coordinate, along with the averages. However, I cannot figure out how to assign my desired color-hex codes for each pitch type.
#Libraries Loaded:
library(baseballr)
library(ggplot2)
library(dbplyr)
library(grDevices)
library(ggplot2)
library(grid)
library(gridExtra)
library(ggpubr)
#Pull the data
yesterday<- Sys.Date()-1
felix<-scrape_statcast_savant(start_date=yesterday-30, end_date=yesterday,playerid = "433587", player_type = 'pitcher')
#Create dataframe with calculated average release locations
avglocfelix<- felix %>%
group_by(pitch_type) %>%
summarize(N=n(),
avg_rpx=mean(release_pos_x, na.rm = TRUE),
avg_rpz=mean(release_pos_z, na.rm=TRUE))
#Make pitch types factor() [is this necessary?]
felix$fpitch_type <- factor(felix$pitch_type)
avglocfelix$fpitch_type<- factor(avglocfelix$pitch_type)
#plot both: individual locations and avg locations
#[image attached. small dots are individual locations, large dots are avg. locations]
ggplot()+geom_point(data=felix, aes(felix$release_pos_x, felix$release_pos_z, color=felix$fpitch_type), size=3)+
geom_point(data=avglocfelix, aes(avglocfelix$avg_rpx, avglocfelix$avg_rpz, color=avglocfelix$fpitch_type), size=7)
Thanks in advance for any help you all can provide. I'm kind of new to this, so let me know if I'm missing something.