Please explain the following R code: #Oline Statistic
library(tidyverse)



line2015 <- read.csv('2015 Basic Line Stats offense.csv')
line2016 <- read.csv('2016 Basic Line Stats offense.csv')
line2017 <- read.csv('2017 Basic Line Stats offense.csv')
line2018 <- read.csv('2018 Basic Line Stats offense.csv')
line2019 <- read.csv('2019 Basic Line Stats offense.csv')
line2020 <- read.csv('2020 Basic Line Stats offense (1).csv')

line2015 <- line2015 %>%
  mutate(Year = '2015') %>%
  filter(Adjusted.Line.Yards >= 3.27)

line2016 <- line2016 %>%
  mutate(Year = '2016') %>%
  filter(Adjusted.Line.Yards >= 3.27)


line2017 <- line2017 %>%
  mutate(Year = '2017')%>%
  filter(Adjusted.Line.Yards >= 3.27)


line2018 <- line2018 %>%
  mutate(Year = '2018')%>%
  filter(Adjusted.Line.Yards >= 3.27)


line2019 <- line2019 %>%
  mutate(Year = '2019')%>%
  filter(Adjusted.Line.Yards >= 3.27)


line2020 <- line2020 %>%
  mutate(Year = '2020')%>%
  filter(Adjusted.Line.Yards >= 3.27)


together1 <- inner_join(line2015, line2016, by = 'Team')
together2 <- inner_join(line2016, line2017, by = 'Team')
together3 <- inner_join(line2017, line2018, by = 'Team')
together4 <- inner_join(line2018, line2019, by = 'Team')
together5 <- inner_join(line2019, line2020, by = 'Team')

c <- ggplot() +
  geom_point(data = together1, aes(x = Running.Back.Yards.x, y = Running.Back.Yards.y), color = 'royalblue1', size = 2.5)+
  geom_point(data = together2, aes(x = Running.Back.Yards.x, y = Running.Back.Yards.y), color = 'royalblue2', size = 2.5)+
  geom_point(data = together3, aes(x = Running.Back.Yards.x, y = Running.Back.Yards.y), color = 'royalblue3', size = 2.5)+
  geom_point(data = together4, aes(x = Running.Back.Yards.x, y = Running.Back.Yards.y), color = 'royalblue4', size = 2.5)+
  geom_point(data = together5, aes(x = Running.Back.Yards.x, y = Running.Back.Yards.y), color = 'navy', size = 2.5)
# c <- c + grids(linetype = "dashed")
# a <- a + xlim(0, 350)
# a <- a + ylim(0, 400)
c <- c + ggtitle("Running Back Rushing Att. From One Year To The Next") +
  xlab("Year One Rushing Att.") + ylab("Year Two Rushing Att.")
c

pg <- ggplot_build(c)
pg
df <- rbind(pg$data[[1]], pg$data[[2]], pg$data[[3]], pg$data[[4]], pg$data[[5]])
df
cor(df[['x']], df[['y']])
#GOOD



##################################################################CORRELATATION BETWEEN OLINE AND FANTASY PERFORMANCE

# GEOM POINT NEEDS:
# 1 DATA THAT HAS: Team specific, Year Specific OLINE Data + Fantasy Points of the RB from that team + that year
# Line15 has Team specific, year specific OLINE data, need to combine that with 

year15 <- read.csv('2015teamstat.csv')
year16 <- read.csv('2016teamstat.csv')
year17 <- read.csv('2017teamstat.csv')
year18 <- read.csv('2018teamstat.csv')
year19 <- read.csv('2019teamstat.csv')
year20 <- read.csv('new2020data.csv')

line2015 <- read.csv('2015 Basic Line Stats offense.csv')
line2016 <- read.csv('2016 Basic Line Stats offense.csv')
line2017 <- read.csv('2017 Basic Line Stats offense.csv')
line2018 <- read.csv('2018 Basic Line Stats offense.csv')
line2019 <- read.csv('2019 Basic Line Stats offense.csv')
line2020 <- read.csv('2020 Basic Line Stats offense (1).csv')

