Skip to content

Instantly share code, notes, and snippets.

@amzoss
Created December 16, 2021 19:13
Show Gist options
  • Save amzoss/c2f38ced5d88cba63abf66bce8fb4228 to your computer and use it in GitHub Desktop.
Save amzoss/c2f38ced5d88cba63abf66bce8fb4228 to your computer and use it in GitHub Desktop.
Dr Who code chunk
viewership_with_names <- series_viewership %>%
left_join(doctors) %>%
mutate(doctor_name = fct_reorder(doctor_name, season_number))
first_season <- viewership_with_names %>%
group_by(doctor_name) %>%
summarise(first_year = min(year)) %>%
mutate(y = ifelse(first_year == 2005, 2, 1))
ggplot(viewership_with_names,
aes(x = year, y = mean_uk_viewers, group = 1)
) +
geom_col(aes(x = year, y = 9, fill = doctor_name), width = 1.0, color = NA, alpha = .25, show.legend = FALSE) +
geom_text(data=first_season, aes(x = first_year, y = y, label = doctor_name, color = doctor_name), hjust=0, nudge_x = -.3, fontface="bold", show.legend = FALSE) +
geom_line(color = "gray50") +
geom_point(size = 2, color = "gray50") +
geom_text(aes(label = paste0(mean_uk_viewers, "m"), y = ifelse(mean_uk_viewers > 6, mean_uk_viewers+.4, mean_uk_viewers-.4)),
show.legend = FALSE, color = "gray50") +
scale_y_continuous(breaks = c(2,4,6,8), expand = c(0,0)) +
scale_x_discrete(expand = c(0,0,0,0)) +
scale_color_OkabeIto(darken = 0.2) +
scale_fill_OkabeIto(darken = 0.2) +
theme_minimal() +
labs(
x = NULL, y = "UK viewers (in millions)",
color = "Doctor", shape = "Doctor",
title = "**Doctor Who TV ratings:** series average",
caption = "Recreated plot from Independent.ie, not all values match."
) +
theme(
plot.title = element_markdown(),
axis.text.y = element_blank(),
legend.position = c(0.2, 0.35),
panel.grid.minor.y = element_blank(),
plot.margin = margin(5,20,5,5)
) +
coord_cartesian(clip = "off")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment