knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE, warning = FALSE, error = FALSE,
  out.width = "100%"
)
library(tRakt)
library(tibble) # For glimpse()

Step 1: Search

Use the search function to find the show you're looking for.

show_info <- search_query("24", type = "show")
show_info

Step 2: Use the ID

Use the trakt ID (recommended over other IDs) for subsequent API calls.
In this case, we'll use seasons_summary() to get data for all seasons of the show, while also getting an additional list-column containing all episode data, which includes user ratings.

seasons <- seasons_summary(show_info$trakt, extended = "full", episodes = TRUE)
glimpse(seasons)

Step 3: Tidying up

We're interested in the episodes list-column, which needs unnesting. In this case we can simply use dplyr::bind_rows to take the list of tbls that is seasons$episodes and basically rbind them all together, meaning the result is a tibble of the episode data we care about.

library(dplyr)

episodes <- bind_rows(seasons$episodes)
glimpse(episodes)

Step 4: Graph!

Now we have our episode data in a tidy form, might as well look at it.

library(ggplot2)

ggplot(data = episodes, aes(x = episode, y = rating, color = votes)) +
  geom_point(size = 3, alpha = 2 / 3) +
  facet_wrap(~season, nrow = 1, scales = "free_x") +
  scale_x_continuous(breaks = c(1, 10, 20), expand = c(0, 3)) +
  scale_y_continuous(breaks = seq(0, 10, .5), minor_breaks = seq(0, 10, .25), limits = c(7, 9)) +
  scale_color_viridis_c() +
  guides(color = guide_colorbar(barwidth = unit(6, "cm"), title.vjust = .75)) +
  labs(
    title = "24: Episode Ratings on trakt.tv",
    subtitle = "Episode ratings by trakt.tv users by season",
    x = "Episode Number", y = "Rating (1-10)", color = "# of Votes",
    caption = "jemus42.github.io/tRakt"
  ) +
  theme_minimal() +
  theme(
    legend.position = "bottom"
  )


jemus42/tRakt documentation built on May 4, 2024, 9:21 a.m.