line2015 <- line2015 %>%
  mutate(Year = '2015') %>%
  arrange(Team) %>%
  mutate(Number = row_number())

line2016 <- line2016 %>%
  mutate(Year = '2016')%>%
  arrange(Team) %>%
  mutate(Number = row_number())


line2017 <- line2017 %>%
  mutate(Year = '2017')%>%
  arrange(Team) %>%
  mutate(Number = row_number())


line2018 <- line2018 %>%
  mutate(Year = '2018')%>%
  arrange(Team) %>%
  mutate(Number = row_number())


line2019 <- line2019 %>%
  mutate(Year = '2019')%>%
  arrange(Team) %>%
  mutate(Number = row_number())


line2020 <- line2020 %>%
  mutate(Year = '2020')%>%
  arrange(Team) %>%
  mutate(Number = row_number())


year15 <- year15 %>%
  select(-Number)%>%
  filter(!is.na(PTS))

year16 <- year16 %>%
  select(-X)%>%
  filter(!is.na(PTS))

year17 <- year17 %>%
  select(-X) %>%
  select(-X.1) %>%
  filter(!is.na(PTS))

year18 <- year18 %>%
  select(-X) %>%
  select(-X.1) %>%
  filter(!is.na(PTS))

year19 <- year19 %>%
  select(-X) %>%
  select(-X.1) %>%
  filter(!is.na(PTS))
  

year20 <- year20 %>%
  select(-X)%>%
  filter(!is.na(PTS))

year20$Team <- gsub('?', '', year20$Team)

#########
year15 <- year15 %>%
  mutate(Year = '2015') %>%
  arrange(Team) %>%
  mutate(Number = row_number())

year16 <- year16 %>%
  mutate(Year = '2016') %>%
  arrange(Team) %>%
  mutate(Number = row_number())

year17 <- year17 %>%
  mutate(Year = '2017')%>%
  arrange(Team) %>%
  mutate(Number = row_number())

year18 <- year18 %>%
  mutate(Year = '2018')%>%
  arrange(Team) %>%
  mutate(Number = row_number())

year19 <- year19 %>%
  mutate(Year = '2019')%>%
  arrange(Team) %>%
  mutate(Number = row_number())

year20 <- year20 %>%
  mutate(Year = '2020')%>%
  arrange(Team) %>%
  mutate(Number = row_number())

together1 <- inner_join(year15, line2015, by = 'Number')
together2 <- inner_join(year16, line2016, by = 'Number')
together3 <- inner_join(year17, line2017, by = 'Number')
together4 <- inner_join(year18, line2018, by = 'Number')
together5 <- inner_join(year19, line2019, by = 'Number')
together5 <- inner_join(year20, line2020, by = 'Number')

p <- ggplot() +
  geom_point(data = together1, aes(x = Running.Back.Yards, y = RB), color = 'royalblue1', size = 2.5)+
  geom_point(data = together2, aes(x = Running.Back.Yards, y = RB), color = 'royalblue2', size = 2.5)+
  # geom_point(data = together3, aes(x = Running.Back.Yards, y = RB), color = 'royalblue3', size = 2.5)+
  geom_point(data = together4, aes(x = Running.Back.Yards, y = RB), color = 'royalblue4', size = 2.5)+
  geom_point(data = together5, aes(x = Running.Back.Yards, y = RB), color = 'navy', size = 2.5)
p
pg <- ggplot_build(p)
pg
df <- rbind(pg$data[[1]], pg$data[[2]], pg$data[[3]], pg$data[[4]])
df
fit <- lm(y ~ x, data = df)
fit[["coefficients"]]


p <- p + geom_abline(intercept = 69.91583     , slope = 29.06602  , color = "red")
p <- p + labs(title = 
                "Correlation between Running Back Fantasy Performance and Average Team Points per Game") +
  xlab("Average Team Points Per Game") + ylab("Running Back Fantasy Performance")

p
cor(df[['x']], df[['y']])