// two "supported" packages, we can skip the full dependency & import boilerplate
%use lets-plot, krangl
// csv is courtesy of pro-football-reference: https://www.pro-football-reference.com/years/NFL/passing.htm
val dfPassing = DataFrame.readCSV("nfl_passing.csv")
dfPassing
val mapPassing = dfPassing.filter { (it["Year"] lt 2020) AND (it["Year"] gt 1989) }.toMap()
mapPassing
val p = lets_plot(mapPassing) { x = "Year"; y = "AY/A" } + ggsize(640, 240)
p + geom_bar(stat=Stat.identity) +
ggtitle("Avg Adjusted Yds/Attempt per NFL regular season")
val p = lets_plot(mapPassing) { x = "Year"; y = "Rate" } + ggsize(640, 240)
p + geom_bar(stat=Stat.identity) +
ggtitle("Total Combined Passer Rating per NFL regular season")
val dfPassingRanges = dfPassing
.filter { (it["Year"] lt 2020) AND (it["Year"] gt 1989) }
.addColumn("YearRange") { it["Year"].map<Double>{ floor(it.div(5.0)).times(5).toInt() }}
.addColumn("Years") { it["YearRange"].map<Int>{ "$it - ${it + 4}" }}
val mapPassingRanges = dfPassingRanges
.select({ listOf("Year", "AY/A", "Rate", "YearRange", "Years") })
.groupBy("YearRange", "Years")
.summarize(
"mean_AY/A" to { it["AY/A"].mean(removeNA = true) },
"mean_Rate" to { it["Rate"].mean(removeNA = true) }
).toMap()
val xlimits = listOf("1990 - 1994", "1995 - 1999", "2000 - 2004", "2005 - 2009", "2010 - 2014", "2015 - 2019")
val p = lets_plot(mapPassingRanges) { x = "Years"; y = "mean_AY/A" } + ggsize(720, 240)
p + geom_bar(stat=Stat.identity) + scale_x_discrete(limits = xlimits) +
scale_y_continuous(limits = Pair(4.0, 8.0)) +
ggtitle("Average Adjusted Yards/Attempt per NFL regular season")
val p = lets_plot(mapPassingRanges) { x = "Years"; y = "mean_Rate" } + ggsize(720, 240)
p + geom_bar(stat=Stat.identity) + scale_x_discrete(limits = xlimits) +
ggtitle("Total Combined Passer Rating per NFL regular season")