Row

Points Per Game

Rebounds Per Game

Row

Assists Per Game

Field Goal Percentage

Row

Aggregate Table of Playoff Performance

Row

Proportion of Players with Increased PPPG

---
title: "NBA Regular Season and Playoff Comparison from 2000 to 2016"
author: "David Sung"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    theme: journal
    social: menu
    source_code: embed
    vertical_layout: scroll
---

```{r setup, include=FALSE}
library(dplyr)
library(pipeR)
library(sqldf)
library(plotly)
library(knitr)
library(DT)

# Inputs of start and end years to analyze
startYear <- 2000
endYear <- 2016

# Set wd
setwd('C:\\Users\\sungdavid\\Documents\\Personal\\NBA')

# Assign data frames
for (year in startYear:endYear) {
  x <- paste0('df_', year)
  temp_df <- read.csv(paste0('./data/',x, '.csv'))
  assign(x, temp_df)
}


# Aggregate data

for (i in startYear:endYear) {
  
  if (i == startYear) {
    x <- paste0('df_', i)
    df_agg <-  get(x)
  } else {
    x <- paste0('df_', i)
    assign(x, get(x))
    
    q1 <- "
    SELECT * FROM df_agg UNION ALL
    SELECT * FROM 
    "
    
    q2 <- x
    
    q <- paste0(q1, q2)
    
    df_agg <- sqldf(q)
  }
}

# Create plots
ppg <- plot_ly(df_agg, x = ~regPPG, y = ~regPPG, type = 'scatter', mode = 'lines'
) %>% 
  add_trace(x = ~regPPG, y = ~playPPG, mode = 'markers', color = ~diffPPG, size = ~absPPG,
            text = ~paste0(player, '
Regular Season PPG: ', regPPG, '
Playoffs PPG: ', playPPG, '
Difference: ', diffPPG, '
Year: ', year) ) %>% layout(showlegend = FALSE, xaxis = list(title = 'Regular Season'), yaxis = list(title = 'Playoffs') ) rpg <- plot_ly(df_agg, x = ~regRPG, y = ~regRPG, type = 'scatter', mode = 'lines' ) %>% add_trace(x = ~regRPG, y = ~playRPG, mode = 'markers', color = ~diffRPG, size = ~absRPG, text = ~paste0(player, '
Regular Season RPG: ', regRPG, '
Playoffs RPG: ', playRPG, '
Difference: ', diffRPG, '
Year: ', year) ) %>% layout(showlegend = FALSE, xaxis = list(title = 'Regular Season'), yaxis = list(title = 'Playoffs') ) apg <- plot_ly(df_agg, x = ~regAPG, y = ~regAPG, type = 'scatter', mode = 'lines' ) %>% add_trace(x = ~regAPG, y = ~playAPG, mode = 'markers', color = ~diffAPG, size = ~absAPG, text = ~paste0(player, '
Regular Season APG: ', regAPG, '
Playoffs APG: ', playAPG, '
Difference: ', diffAPG, '
Year: ', year) ) %>% layout(showlegend = FALSE, xaxis = list(title = 'Regular Season'), yaxis = list(title = 'Playoffs') ) fgp <- plot_ly(df_agg, x = ~regFGP, y = ~regFGP, type = 'scatter', mode = 'lines' ) %>% add_trace(x = ~regFGP, y = ~playFGP, mode = 'markers', color = ~diffFGP, size = ~absFGP, text = ~paste0(player, '
Regular Season FGP: ', regFGP, '
Playoffs FGP: ', playFGP, '
Difference: ', diffFGP, '
Year: ', year) ) %>% layout(showlegend = FALSE, xaxis = list(title = 'Regular Season'), yaxis = list(title = 'Playoffs') ) # Create table for viewing table_view <- data.frame('Player Name' = df_agg$player, 'Year' = df_agg$year, 'Playoff PPG' = df_agg$playPPG, 'Regular Season PPG' = df_agg$regPPG, 'PPG Difference' = df_agg$diffPPG, 'Playoff RPG' = df_agg$playRPG, 'Regular Season RPG' = df_agg$regRPG, 'RPG Difference' = df_agg$diffRPG, 'Playoff APG' = df_agg$playAPG, 'Regular Season APG' = df_agg$regAPG, 'APG Difference' = df_agg$diffAPG, 'Playoff FGP' = df_agg$playFGP, 'Regular Season FGP' = df_agg$regFGP, 'FGP Difference' = df_agg$diffFGP ) # Find quartiles and proportion of players who showed increase in scoring ppg_quantile<- c('cutoff' = c(quantile(df_agg$regPPG, c(.25, .75)))) ppg_LQ <- df_agg[df_agg$regPPG < ppg_quantile[1] , ] ppg_IQR <- df_agg[with(df_agg, regPPG >= ppg_quantile[1] & regPPG <= ppg_quantile[2]), ] ppg_UQ <- df_agg[df_agg$regPPG > ppg_quantile[2] , ] LQ <- sum(ppg_LQ$diffPPG > 0) / nrow(ppg_LQ) IQR <- sum(ppg_IQR$diffPPG > 0) / nrow(ppg_IQR) UQ <- sum(ppg_UQ$diffPPG > 0) / nrow(ppg_UQ) cat <- c("Lower Quartile", "Interquartile", "Upper Quartile") ppg_inc <- c(LQ, IQR, UQ) ppg_dec <- c(1 - LQ, 1 - IQR, 1 - UQ) # Results data <- data.frame(cat, ppg_inc, ppg_dec) prop <- plot_ly(data, x = ~cat, y = ~ppg_inc, type = 'bar', marker = list(color = '#01286b'), name = 'Proportion of Players with Increased Scoring') %>% add_trace(y = ~ppg_dec, name = 'Proportion of Players with Decreased Scoring', marker = list(color = '#5469b4')) %>% layout(yaxis = list(title = 'Ratio'), xaxis = list(title = 'Range'), barmode = 'stack') ``` Row ----------------------------------------------------------------------- ### Points Per Game ```{r} ppg ``` ### Rebounds Per Game ```{r} rpg ``` Row ----------------------------------------------------------------------- ### Assists Per Game ```{r} apg ``` ### Field Goal Percentage ```{r} fgp ``` Row ----------------------------------------------------------------------- ### Aggregate Table of Playoff Performance ```{r} datatable(table_view) ``` Row ----------------------------------------------------------------------- ### Proportion of Players with Increased PPPG ```{r} prop ```