Quick, simple visualization of wins/losses/pushes against the spread (ATS) for each NFL team for each week of 2013. First prepare data:
# read data
df <- read.csv("http://www.repole.com/sun4cast/stats/nfl2013stats.csv", stringsAsFactors = FALSE)
# transform line so, for example, favorites are -3 (not +3)
df$Line <- df$Line * -1
# calculate result against the spread
score_diff <- df$ScoreOff - df$ScoreDef
line_diff <- score_diff + df$Line
cover <- c()
i <- 1
for (i in i:length(line_diff)) {
if (line_diff[i] > 0) {
cover[i] <- "Win"
} else if (line_diff[i] < 0) {
cover[i] <- "Loss"
} else {
cover[i] <- "Push"
}
}
# add cover column
df <- cbind(df, cover)
# the data contains date of game but not week of the season so I determine
# the week based on the difference between the first day in the data set
# first convert date to date type
df$Date <- as.Date(df$Date, "%m/%d/%Y")
## initialize week vector
wk <- c()
## for loop comparing the difference between each date to the first date of
## year
i <- 1
for (i in i:nrow(df)) {
dt_diff <- abs(as.integer(difftime(time1 = df[1, 1], time2 = df[i, 1], units = "days")))
wk <- append(wk, dt_diff)
}
# take integer of date diff values, add one (turn 0 to 1, etc)
week <- as.integer((wk/7) + 1)
## combine week value to data frame
df <- cbind(df, week)
# clean up
rm(i, dt_diff, wk, week, cover, line_diff, score_diff)
# limit to data frame to plot data
df <- df[, c("TeamName", "week", "cover")]
# reorder factor levels for plot legend
df$cover <- factor(df$cover, levels = c("Win", "Loss", "Push"))
head(df)
## TeamName week cover
## 1 Baltimore Ravens 1 Loss
## 2 Denver Broncos 1 Win
## 3 Arizona Cardinals 1 Win
## 4 Atlanta Falcons 1 Loss
## 5 Buffalo Bills 1 Win
## 6 Carolina Panthers 1 Loss
Then the plot:
# create graph
library(ggplot2)
ggplot(df, aes(x = factor(TeamName), y = week, colour = cover)) + geom_point(size = 9,
shape = 15) + coord_flip() + theme(axis.title.y = element_blank(), axis.title.x = element_blank(),
legend.title = element_blank()) + scale_colour_manual(values = c("#00BA38",
"#F8766D", "#619CFF")) + scale_y_discrete(breaks = unique(df$week)) + ggtitle("ATS Results 2013 by Team and Week")
A couple notes: