Tutorial 5

library(dplyr)
library(tidyr)
#install.packages("devtools")
devtools::install_github("cardiomoon/moonBook")
devtools::install_github("cardiomoon/moonBook2")

require(moonBook)
require(ggplot2)
require(ggiraph)
#require(plyr)
require(reshape2)
require(scales)
require(moonBook2)
setwd("/Users/mitchell/R/R_MarkDowns")
# DF1 is the table I am using for the Spider.Plot function. 
DF1 <- read.csv("ESPN.TIME.TABLE1.csv", stringsAsFactors = F) %>% mutate(POS = trimws(POS))
NFLterms <- read.csv("FBall.Terms.csv", stringsAsFactors = F)

This is my Spider.Plot Function (or radar plot) for multiple players in a given year. Simply entering the player names and year will generate a spider plot as depicted below…

SpiderPlot.Func <- function(Category, Position, YEAR, Player1, Player2, Player3, Player4, Player5, Player6)
{
  p <<- DF1 %>% filter(POS == Position) %>%
    filter(Year == YEAR) %>%
    filter(Web.Cat == Category) %>%
    group_by(Web.Cat, POS, Year, Pro.Record) %>%
    mutate(Max = max(Value, na.rm = T)
           ,T.Value = (Value / Max)) %>%
    ungroup() %>%
    select(POS, PLAYER, Pro.Record, T.Value) %>%
    filter(PLAYER == Player1 | PLAYER == Player2 | PLAYER == Player3 
           | PLAYER == Player4 | PLAYER == Player5 | PLAYER == Player6) %>%
    spread(Pro.Record, T.Value)
  write.csv(p, "p.csv", row.names = F)
  p1 <<- p 
  
  ggRadar(p, groupvar= "PLAYER", interactive = TRUE)

}

SpiderPlot.Func(Category = "Passing", Position = "QB", YEAR = "2006"
                , Player1 = "Peyton Manning", Player2 = "Drew Brees", Player3 = "Carson Palmer"
                , Player4 = "Brett Favre", Player5 = "Tom Brady", Player6 = "Aaron Rodgers")

This series of radar plots is created by a seperate df “p1”, also created in the above function…

pA<-ggRadar(p1[1:6,],rescale=F,groupvar="PLAYER",legend.position="none")+
  ylim(0,1)+facet_wrap(~PLAYER)
ggiraph(code=print(pA))

All of the plots above are created from the below table. This table is also a product of the Spider.Plot Function…

2006 Quarter Backs selected for the Radar Plot
POS PLAYER ATT COMP INT LONG PCT RATE SACK TD YDS YDS.A YDS.G
QB Aaron Rodgers 0.0244698 0.0161290 NA 0.1797753 0.400 0.3505455 0.0476190 NA 0.0104120 0.2971926 0.0833333
QB Brett Favre 1.0000000 0.9220430 0.7826087 0.9213483 0.560 0.5287273 0.3333333 0.5806452 0.8793572 0.6137464 0.8804348
QB Carson Palmer 0.8482871 0.8709677 0.5652174 0.8314607 0.623 0.6829091 0.5714286 0.9032258 0.9133092 0.7512101 0.9130435
QB Drew Brees 0.9037520 0.9569892 0.4782609 0.9662921 0.643 0.6996364 0.2857143 0.8387097 1.0000000 0.7725073 1.0000000
QB Peyton Manning 0.9086460 0.9731183 0.3913043 0.7640449 0.650 0.7345455 0.2222222 1.0000000 0.9952467 0.7637948 0.9963768
QB Tom Brady 0.8417618 0.8575269 0.5217391 0.6966292 0.618 0.6392727 0.4126984 0.7741935 0.7987777 0.6621491 0.8007246

This radar plot displays a single player’s recorded performance over multiple / selected years…

SpiderPlot.Year.Func <- function(Category, Position, Player, YEAR1, YEAR2, YEAR3, YEAR4, YEAR5, YEAR6)
{
  pY <<- DF1 %>% filter(POS == Position) %>%
    filter(Web.Cat == Category) %>%
    group_by(Web.Cat, POS, Year, Pro.Record) %>%
    mutate(Max = max(Value, na.rm = T)
           ,T.Value = (Value / Max)) %>%
    ungroup() %>%
    select(POS, PLAYER, Year, Pro.Record, T.Value) %>%
    filter(PLAYER == Player) %>%
    filter(Year  == YEAR1 | Year == YEAR2 | Year == YEAR3 
           | Year == YEAR4 | Year == YEAR5 | Year == YEAR6) %>%
    spread(Pro.Record, T.Value)
  write.csv(pY, "pYear.csv", row.names = F)
  p2 <<- pY  
  ggRadar(pY, groupvar= "Year", interactive = TRUE)
  
}

SpiderPlot.Year.Func(Category = "Passing", Position = "QB", Player = "Aaron Rodgers"
                     , YEAR1 = "2007", YEAR2 = "2008", YEAR3 = "2009"
                     , YEAR4 = "2010", YEAR5 = "2013", YEAR6 = "2014")

Above is Aaron Rodgers’s stats for 2007-2010 + 2013 and 2014…

Even with 6 years displayed in this radar plot you can still clearly see his performance levels change over the course of time. For example, notice how there is a level of consistency throughout his career that drastically changes for 2013, the year sustained a broken collarbone. Still, if this plot appears too congested a plot for every year can be created instead…

pB<-ggRadar(p2[1:6,],rescale=F,groupvar="Year",legend.position="none")+
  ylim(0,1)+facet_wrap(~Year)
ggiraph(code=print(pB))

