0

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.

Image Link Here is my code:

#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.

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
  • [Don't use `$` inside `aes`](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio) – camille Sep 27 '19 at 22:54

1 Answers1

1

Here's an example with the iris data. You should be able to define all the color codes once, and apply those. It will use the defined colors each plot needs.

my_colors <- c("setosa" = "#E15759",
               "CU" = "#59A14F",
               "CH" = "#FF9DA7",
               "versicolor" = "#4E79A7",
               "virginica" = "#76B7B2")

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
  geom_point(size = 5) +
  scale_color_manual(values = my_colors)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53