knitr::opts_chunk$set(echo = TRUE)
#install.packages("gganimate")
library(gganimate)
library(tidyverse)
library(lubridate)
#install.packages("gifski")
library(gifski)
fullnba<-read_csv('Seasons_Stats.csv')
goat<-read_csv('finalstats.csv')
#remove all * from the Player column
fullnba$Player<- gsub("[*]", "", fullnba$Player)
#convert season to year
goat$season<-as.Date(goat$season, format="%Y")
goat$year<-year(goat$season)
install.packages("transformr") 
library("transformr")

 # This is where I changed year to indexes to use for number of years played. I still need to limit years so they are all the same.

regmj <- filter(goat, player == "Michael Jordan" , gametype == "Regular Season")

mj_regnum <- tibble::rowid_to_column(regmj,"num_year")

playmj<-filter(goat, player == "Michael Jordan" , gametype == "Playoffs")

mj_playnum <- tibble::rowid_to_column(playmj,"num_year")


regkb<-filter(goat, player == "Kobe Bryant" , gametype == "Regular Season")

kb_regnum <- tibble::rowid_to_column(regkb,"num_year")

playkb <- filter(goat, player == "Kobe Bryant" , gametype == "Playoffs")

kb_playnum <- tibble::rowid_to_column(playkb,"num_year")


reglj<-filter(goat, player == "Lebron James", gametype == "Regular Season")


lj_regnum <- tibble::rowid_to_column(reglj,"num_year")

playlj<-filter(goat, player == "Lebron James", gametype == "Playoffs")

lj_playnum <- tibble::rowid_to_column(playlj,"num_year")

index_goat <- rbind(mj_regnum, mj_playnum,kb_regnum,kb_playnum,lj_regnum,lj_playnum)
bullsred = "#CE1141"
lakeryellow = "#FDB927"
cavswine = "#6f263d"

team_colors <- c("#CE1141", "#FDB927", "#6f263d")

player_playBMI<- index_goat %>% filter(gametype == "Playoffs")%>%
  ggplot(aes(x=num_year, y=overallbpm, color = player))+
    geom_line( alpha = .8)+
    scale_colour_manual(values = team_colors,
                        breaks=c('Michael Jordan', 'Kobe Bryant', 'Lebron James'),
                        labels=c('Michael Jordan', 'Kobe Bryant', 'LeBron James'))+
    theme_bw()+
    theme(
      plot.caption=element_text(size=8,hjust = 0)
    )+
  scale_x_continuous(breaks = seq(0,24))+
    labs(
      title="Michael Jordan v. Kobe Bryant v. Lebron James",
      subtitle="Box Plus/Minus (Playoffs BPM) - how do they compare?",
      caption="Box Plus/Minus (Playoffs BPM) is a basketball box score-based metric that estimates a basketball player's contribution
to the team when that player is on the court for the playoffs.",
      color="Player",
      y="Playoffs BPM",
      x="years played"
    ) + coord_equal(ratio = 1)

# Here is the plot
plot(player_playBMI)

# here is how I used gganimate. This will I hope have more labels of when each bball player started playing and ended playing as well as each dot representing the year they actally made the BPM stat. 
player_playBMI+transition_reveal(along = num_year)