Here’s a (hokey) little ifelse function that pulls the terminology for each stats category (e.g. Rushing, Passing, etc.)

CatTable <- function(Category)
{ 
  BBB <- Category
   ifelse(BBB == "Passing", x <- 1, ifelse(BBB == "Returning", x <- 3, ifelse(BBB == "Rushing", x <- 5
    , ifelse(BBB == "Scoring", x <- 7, ifelse(BBB == "Receiving", x <- 9, ifelse(BBB == "Kicking", x <- 11
    , ifelse(BBB == "Defense", x <- 13, ifelse(BBB == "Punting", x <- 15))))))))  
 
y <- 1 + x

Categories <- NFLterms %>% select(x, y) %>% print()
}

CatTable(Category = "Passing")
##    Passing               Passing.1
## 1     COMP             Completions
## 2      ATT        Passing attempts
## 3      PCT   Completion percentage
## 4      YDS           Passing yards
## 5    YDS.A  Yards per pass attempt
## 6     LONG            Longest pass
## 7       TD      Passing touchdowns
## 8      INT    Interceptions thrown
## 9     SACK                   Sacks
## 10    RATE           Passer rating
## 11   YDS.G  Passing yards per game
## 12                                
## 13

I created a few other quick helpful functions. For instance, the below function will display the rank of players of a selected position, category of recorded stats (e.g. Passing, or Rushing, etc.), and chosen year. Furthermore, if a variable might disrupt this ranking, say for instance like a perfect percentage of completions score for a QB who only threw one pass in a season, said variable can be removed by name.

PlayerRank <- function(Position, YEAR, Category, Disruptive.Variable, Count)
{
  pRnk <<- DF1 %>%
    filter(POS == Position) %>%
    filter(Year == YEAR) %>%
    filter(Web.Cat == Category) %>%
    filter(Pro.Record != Disruptive.Variable) %>%
    group_by(Pro.Record) %>%
    mutate(Avg = round(mean(Value2, na.rm = T), 2)
           , Max = max(Value, na.rm = T)) %>%
    ungroup() %>%
    mutate(Grade = round(Value2/Max, 2) * 100) %>%
    group_by(PLAYER) %>%
    mutate(Average.Grade = mean(Grade, na.rm = T)) %>%
    ungroup() %>%
    mutate(Max.Avg = max(Average.Grade, na.rm = T))%>%
    mutate(True.Grade = round(Average.Grade/Max.Avg, 2) * 100) %>%
    arrange(desc(True.Grade)) %>%
    select(1,2,3,4,15) %>%
    distinct() %>%
    slice(1:Count) 
}

PlayerRank(Position = "QB", YEAR = "2006", Category = "Passing"
           , Disruptive.Variable = "PCT", Count = "10")

Top Ranked Quarter Backs for 2006
PLAYER POS TEAM Year True.Grade
Peyton Manning QB IND 2006 100
Drew Brees QB NO 2006 98
Marc Bulger QB LA 2006 87
Carson Palmer QB CIN 2006 85
Brett Favre QB GB 2006 80
Tom Brady QB NE 2006 79
Donovan McNabb QB PHI 2006 77
Philip Rivers QB SD 2006 76
Steve McNair QB BAL 2006 75
Chad Pennington QB NYJ 2006 70

This function identifies players from a partial spelling of their name…

PlayerFinder <- function(Partial_Player_name)
{
PFound <<- DF1 %>% 
  mutate(PlayerFound = ifelse(grepl(Partial_Player_name, DF1$PLAYER), PLAYER, "NA")) %>%
  filter(PLAYER == PlayerFound) %>% 
  select(1,2,3,4) %>% 
  distinct()
}

PlayerFinder(Partial_Player_name = "Fav")

print(PFound)
##        PLAYER POS TEAM Year
## 1 Brett Favre  QB   GB 2005
## 2 Brett Favre  QB   GB 2006
## 3 Brett Favre  QB   GB 2007
## 4 Brett Favre  QB  NYJ 2008
## 5 Brett Favre  QB  MIN 2009
## 6 Brett Favre  QB  MIN 2010
## 7 Greg Favors  LB  JAX 2005

This function creates a list of every player of a stats catagory, on a team, in a given year…

PlayerInfo.Func <- function(Category, Team, YEAR)
{PlayerInfo <<- DF1 %>% 
  filter(Web.Cat == Category) %>%
  filter(TEAM == Team) %>%
  filter(Year == YEAR) %>%
  select(1,2) %>% distinct() %>%
  print()
}
PlayerInfo.Func(Category = "Defense", Team = "PHI", YEAR = "2015")
##                 PLAYER POS
## 1      Malcolm Jenkins  FS
## 2     Mychal Kendricks  LB
## 3  Walter Thurmond III  DB
## 4         Fletcher Cox  DE
## 5        Byron Maxwell  CB
## 6     Nolan Carroll II  CB
## 7         Bennie Logan  DT
## 8        Connor Barwin  LB
## 9       Brandon Graham  LB
## 10        Jordan Hicks  LB
## 11        DeMeco Ryans  LB
## 12        E.J. Biggers  CB
## 13         Kiko Alonso  LB
## 14     Cedric Thornton  DT
## 15           Eric Rowe  DB
## 16          Beau Allen  DT
## 17         Taylor Hart  DE
## 18         Ed Reynolds   S
## 19       Chris Maragos  FS
## 20         Vinny Curry  DE
## 21        Brandon Bair  DE
## 22         Najee Goode  LB
## 23     Marcus Smith II  LB
## 24        Bryan Braman  LB
## 25          Brad Jones  LB
## 26  Jerome Couplin III   S
## 27      Jaylen Watkins  CB

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.