Skip to content

Instantly share code, notes, and snippets.

@vincentarelbundock
Created March 4, 2020 14:22
Show Gist options
  • Save vincentarelbundock/9bd2d13680d4edd82cd45c60ef1e5373 to your computer and use it in GitHub Desktop.
Save vincentarelbundock/9bd2d13680d4edd82cd45c60ef1e5373 to your computer and use it in GitHub Desktop.
library(gghighlight)
library(tidyverse)
# load data
raw <- read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-03/game_goals.csv')
# 8 best scorers (there are 8 colors in scale_color_brewer Dark2)
best <- raw %>%
group_by(player) %>%
summarize(goals = sum(goals)) %>%
arrange(-goals) %>%
pull(player) %>%
head(8)
# cumsum
dat <- raw %>%
filter(player %in% best) %>%
arrange(player, date) %>%
group_by(player) %>%
mutate(`Goals scored` = cumsum(goals),
`Games played` = 1:n())
# plot
p <- ggplot(dat, aes(`Games played`, `Goals scored`, color = player, group = player)) +
geom_line(alpha = 0) + # hack: gghighlight needs a line
geom_smooth(se = FALSE) +
xlab('Games played') +
ylab('Goals scored') +
gghighlight(use_direct_label = TRUE,
label_params = list(size = 2.5, fill = 'white',
box.padding = .1,
label.size = .01,
alpha = .9)) +
theme_minimal() +
scale_colour_brewer(palette = "Dark2") +
theme(panel.grid = element_blank())
ggsave(p, file = '~/Downloads/tidytuesday.png', width = 8, height = 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment