/
show-analysis-24.Rmd
83 lines (66 loc) · 2.32 KB
/
show-analysis-24.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
title: "Show Analysis: 24"
author: "Lukas Burk"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteEncoding{UTF-8}
%\VignetteIndexEntry{Show Analysis: 24}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
message = FALSE, warning = FALSE, error = FALSE,
out.width = "100%"
)
```
```{r startup}
library(tRakt)
library(tibble) # For glimpse()
```
## Step 1: Search
Use the search function to find the show you're looking for.
```{r 24_search}
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.
```{r 24_getting_seasons}
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.
```{r 24_episodes_unnesting}
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.
```{r 24_episodes_plot}
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"
)
```