Skip to content

Instantly share code, notes, and snippets.

@jemus42
Created April 7, 2020 19:00
Show Gist options
  • Save jemus42/909123486b3ee33be3a1899107879cbc to your computer and use it in GitHub Desktop.
Save jemus42/909123486b3ee33be3a1899107879cbc to your computer and use it in GitHub Desktop.
Heatmapping episode ratings from trakt.tv
library(tRakt) # remotes::install_github("jemus42/tRakt")
library(dplyr)
library(ggplot2)
library(firasans) # remotes::install_github("hrbrmstr/firasans")
#' @param show_id The show's id on trakt.tv, can also be the show's slug.
#' @param asp_rect Wther to add ggplot2::coord_equal()
#' @examples
#' episode_heatmap("the-simpsons")
episode_heatmap <- function(show_id, asp_rect = TRUE) {
episode_data <- seasons_summary(show_id, episodes = TRUE, extended = "full") %>%
pull(episodes) %>%
bind_rows()
p <- episode_data %>%
filter(first_aired <= lubridate::now()) %>%
mutate(
rating = round(rating, 1),
label_col = ifelse(rating > 7.5, "dark", "bright")
) %>%
ggplot(aes(x = season, y = episode, fill = rating)) +
geom_tile() +
geom_text(
aes(label = rating, color = label_col),
size = 3,
) +
scale_x_continuous(
position = "top",
expand = c(0, 0),
breaks = seq(1, 40, 1)
) +
scale_y_reverse(
expand = c(0, 0),
breaks = 1:30
) +
scale_color_manual(values = c("dark" = "black", "bright" = "white"), guide = FALSE) +
scale_fill_viridis_c(breaks = seq(1, 10, .5), option = "C") +
guides(fill = guide_colorbar(barwidth = 10)) +
labs(
title = shows_summary(show_id)$title,
subtitle = "Episode Ratings on trakt.tv",
x = "Season", y = "Episode", fill = ""
) +
firasans::theme_ipsum_fsc(grid = FALSE) +
theme(
legend.position = "top"
)
if (asp_rect) p <- p + coord_equal()
p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment