0

I'm trying to plot the relationship between Batting Average and On Base Percentage and how it changes from week to week.

Below, i exported data from Game Changer (web.gc.com) and used a scatterplot with points\labels that intersect a player at BattingAVG and OnBasePercentage. The teamstats_week1.csv contains the totals stats for each player and obviously data changes week to week.

I'll need to export the new totals CSV file each week (teamstats_week1.csv, teamstats_week2.csv, etc). My question is how can i take multiple CSV files, plot the point, then show movement of those points (good or bad) throughout the course of the season?

library(tidyverse)
library(ggrepel)

# read CSV from current working directory. 
# the CSV file was exported from web.gc.com and renamed teamstats.csv
# teamstats_week1.csv should be in the same directory as the R Script
# first row is skipped since it doesn't contain any relevant information

gc_read_csv <- read.csv("teamstats_week1.csv", skip = 1, stringsAsFactors = FALSE)

# create new data frame gc_team_select

gc_team_select <- gc_read_csv %>% 
  mutate(BattingAVG = as.double(AVG) * 1000, OnBasePercentage = as.double(OBP) * 1000, GamesPlayed = as.numeric(GP), PlayerNumber = as.integer(Number)) %>%
  select(PlayerNumber, First, GamesPlayed, BattingAVG, OnBasePercentage) %>% 
  filter(PlayerNumber < 100) %>%
  filter(GamesPlayed > 6)

# plot BattingAVG and OnBasePercentage

ggplot(data = gc_team_select, mapping = aes(x = BattingAVG, y = OnBasePercentage)) + 
  geom_point() +
  geom_text_repel(aes(label = First), size = 3) 

Daniel H
  • 3
  • 2
  • 1
    Questions asking how to work (load and process) with multiple csv files have been asked and answered several times on SO. Have you done any searching? – IRTFM Oct 24 '21 at 21:38
  • Since this is really a multi-part question that asks us to write your project for you (which is not really on-topic for SO) I'm going to close as a duplicate of how to load and merge multiple csv files. I realize that the duplicate doesn't have a complete answer to your question but this should solve the first step and then you can write a more narrowly defined follow-up question that shows where you get to after taking a smaller step toward completion. This was how I found the duplicate I'm nominating: https://stackoverflow.com/search?tab=votes&q=%5br%5d%20load%20multiple%20csv%20file – IRTFM Oct 24 '21 at 21:41

0 Answers0