Advertisement
Guest User

Untitled

a guest
Mar 8th, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. library(tidyverse)
  2. library(rvest)
  3. library(janitor)
  4. library(lubridate)
  5. library(directlabels)
  6. library(ggrepel)
  7. library(ggthemes)
  8.  
  9. setwd("Development/r/2103.nhl")
  10.  
  11. leafs <- read_html("https://en.wikipedia.org/wiki/2020%E2%80%9321_Toronto_Maple_Leafs_season") %>%
  12. html_table(fill = TRUE) %>%
  13. .[[4]] %>%
  14. row_to_names(row_number = 2) %>%
  15. select(2,4) %>%
  16. filter(!Date %in% c("Game", "Date")) %>%
  17. filter(!grepl("schedule", Date)) %>%
  18. filter(Score != "–") %>%
  19. separate(Score, c("HomeScore", "VisitorScore"), sep = "–") %>%
  20. mutate(result = case_when(HomeScore > VisitorScore ~ 1,
  21. TRUE ~ -1)) %>%
  22. mutate(Leafs = cumsum(result)) %>%
  23. select(Date, Leafs) %>%
  24. mutate(Date = mdy(paste(Date, "2021", sep = " ")))
  25.  
  26. jets <- read_html("https://en.wikipedia.org/wiki/2020%E2%80%9321_Winnipeg_Jets_season") %>%
  27. html_table(fill = TRUE) %>%
  28. .[[4]] %>%
  29. row_to_names(row_number = 3) %>%
  30. select(2:5) %>%
  31. filter(!Date %in% c("Game", "Date")) %>%
  32. filter(!grepl("Road|Legend", Date)) %>%
  33. filter(grepl("–", Score)) %>%
  34. separate(Score, c("VisitorScore", "HomeScore"), sep = "–") %>%
  35. mutate(result = case_when(HomeScore > VisitorScore & Home == "Winnipeg" ~ 1,
  36. HomeScore > VisitorScore & Home != "Winnipeg" ~ -1,
  37. HomeScore < VisitorScore & Visitor != "Winnipeg" ~ -1,
  38. HomeScore < VisitorScore & Visitor == "Winnipeg" ~ 1)) %>%
  39. mutate(Jets = cumsum(result)) %>%
  40. select(Date, Jets) %>%
  41. mutate(Date = mdy(paste(Date, "2021", sep = " ")))
  42.  
  43.  
  44. oilers <- read_html("https://en.wikipedia.org/wiki/2020%E2%80%9321_Edmonton_Oilers_season") %>%
  45. html_table(fill = TRUE) %>%
  46. .[[3]] %>%
  47. row_to_names(row_number = 3) %>%
  48. select(2:5) %>%
  49. filter(!Date %in% c("Game", "Date")) %>%
  50. filter(!grepl("Road|Legend", Date)) %>%
  51. filter(grepl("–", Score)) %>%
  52. separate(Score, c("VisitorScore", "HomeScore"), sep = "–") %>%
  53. mutate(result = case_when(HomeScore > VisitorScore & Home == "Edmonton" ~ 1,
  54. HomeScore > VisitorScore & Home != "Edmonton" ~ -1,
  55. HomeScore < VisitorScore & Visitor != "Edmonton" ~ -1,
  56. HomeScore < VisitorScore & Visitor == "Edmonton" ~ 1)) %>%
  57. mutate(Oilers = cumsum(result)) %>%
  58. select(Date, Oilers) %>%
  59. mutate(Date = mdy(paste(Date, "2021", sep = " ")))
  60.  
  61. habs <- read_html("https://en.wikipedia.org/wiki/2020%E2%80%9321_Montreal_Canadiens_season") %>%
  62. html_table(fill = TRUE) %>%
  63. .[[3]] %>%
  64. row_to_names(row_number = 3) %>%
  65. select(2:5) %>%
  66. filter(!Date %in% c("Game", "Date")) %>%
  67. filter(!grepl("Road|Legend", Date)) %>%
  68. filter(grepl("–", Score)) %>%
  69. separate(Score, c("VisitorScore", "HomeScore"), sep = "–") %>%
  70. mutate(result = case_when(HomeScore > VisitorScore & Home == "Montreal" ~ 1,
  71. HomeScore > VisitorScore & Home != "Montreal" ~ -1,
  72. HomeScore < VisitorScore & Visitor != "Montreal" ~ -1,
  73. HomeScore < VisitorScore & Visitor == "Montreal" ~ 1)) %>%
  74. mutate(Habs = cumsum(result)) %>%
  75. select(Date, Habs) %>%
  76. mutate(Date = mdy(paste(Date, "2021", sep = " ")))
  77.  
  78. flames <- read_html("https://en.wikipedia.org/wiki/2020%E2%80%9321_Calgary_Flames_season") %>%
  79. html_table(fill = TRUE) %>%
  80. .[[3]] %>%
  81. row_to_names(row_number = 3) %>%
  82. select(2:5) %>%
  83. filter(!Date %in% c("Game", "Date")) %>%
  84. filter(!grepl("Road|Legend", Date)) %>%
  85. filter(grepl("–", Score)) %>%
  86. separate(Score, c("VisitorScore", "HomeScore"), sep = "–") %>%
  87. mutate(result = case_when(HomeScore > VisitorScore & Home == "Calgary" ~ 1,
  88. HomeScore > VisitorScore & Home != "Calgary" ~ -1,
  89. HomeScore < VisitorScore & Visitor != "Calgary" ~ -1,
  90. HomeScore < VisitorScore & Visitor == "Calgary" ~ 1)) %>%
  91. mutate(Flames = cumsum(result)) %>%
  92. select(Date, Flames) %>%
  93. mutate(Date = mdy(paste(Date, "2021", sep = " ")))
  94.  
  95. canucks <- read_html("https://en.wikipedia.org/wiki/2020%E2%80%9321_Vancouver_Canucks_season") %>%
  96. html_table(fill = TRUE) %>%
  97. .[[3]] %>%
  98. row_to_names(row_number = 3) %>%
  99. select(2:5) %>%
  100. filter(!Date %in% c("Game", "Date")) %>%
  101. filter(!grepl("Road|Legend", Date)) %>%
  102. filter(grepl("–", Score)) %>%
  103. separate(Score, c("VisitorScore", "HomeScore"), sep = "–") %>%
  104. mutate(result = case_when(HomeScore > VisitorScore & Home == "Vancouver" ~ 1,
  105. HomeScore > VisitorScore & Home != "Vancouver" ~ -1,
  106. HomeScore < VisitorScore & Visitor != "Vancouver" ~ -1,
  107. HomeScore < VisitorScore & Visitor == "Vancouver" ~ 1)) %>%
  108. mutate(Canucks = cumsum(result)) %>%
  109. na.omit() %>%
  110. select(Date, Canucks) %>%
  111. mutate(Date = mdy(paste(Date, "2021", sep = " ")))
  112.  
  113. senators <- read_html("https://en.wikipedia.org/wiki/2020%E2%80%9321_Ottawa_Senators_season") %>%
  114. html_table(fill = TRUE) %>%
  115. .[[3]] %>%
  116. row_to_names(row_number = 3) %>%
  117. select(2:5) %>%
  118. filter(!Date %in% c("Game", "Date")) %>%
  119. filter(!grepl("Road|Legend", Date)) %>%
  120. filter(grepl("–", Score)) %>%
  121. separate(Score, c("VisitorScore", "HomeScore"), sep = "–") %>%
  122. mutate(result = case_when(HomeScore > VisitorScore & Home == "Ottawa" ~ 1,
  123. HomeScore > VisitorScore & Home != "Ottawa" ~ -1,
  124. HomeScore < VisitorScore & Visitor != "Ottawa" ~ -1,
  125. HomeScore < VisitorScore & Visitor == "Ottawa" ~ 1)) %>%
  126. mutate(Senators = cumsum(result)) %>%
  127. select(Date, Senators) %>%
  128. mutate(Date = mdy(paste(Date, "2021", sep = " ")))
  129.  
  130.  
  131. canucks %>%
  132. full_join(flames) %>%
  133. full_join(habs) %>%
  134. full_join(jets) %>%
  135. full_join(leafs) %>%
  136. full_join(oilers) %>%
  137. full_join(senators) %>%
  138. arrange(Date) %>%
  139. gather(Team, record, 2:8) %>%
  140. as_tibble() %>%
  141. na.omit() %>%
  142. group_by(Team) %>%
  143. mutate(label = if_else(Date == max(Date),
  144. as.character(Team),
  145. NA_character_)) %>%
  146. mutate(col = case_when(Team == "Leafs" ~ "Leafs",
  147. Team == "Senators" ~ "Senators",
  148. TRUE ~ "Other")) %>%
  149. ggplot(aes(Date, record, color = col, group = Team, linetype = Team)) +
  150. geom_point() +
  151. geom_path() +
  152. geom_hline(yintercept = 0) +
  153. geom_label_repel(aes(label = label), nudge_x = 1, na.rm = TRUE) +
  154. labs(x = "2021 NHL Season to Date",
  155. y = "",
  156. title = "A tale of seven cities: NHL North records to date",
  157. subtitle = "Cumulative games above / below .500 since January") +
  158. scale_color_manual(values = c("blue", "grey", "red")) +
  159. theme_bw() +
  160. theme(legend.position = "none")
  161.  
  162. ggsave("nhlplot.png", width = 10, height = 6)
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement