library(ggplot2)
library(grid)
setwd("~/Desktop/Mark NFL")
data <-read.csv("nfl_data2.csv")
# Deal with O-U W-L
data$point <-paste(substring(data$total_result,1,1),substring(data$home_team.1,1,1), sep = "")
##look at the data table
data
##    year away_team home_team score_away score_home vegas_home_line
## 1  2013       CHI        GB         27         20           -10.5
## 2  2013        GB       CHI         33         28             3.0
## 3  2014        GB       CHI         38         17             2.0
## 4  2014       CHI        GB         14         55            -7.5
## 5  2015        GB       CHI         31         23             7.0
## 6  2015       CHI        GB         17         13            -8.0
## 7  2016       CHI        GB         10         26            -7.5
## 8  2016        GB       CHI         30         27             4.5
## 9  2017       CHI        GB         14         35            -7.0
## 10 2017        GB       CHI         23         16            -4.5
## 11 2018       CHI        GB         NA         NA            -8.0
##    vegas_total total_score total_result home_team.1 point
## 1         50.5          47         Over        Loss    OL
## 2         51.0          61        Under        Loss    UL
## 3         51.0          55        Under        Loss    UL
## 4         53.0          69        Under         Win    UW
## 5         48.0          54        Under        Loss    UL
## 6         45.0          30         Over        Loss    OL
## 7         46.5          36         Over         Win    OW
## 8         38.0          57        Under        Loss    UL
## 9         44.0          49        Under         Win    UW
## 10        38.0          39        Under        Loss    UL
## 11        48.0           0         Over        Loss    OL
# Graph 1 vegas_total ~ vegas_home_line
plot1 <- ggplot(data = data, aes(x = vegas_home_line, y = vegas_total, shape = as.factor(year)))
plot1 <- plot1 + geom_point() +geom_text(aes(label=point),hjust=0, vjust=0)
# Graph 2 Year ~ vegas_home_line
plot2 <- ggplot(data = data, aes(x = vegas_home_line, y = year, color = home_team.1))
plot2 <- plot2 + geom_point() +geom_text(aes(label=substring(point,1,1),hjust=0, vjust=0))
                                                                                                                
# Graph 3 Year ~ vegas_total
plot3 <- ggplot(data = data, aes(x = vegas_total, y = year, color = home_team.1))
plot3 <-plot3 + geom_point() + geom_text(aes(label=substring(point,2),hjust=0, vjust=0))

# show graphs in grid
# define grid function for ggplot
vp.layout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)
arrange <- function(..., nrow=NULL, ncol=NULL, as.table=FALSE) {
  dots <- list(...)
  n <- length(dots)
  if(is.null(nrow) & is.null(ncol)) { nrow = floor(n/2) ; ncol = ceiling(n/nrow)}
  if(is.null(nrow)) { nrow = ceiling(n/ncol)}
  if(is.null(ncol)) { ncol = ceiling(n/nrow)}
  ## NOTE see n2mfrow in grDevices for possible alternative
  grid.newpage()
  pushViewport(viewport(layout=grid.layout(nrow,ncol) ) )
  ii.p <- 1
  for(ii.row in seq(1, nrow)){
    ii.table.row <- ii.row
    if(as.table) {ii.table.row <- nrow - ii.table.row + 1}
    for(ii.col in seq(1, ncol)){
      ii.table <- ii.p
      if(ii.p > n) break
      print(dots[[ii.table]], vp=vp.layout(ii.table.row, ii.col))
      ii.p <- ii.p + 1
    }
  }
}
arrange(plot1, plot2, plot3)

plot1

plot2

plot3