foul_analysis.R 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Analysis of foul data
  2. library(tidyverse)
  3. allfouls <- read_csv("data/foulsonly.csv")
  4. ## Plots
  5. # histogram of fouls as a function of corrected score margin
  6. png('figures/foul_histogram-all.png')
  7. ggplot(allfouls, aes(x=SCOREMARGIN_CORR)) +
  8. geom_histogram(binwidth=1, fill="black") +
  9. theme_bw() +
  10. scale_y_log10() +
  11. xlab("Score Margin") + ylab("N Fouls")
  12. dev.off()
  13. # histogram of fouls as a function of corrected score margin,
  14. # ignoring overtime and the final minute of regular play
  15. earlyfouls <- filter(allfouls,
  16. PERIOD <= 4,
  17. !(PERIOD == 4 & PCTIMESTRING < "00:01:00"))
  18. png('figures/foul_histogram-regular_nofinalmin.png')
  19. ggplot(earlyfouls, aes(x=SCOREMARGIN_CORR)) +
  20. geom_histogram(binwidth=1, fill="red", alpha=0.5) +
  21. theme_bw() +
  22. scale_y_log10() +
  23. xlab("Score Margin") + ylab("N Fouls") +
  24. geom_histogram(data=allfouls, binwidth=1, fill="green", alpha=0.5)
  25. dev.off()
  26. # hexbin plots of fouls as a function of total score and corrected score
  27. # margin, separated by home and away teams
  28. png('figures/fouls_totalscore-hexbin.png', height=600, width=1200)
  29. ggplot(allfouls, aes(SCOREMARGIN_CORR, TOTALSCORE)) +
  30. geom_hex() +
  31. scale_fill_viridis_c() +
  32. theme_bw() +
  33. facet_wrap(vars(FOULTEAM))
  34. dev.off()