library(directlabels)
library(tidyverse)

theme_custom <- function () { 
  theme(axis.text = element_text(color = "white"),
        axis.ticks = element_blank(),
        axis.line = element_line(colour = "white"),
        axis.title = element_text(color = "white"),
        panel.background = element_rect(fill = "#001D3D"),
        panel.grid = element_blank(),
        plot.subtitle = element_text(color = "white"),
        plot.title = element_text(color = "white"),
        plot.background = element_rect(fill = "#001D3D"))}

df2 <- clipr::read_clip_tbl() %>% 
  as_tibble()

df2 %>% 
  filter(LGE == "NHL") %>% 
  filter(!grepl("Pav|Kess|Perr|Berg|Tav|Spez", Name)) %>% 
  group_by(Name) %>% 
  mutate(Season = row_number(),
         cumG = cumsum(G),
         cumP = cumsum(P)) %>% 
  mutate(label = if_else(Season == max(Season), Name, NA_character_)) %>%
  ggplot(aes(Season, cumG, color = Name)) +
  geom_line() +
  geom_dl(aes(label = label), 
          method = list(dl.combine("last.points"), cex = .8)) +
  expand_limits(x = c(NA, 28)) +
  labs(y = "Total Goals",
       title = "NHL Top 10 Goal Leaders, Active Players",
       subtitle = "Visualizes the cumulative trend for each player from their first season to their most recent.") +
  theme_classic() +
  theme_custom() +
  theme(legend.position = "none")

ggsave("goals_t.png")

df2 %>% 
  filter(LGE == "NHL") %>% 
  filter(!grepl("Pav|Kess|Perr|Berg|Tav|Spez", Name)) %>% 
  group_by(Name) %>% 
  mutate(Season = row_number(),
         cumG = cumsum(G),
         cumP = cumsum(P)) %>% 
  filter(Season < 7) %>% 
  mutate(label = if_else(Season == max(Season), Name, NA_character_)) %>%
  ggplot(aes(Season, cumG, color = Name)) +
  geom_line() +
  geom_point() +
  geom_dl(aes(label = label), 
          method = list(dl.combine("last.points"), cex = .8, hjust = -.1)) +
  expand_limits(x = c(NA, 7)) +
  labs(y = "Total Goals",
       title = "NHL Top 10 Goal Leaders, Active Players - First Six Seasons",
       subtitle = "Visualizes the cumulative trend for each player from their sixth.") +
  theme_classic() +
  theme_custom() +
  theme(legend.position = "none")

ggsave("goals_f6.png")

df2 %>% 
  filter(LGE == "NHL") %>% 
  filter(!grepl("Pav|Kess|Perr|Berg|Tav|Spez", Name)) %>% 
  group_by(Name) %>% 
  mutate(Season = row_number(),
         cumG = cumsum(G),
         cumP = cumsum(P)) %>% 
  mutate(label = if_else(Season == max(Season), Name, NA_character_)) %>%
  ggplot(aes(Season, cumP, color = Name)) +
  geom_line() +
  geom_dl(aes(label = label), 
          method = list(dl.combine("last.points"), cex = .8)) +
  expand_limits(x = c(NA, 28)) +
  labs(y = "Total Points",
       title = "Same Player set, Points Totals, Active Players",
       subtitle = "Visualizes the cumulative trend for each player from their first season to their most recent.") +
  theme_classic() +
  theme_custom() +
  theme(legend.position = "none")

ggsave("points_t.png")

df2 %>% 
  filter(LGE == "NHL") %>% 
  filter(!grepl("Pav|Kess|Perr|Berg|Tav|Spez", Name)) %>% 
  group_by(Name) %>% 
  mutate(Season = row_number(),
         cumG = cumsum(G),
         cumP = cumsum(P)) %>% 
  filter(Season < 7) %>% 
  mutate(label = if_else(Season == max(Season), Name, NA_character_)) %>%
  ggplot(aes(Season, cumP, color = Name)) +
  geom_line() +
  geom_point() +
  geom_dl(aes(label = label), 
          method = list(dl.combine("last.points"), cex = .8, hjust = -.1)) +
  expand_limits(x = c(NA, 7)) +
  labs(y = "Total Points",
       title = "NHL Top 10 Goal Leaders, Active Players - First Six Seasons",
       subtitle = "Visualizes the cumulative trend for each player from their sixth.") +
  theme_classic() +
  theme_custom() +
  theme(legend.position = "none")

ggsave("points_f6.png")
Edit
Pub: 16 May 2021 01:14 UTC
Views: 596