class: inverse, left, middle # An Introduction to Hockey Analytics with R ## Meghan Hall UConn Sports Analytics Symposium<br> October 9, 2021 <br> <br> <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg> [@MeghanMHall](https://www.twitter.com/MeghanMHall) <br> <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> [meghall6](https://github.com/meghall06) <br> <svg viewBox="0 0 640 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M624 416H381.54c-.74 19.81-14.71 32-32.74 32H288c-18.69 0-33.02-17.47-32.77-32H16c-8.8 0-16 7.2-16 16v16c0 35.2 28.8 64 64 64h512c35.2 0 64-28.8 64-64v-16c0-8.8-7.2-16-16-16zM576 48c0-26.4-21.6-48-48-48H112C85.6 0 64 21.6 64 48v336h512V48zm-64 272H128V64h384v256z"></path></svg> [meghan.rbind.io](https://meghan.rbind.io/) --- layout: true <div class="my-footer"><span>tinyurl.com/ucsas21</span></div> --- # About ~~me~~ you <br> <br> .large[You know a bit about R...] <br> .large[...and a bit about hockey] --- layout: true <div class="my-footer"><span>tinyurl.com/ucsas21</span></div> --- .left[# About me] <br> .pull-left[ ## π° π data manager in higher ed data scientist at Zelus Analytics ] -- .pull-right[ ## π π° hockey analysis courses + tutorials + workshops ] --- # Today's agenda <br> .large[Explore how R makes hockey analysis easier]<br> .medium[Data manipulation, via the β`tidyverse`β and data visualization] <br> .large[Go through a basic hockey analysis question] -- <br> .large[*A Day in the Life of an NHL Power Play*] --- # Data sources <br> <img src="figs/data.gif" width="70%" style="display: block; margin: auto;" /> --- # Data sources .large[Aggregate data freely available for most leagues]<br> .medium[league websites, eliteprospects.com]<br> .medium[some event data for AHL & CHL]<br> .medium[no TOI π] <br> .large[[Big Data Cup](https://github.com/bigdatacup/Big-Data-Cup-2021)] <br> .large[Specialty sources, usually for a fee]<br> .medium[[Their Hockey Counts](https://theirhockeycounts.com/index.html#about)]<br> .medium[[CHL microstats](https://www.patreon.com/user/overview?u=13951676)]<br> .medium[[NHL microstats](https://www.patreon.com/CSznajder)]<br> --- # hockeyR <img src="figs/hockeyR.png" width="20%" style="display: block; margin: auto;" /> .large[From Dan Morse: [hockeyr.netlify.app/](https://hockeyr.netlify.app/)] <br> .large[Various functions to scrape NHL play-by-play data] <br> .large[Other helper functions to assist with analysis] --- # Today's packages ```r library(tidyverse) library(janitor) library(hockeyR) library(sportyR) library(gt) library(gtExtras) library(ggtext) ``` --- # hockeyR sample functions from `hockeyR` ```r scrape_game(2020020322) scrape_day(day = "2021-02-27") scrape_season(season = 2020, type = "REG") ``` -- <br> today's data ```r pbp <- scrape_day(day = "2021-02-27") ``` --- # What is play-by-play data? .medium[`pbp` from `hockeyR` has 105 variables]<br> One row per event, with location (`x` and `y`), players on ice, etc. -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> event_type </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:right;"> 4401 </td> </tr> <tr> <td style="text-align:left;"> FACEOFF </td> <td style="text-align:right;"> 813 </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:right;"> 778 </td> </tr> <tr> <td style="text-align:left;"> STOP </td> <td style="text-align:right;"> 629 </td> </tr> <tr> <td style="text-align:left;"> HIT </td> <td style="text-align:right;"> 606 </td> </tr> <tr> <td style="text-align:left;"> BLOCKED_SHOT </td> <td style="text-align:right;"> 346 </td> </tr> <tr> <td style="text-align:left;"> MISSED_SHOT </td> <td style="text-align:right;"> 300 </td> </tr> <tr> <td style="text-align:left;"> GIVEAWAY </td> <td style="text-align:right;"> 222 </td> </tr> <tr> <td style="text-align:left;"> TAKEAWAY </td> <td style="text-align:right;"> 156 </td> </tr> <tr> <td style="text-align:left;"> GOAL </td> <td style="text-align:right;"> 93 </td> </tr> <tr> <td style="text-align:left;"> PENALTY </td> <td style="text-align:right;"> 91 </td> </tr> <tr> <td style="text-align:left;"> PERIOD_END </td> <td style="text-align:right;"> 48 </td> </tr> <tr> <td style="text-align:left;"> GAME_END </td> <td style="text-align:right;"> 14 </td> </tr> <tr> <td style="text-align:left;"> GAME_SCHEDULED </td> <td style="text-align:right;"> 14 </td> </tr> <tr> <td style="text-align:left;"> CHALLENGE </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> SHOOTOUT_COMPLETE </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> --- # What is play-by-play data? .medium[`pbp` from `hockeyR` has 105 variables]<br> One row per event, with location (`x` and `y`), players on ice, etc. <br> .large[Tracking data??] --- # The data we want ```r powerplay <- pbp %>% mutate(length = lead(period_seconds) - period_seconds, PP_team = case_when((event_team_type == "home" & strength_state == "5v4") | (event_team_type == "away" & strength_state == "4v5") ~ home_abbreviation, TRUE ~ away_abbreviation)) %>% filter(strength_state %in% c("5v4","4v5") & (length > 0 | event_type %in% c("GOAL","SHOT"))) %>% select(event_type, event_team_abbr, event_team_type, home_abbreviation, away_abbreviation, PP_team, length, strength_state, x:y_fixed, home_on_1:away_on_7, event_idx, game_id, period) %>% remove_empty("cols") ``` --- # The data we want <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> PP_team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> length </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> home_on_1 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> home_on_2 </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> game_id </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> event_idx </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> FACEOFF </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> Jacob.Bryson </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> Jacob.Bryson </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> </tr> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> Jacob.Bryson </td> <td style="text-align:left;"> Cody.Eakin </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 39 </td> </tr> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 3 </td> <td style="text-align:left;"> Cody.Eakin </td> <td style="text-align:left;"> Tobias.Rieder </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 40 </td> </tr> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 5 </td> <td style="text-align:left;"> Cody.Eakin </td> <td style="text-align:left;"> Tobias.Rieder </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 41 </td> </tr> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 10 </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:left;"> Rasmus.Ristolainen </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 42 </td> </tr> <tr> <td style="text-align:left;"> GIVEAWAY </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 31 </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:left;"> Rasmus.Ristolainen </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 43 </td> </tr> <tr> <td style="text-align:left;"> BLOCKED_SHOT </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:left;"> Rasmus.Ristolainen </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 44 </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 0 </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:left;"> Rasmus.Ristolainen </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 45 </td> </tr> <tr> <td style="text-align:left;"> FACEOFF </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 6 </td> <td style="text-align:left;"> Jacob.Bryson </td> <td style="text-align:left;"> Cody.Eakin </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 50 </td> </tr> </tbody> </table> --- # The question we have .large[*A Day in the Life of an NHL Power Play*] <br> -- .large[Position breakdown of a power play: usually 4F/1D, sometimes 3F/2D] <br> .large[How does that vary among these teams for this one day of games?] --- # The data we have what is missing and what structure do we need? <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> PP_team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> length </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> home_on_1 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> home_on_2 </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> game_id </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> event_idx </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> FACEOFF </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> Jacob.Bryson </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> Jacob.Bryson </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> </tr> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> Jacob.Bryson </td> <td style="text-align:left;"> Cody.Eakin </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 39 </td> </tr> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 3 </td> <td style="text-align:left;"> Cody.Eakin </td> <td style="text-align:left;"> Tobias.Rieder </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 40 </td> </tr> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 5 </td> <td style="text-align:left;"> Cody.Eakin </td> <td style="text-align:left;"> Tobias.Rieder </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 41 </td> </tr> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 10 </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:left;"> Rasmus.Ristolainen </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 42 </td> </tr> <tr> <td style="text-align:left;"> GIVEAWAY </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 31 </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:left;"> Rasmus.Ristolainen </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 43 </td> </tr> <tr> <td style="text-align:left;"> BLOCKED_SHOT </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:left;"> Rasmus.Ristolainen </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 44 </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 0 </td> <td style="text-align:left;"> Curtis.Lazar </td> <td style="text-align:left;"> Rasmus.Ristolainen </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 45 </td> </tr> <tr> <td style="text-align:left;"> FACEOFF </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 6 </td> <td style="text-align:left;"> Jacob.Bryson </td> <td style="text-align:left;"> Cody.Eakin </td> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 50 </td> </tr> </tbody> </table> --- # Changing the level of observation ```r player <- powerplay %>% mutate(PP_1 = ifelse(PP_team == home_abbreviation, home_on_1, away_on_1), PP_2 = ifelse(PP_team == home_abbreviation, home_on_2, away_on_2), PP_3 = ifelse(PP_team == home_abbreviation, home_on_3, away_on_3), PP_4 = ifelse(PP_team == home_abbreviation, home_on_4, away_on_4), PP_5 = ifelse(PP_team == home_abbreviation, home_on_5, away_on_5)) %>% select(game_id, event_idx, contains("PP"), length) %>% pivot_longer(PP_1:PP_5, values_to = "player") ``` --- # Changing the level of observation <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> game_id </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> event_idx </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> PP_team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> length </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> name </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> player </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_1 </td> <td style="text-align:left;"> Kevin.Hayes </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_2 </td> <td style="text-align:left;"> James.van.Riemsdyk </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_3 </td> <td style="text-align:left;"> Ivan.Provorov </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_4 </td> <td style="text-align:left;"> Sean.Couturier </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_5 </td> <td style="text-align:left;"> Joel.Farabee </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_1 </td> <td style="text-align:left;"> Kevin.Hayes </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_2 </td> <td style="text-align:left;"> James.van.Riemsdyk </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_3 </td> <td style="text-align:left;"> Ivan.Provorov </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_4 </td> <td style="text-align:left;"> Sean.Couturier </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_5 </td> <td style="text-align:left;"> Joel.Farabee </td> </tr> </tbody> </table> --- # Adding position data ```r roster <- get_rosters(team = "all", season = 2021) ``` -- ```r position <- roster %>% select(player, position) %>% unique() ``` -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> player </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> position </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Andrew Agozzino </td> <td style="text-align:left;"> LW </td> </tr> <tr> <td style="text-align:left;"> David Backes </td> <td style="text-align:left;"> C </td> </tr> <tr> <td style="text-align:left;"> Simon Benoit </td> <td style="text-align:left;"> D </td> </tr> <tr> <td style="text-align:left;"> Sam Carrick </td> <td style="text-align:left;"> C </td> </tr> <tr> <td style="text-align:left;"> Max Comtois </td> <td style="text-align:left;"> LW </td> </tr> <tr> <td style="text-align:left;"> Chase De Leo </td> <td style="text-align:left;"> C </td> </tr> <tr> <td style="text-align:left;"> Nicolas Deslauriers </td> <td style="text-align:left;"> LW </td> </tr> <tr> <td style="text-align:left;"> Jamie Drysdale </td> <td style="text-align:left;"> D </td> </tr> <tr> <td style="text-align:left;"> Haydn Fleury </td> <td style="text-align:left;"> D </td> </tr> </tbody> </table> --- # Adding position data ```r position %>% get_dupes(player) ``` ``` ## # A tibble: 2 x 3 ## player dupe_count position ## <chr> <int> <chr> ## 1 Sebastian Aho 2 F ## 2 Sebastian Aho 2 D ``` -- ```r position <- position %>% filter(!(player == "Sebastian Aho" & position == "D")) position <- position %>% mutate(player = str_replace_all(player, " ", "."), player = str_replace_all(player, "-", "."), player = str_replace_all(player, "'", ".")) ``` -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> player </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> position </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Andrew.Agozzino </td> <td style="text-align:left;"> LW </td> </tr> </tbody> </table> --- # Adding position data joining data by name can be π₯΄ ```r player %>% left_join(position, by = "player") %>% filter(is.na(position)) %>% select(player) %>% unique() ``` -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> player </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Josh.Norris </td> </tr> <tr> <td style="text-align:left;"> Mitchell.Marner </td> </tr> <tr> <td style="text-align:left;"> Nick.Caamano </td> </tr> <tr> <td style="text-align:left;"> Cal.Foote </td> </tr> <tr> <td style="text-align:left;"> Alex.Wennberg </td> </tr> <tr> <td style="text-align:left;"> Dominik.Kubalik </td> </tr> <tr> <td style="text-align:left;"> Matt.Dumba </td> </tr> </tbody> </table> --- # Adding position data ```r position <- position %>% mutate(player = case_when(player == "Joshua.Norris" ~ "Josh.Norris", player == "Mitch.Marner" ~ "Mitchell.Marner", player == "Callan.Foote" ~ "Cal.Foote", player == "Nicholas.Caamano" ~ "Nick.Caamano", player == "Alexander.Wennberg" ~ "Alex.Wennberg", player == "Dominik.KubalΓk" ~ "Dominik.Kubalik", player == "Mathew.Dumba" ~ "Matt.Dumba", TRUE ~ player)) ``` -- ```r player <- player %>% left_join(position, by = "player") %>% mutate(forward = ifelse(str_detect(position, "D"), 0, 1)) ``` --- # Adding position data <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> game_id </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> event_idx </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> PP_team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> length </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> name </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> player </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> position </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> forward </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_1 </td> <td style="text-align:left;"> Kevin.Hayes </td> <td style="text-align:left;"> RW </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_2 </td> <td style="text-align:left;"> James.van.Riemsdyk </td> <td style="text-align:left;"> LW </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_3 </td> <td style="text-align:left;"> Ivan.Provorov </td> <td style="text-align:left;"> D </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_4 </td> <td style="text-align:left;"> Sean.Couturier </td> <td style="text-align:left;"> C </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> PP_5 </td> <td style="text-align:left;"> Joel.Farabee </td> <td style="text-align:left;"> LW </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_1 </td> <td style="text-align:left;"> Kevin.Hayes </td> <td style="text-align:left;"> RW </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_2 </td> <td style="text-align:left;"> James.van.Riemsdyk </td> <td style="text-align:left;"> LW </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_3 </td> <td style="text-align:left;"> Ivan.Provorov </td> <td style="text-align:left;"> D </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_4 </td> <td style="text-align:left;"> Sean.Couturier </td> <td style="text-align:left;"> C </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> PP_5 </td> <td style="text-align:left;"> Joel.Farabee </td> <td style="text-align:left;"> LW </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> --- # Changing the level of detail ```r fwd <- player %>% group_by(game_id, event_idx, PP_team) %>% summarize(fwds = sum(forward), length = mean(length)) %>% group_by(PP_team, fwds) %>% summarize(time = sum(length)) %>% add_tally(time, n = "total_time") %>% mutate(perc = time / total_time) %>% filter(fwds == 4) ``` --- # Changing the level of detail ```r fwd <- player %>% `group_by(game_id, event_idx, PP_team)` %>% `summarize(fwds = sum(forward),` `length = mean(length))` %>% group_by(PP_team, fwds) %>% summarize(time = sum(length)) %>% add_tally(time, n = "total_time") %>% mutate(perc = time / total_time) %>% filter(fwds == 4) ``` <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> game_id </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> event_idx </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> PP_team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> fwds </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> length </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 18 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 38 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 7 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 39 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 34 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 40 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 3 </td> </tr> <tr> <td style="text-align:right;"> 2020020322 </td> <td style="text-align:right;"> 41 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 5 </td> </tr> </tbody> </table> --- # Changing the level of detail ```r fwd <- player %>% group_by(game_id, event_idx, PP_team) %>% summarize(fwds = sum(forward), length = mean(length)) %>% `group_by(PP_team, fwds)` %>% `summarize(time = sum(length))` %>% add_tally(time, n = "total_time") %>% mutate(perc = time / total_time) %>% filter(fwds == 4) ``` <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> PP_team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> fwds </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> time </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> ARI </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 74 </td> </tr> <tr> <td style="text-align:left;"> ARI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 166 </td> </tr> <tr> <td style="text-align:left;"> BUF </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 101 </td> </tr> <tr> <td style="text-align:left;"> BUF </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 174 </td> </tr> <tr> <td style="text-align:left;"> CAR </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 32 </td> </tr> </tbody> </table> --- # Changing the level of detail ```r fwd <- player %>% group_by(game_id, event_idx, PP_team) %>% summarize(fwds = sum(forward), length = mean(length)) %>% group_by(PP_team, fwds) %>% summarize(time = sum(length)) %>% `add_tally(time, n = "total_time")` %>% `mutate(perc = time / total_time)` %>% filter(fwds == 4) ``` <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> PP_team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> fwds </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> time </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> total_time </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> perc </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> ARI </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 74 </td> <td style="text-align:right;"> 240 </td> <td style="text-align:right;"> 0.3083333 </td> </tr> <tr> <td style="text-align:left;"> ARI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 166 </td> <td style="text-align:right;"> 240 </td> <td style="text-align:right;"> 0.6916667 </td> </tr> <tr> <td style="text-align:left;"> BUF </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 101 </td> <td style="text-align:right;"> 275 </td> <td style="text-align:right;"> 0.3672727 </td> </tr> <tr> <td style="text-align:left;"> BUF </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 174 </td> <td style="text-align:right;"> 275 </td> <td style="text-align:right;"> 0.6327273 </td> </tr> <tr> <td style="text-align:left;"> CAR </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 32 </td> <td style="text-align:right;"> 250 </td> <td style="text-align:right;"> 0.1280000 </td> </tr> </tbody> </table> --- # Changing the level of detail ```r fwd <- player %>% group_by(game_id, event_idx, PP_team) %>% summarize(fwds = sum(forward), length = mean(length)) %>% group_by(PP_team, fwds) %>% summarize(time = sum(length)) %>% add_tally(time, n = "total_time") %>% mutate(perc = time / total_time) %>% filter(fwds == 4) ``` <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> PP_team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> fwds </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> time </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> total_time </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #661414 !important;"> perc </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> ARI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 166 </td> <td style="text-align:right;"> 240 </td> <td style="text-align:right;"> 0.6916667 </td> </tr> <tr> <td style="text-align:left;"> BUF </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 174 </td> <td style="text-align:right;"> 275 </td> <td style="text-align:right;"> 0.6327273 </td> </tr> <tr> <td style="text-align:left;"> CAR </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 218 </td> <td style="text-align:right;"> 250 </td> <td style="text-align:right;"> 0.8720000 </td> </tr> <tr> <td style="text-align:left;"> CBJ </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 319 </td> <td style="text-align:right;"> 350 </td> <td style="text-align:right;"> 0.9114286 </td> </tr> <tr> <td style="text-align:left;"> CGY </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 373 </td> <td style="text-align:right;"> 373 </td> <td style="text-align:right;"> 1.0000000 </td> </tr> <tr> <td style="text-align:left;"> CHI </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 120 </td> <td style="text-align:right;"> 120 </td> <td style="text-align:right;"> 1.0000000 </td> </tr> </tbody> </table> --- # Using `team_logos_colors` ```r logos <- team_logos_colors ``` -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> team_abbr </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> team_color1 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> team_color2 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #661414 !important;"> team_logo_espn </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> ANA </td> <td style="text-align:left;"> #F47A38 </td> <td style="text-align:left;"> #B9975B </td> <td style="text-align:left;"> https://a.espncdn.com/i/teamlogos/nhl/500/ANA.png </td> </tr> <tr> <td style="text-align:left;"> ARI </td> <td style="text-align:left;"> #8C2633 </td> <td style="text-align:left;"> #DDCBA4 </td> <td style="text-align:left;"> https://a.espncdn.com/i/teamlogos/nhl/500/ARI.png </td> </tr> <tr> <td style="text-align:left;"> BOS </td> <td style="text-align:left;"> #FFB81C </td> <td style="text-align:left;"> #000000 </td> <td style="text-align:left;"> https://a.espncdn.com/i/teamlogos/nhl/500/BOS.png </td> </tr> <tr> <td style="text-align:left;"> BUF </td> <td style="text-align:left;"> #002654 </td> <td style="text-align:left;"> #FCB514 </td> <td style="text-align:left;"> https://a.espncdn.com/i/teamlogos/nhl/500/BUF.png </td> </tr> <tr> <td style="text-align:left;"> CGY </td> <td style="text-align:left;"> #C8102E </td> <td style="text-align:left;"> #F1BE48 </td> <td style="text-align:left;"> https://a.espncdn.com/i/teamlogos/nhl/500/CGY.png </td> </tr> </tbody> </table> -- ```r fwd <- fwd %>% left_join(select(logos, team_abbr, team_logo_espn, team_color1), by = c("PP_team" = "team_abbr")) ``` --- # Tables with `gt`
Percentage of 5v4 Power Play Time With Four Forwards
Draw no conclusions, this is one night of games!
100%
100%
100%
100%
100%
100%
98%
97%
96%
92%
91%
87%
87%
79%
78%
69%
63%
61%
56%
55%
51%
42%
41%
40%
36%
25%
--- .h-1[# Tables with `gt`] .tiny[ ```r fwd %>% ungroup() %>% select(team_logo_espn, perc) %>% arrange(desc(perc)) %>% gt() %>% tab_header(title = "Percentage of 5v4 Power Play Time With Four Forwards", subtitle = "Draw no conclusions, this is one night of games!") %>% `gt_img_rows(columns = team_logo_espn, height = 25)` %>% `fmt_percent(perc, decimals = 0)` %>% `gt_color_rows(perc,` `palette = "ggsci::blue_grey_material")` %>% tab_style(style = list(cell_fill(color = "white")), locations = cells_body(columns = c(team_logo_espn))) %>% tab_options(table.font.size = 12, heading.title.font.size = 14, table.width = px(250), column_labels.hidden = TRUE, data_row.padding = px(1), table_body.hlines.width = 0, table.border.top.color = 'black', table.border.top.width = 2, heading.border.bottom.color = 'black', heading.border.bottom.width = 2, table_body.border.bottom.color = 'black', table_body.border.bottom.width = 2) ``` ] --- # Plots with `ggplot2` <img src="figs/bar-chart-1.png" width="504" style="display: block; margin: auto;" /> --- # Plots with `ggplot2` ```r fwd %>% ggplot(aes(x = perc, y = reorder(PP_team, perc))) + geom_bar(stat = "identity", `fill = fwd$team_color1`) + scale_x_continuous(label = scales::percent, expand = expansion(mult = c(0, 0.05))) + labs(x = NULL, y = NULL, title = "Percentage of 5v4 power play time with four forwards", subtitle = "Games on Feb. 27, 2021") + theme_linedraw() + ucsas_theme() + theme(plot.title.position = "plot") ``` --- # Shot plots with `sportyR` <img src="figs/sportyR.png" width="20%" style="display: block; margin: auto;" /> .large[From Ross Drucker [<svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg>](https://github.com/rossdrucker/sportyR#readme)] <br> .large[Functions to aid in plotting various sport courts/rinks/fields] --- # Shot plots with `sportyR` `x_fixed` and `y_fixed` correct coordinates so home team shoots to the right, away team to the left<br> ```r PP_shots <- powerplay %>% filter(event_type %in% c("SHOT","GOAL") & event_team_abbr == PP_team) geom_hockey("nhl") + geom_point(data = PP_shots, aes(x = x_fixed, y = y_fixed)) ``` <img src="figs/rink-1.png" width="504" style="display: block; margin: auto;" /> --- # Shot plots with `sportyR` <img src="figs/shot-plot-1.png" width="504" style="display: block; margin: auto;" /> --- # Shot plots with `sportyR` ```r PP_shots <- powerplay %>% filter(event_type %in% c("SHOT","GOAL") & event_team_abbr == PP_team) %>% mutate(x_rotate = ifelse(x > 0, x * -1, x), y_rotate = ifelse(x > 0, y * -1, y)) %>% arrange(desc(event_type)) ``` -- ```r geom_hockey("nhl", `full_surf = FALSE`) + geom_point(data = PP_shots, `aes(x = x_rotate, y = y_rotate)`, size = 6, color = ifelse(PP_shots$event_type == "GOAL", "#661414", "#A9A9A9"), alpha = ifelse(PP_shots$event_type == "GOAL", 1, 0.5)) + labs(title = "Shots and <span style = 'color:#661414;'>**goals**</span> on the 5v4 power play", subtitle = "On the night of February 27, 2021", caption = "Data from hockeyR & plot made with sportyR by @MeghanMHall") + theme(plot.title = element_markdown(hjust = 0.5, vjust = 0.5, size = 16), plot.subtitle = element_text(hjust = 0.5, face = "italic"), plot.caption = element_text(hjust = 0.5)) ``` --- # What we did .large[Explored various data manipulation techniques with the `tidyverse`]<br> .medium[using `stringr` functions, `pivot_longer`, `left_join`]<br> .medium[and the `dplyr` verbs: `select`, `filter`, `mutate`, `group_by`, `summarize`] <br> .large[Made a table with `gt` and a plot with `ggplot2`]<br> .medium[using logos and colors available through `hockeyR`] <br> .large[Made a shot plot with `sportyR`] --- # Questions?? <br> <img src="figs/data.gif" width="70%" style="display: block; margin: auto;" /> --- class: center, middle, inverse # Thank you! Slides created via the R package [xaringan](https://github.com/yihui/xaringan). <br> <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg> [@MeghanMHall](https://www.twitter.com/MeghanMHall)