Power Rangers Frachise Ratings

Number of votes polled for each episode of Power Rangers - visualzied as a tile chart

#TidyTuesday
Author

Aditya Dahiya

Published

August 31, 2024

Figure 1: Number of votes polled on IMDb for each Episode of Power Rangers

How I made this graphic?

Loading libraries & data

Code
# Data Import and Wrangling Tools
library(tidyverse)            # All things tidy

# Final plot tools
library(scales)               # Nice Scales for ggplot2
library(fontawesome)          # Icons display in ggplot2
library(ggtext)               # Markdown text support for ggplot2
library(showtext)             # Display fonts in ggplot2
library(colorspace)           # Lighten and Darken colours
library(seecolor)             # To print and view colours
library(patchwork)            # Combining plots

power_rangers_episodes <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-08-27/power_rangers_episodes.csv')
power_rangers_seasons <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-08-27/power_rangers_seasons.csv')

Data Wrangling

Code
df <- power_rangers_episodes |> 
  janitor::clean_names() |> 
  filter(episode_num > 0) |> 
  group_by(season_title) |> 
  mutate(
    min_date = min(air_date)
  ) |> 
  arrange(min_date)

season_levels <- df |> 
  pull(season_title) |> 
  unique()

df1 <- df |> 
  mutate(season_title = fct(season_title, levels = season_levels))

df2 <- df1 |> 
  group_by(season_title) |> 
  summarise(
    y_var = max(episode_num) + 1
  )

Visualization Parameters

Code
# Font for titles
font_add_google("Carter One",
  family = "title_font"
) 

# Font for the caption
font_add_google("M PLUS Code Latin",
  family = "caption_font"
) 

# Font for plot text
font_add_google("Lexend Deca",
  family = "body_font"
) 

showtext_auto()

bg_col <- "white"

text_col <- "#6F008DFF"
text_hil <- text_col

bts <- 80

# Caption stuff for the plot
sysfonts::font_add(
  family = "Font Awesome 6 Brands",
  regular = here::here("docs", "Font Awesome 6 Brands-Regular-400.otf")
)
github <- "&#xf09b"
github_username <- "aditya-dahiya"
xtwitter <- "&#xe61b"
xtwitter_username <- "@adityadahiyaias"
social_caption_1 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{github};</span> <span style='color: {text_hil}'>{github_username}  </span>")
social_caption_2 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{xtwitter};</span> <span style='color: {text_hil}'>{xtwitter_username}</span>")

plot_title <- "Power Rangers Frachise:\nFalling Populatiry over seasons"

plot_subtitle <- str_wrap("The number of votes polled for ratings of each episode fell after the first three seasons, and never really recovered.", 42)
plot_subtitle |> str_view()

plot_caption <- paste0(
  "**Data:** Kaggle's Power Rangers Dataset", 
  " |  **Code:** ", 
  social_caption_1, 
  " |  **Graphics:** ", 
  social_caption_2
  )

rm(github, github_username, xtwitter, 
   xtwitter_username, social_caption_1, 
   social_caption_2)

The static plot

Code
g <- df1 |> 
  ggplot(
    mapping = aes(
      x = season_title,
      y = episode_num
    )
  ) +
  geom_tile(
    mapping = aes(
      fill = total_votes
    ),
    width = 0.85,
    height = 0.8,
    colour = bg_col,
    linewidth = 0.1
  ) +
  geom_text(
    data = df2,
    mapping = aes(
      label = season_title,
      y = y_var
    ),
    angle = 90,
    hjust = 1,
    vjust = 0.3,
    family = "body_font",
    colour = text_col,
    size = bts / 4
  ) +
  geom_text(
    data = df1 |> distinct(season_title, min_date),
    mapping = aes(
      y = 0,
      label = format(min_date, "%b %y")
    ),
    vjust = 0.5,
    hjust = 0,
    angle = 90,
    family = "caption_font",
    colour = text_col,
    size = bts / 3.5
  ) +
  annotate(
    geom = "text",
    x = 19, y = 54,
    label = plot_subtitle,
    colour = text_col,
    lineheight = 0.35,
    size = bts / 2.5,
    family = "body_font",
    hjust = 0.5
  ) +
  paletteer::scale_fill_paletteer_c(
    "grDevices::Plasma", 
    direction = -1,
    breaks = seq(0, 600, 100)
  ) +
  scale_y_reverse(
    breaks = 1:60,
    labels = 1:60,
    expand = expansion(c(0.35, 0.05))
  ) +
  scale_x_discrete(
    position = "top",
    expand = expansion(0),
    labels = NULL
  ) +
  coord_fixed(
    ratio = 0.4,
    clip = "off"
  ) +
  labs(
    title = plot_title,
    # subtitle = plot_subtitle,
    caption = plot_caption,
    fill = "Total number of votes polled\non IMDb for each episode",
    x = "Season's starting month",
    y = "Episode Number"
  ) +
  theme_minimal(
    base_family = "body_font",
    base_size = bts
  ) +
  theme(
    text = element_text(
      colour = text_col,
      lineheight = 0.3
    ),
    # Legend
    legend.position = c(0.7, 0.2),
    legend.direction = "horizontal",
    legend.key.width = unit(30, "mm"),
    legend.title.position = "top",
    legend.title = element_text(
      margin = margin(0,0,5,0, "mm"),
      hjust = 0.5
    ),
    legend.text = element_text(
      margin = margin(2,0,0,0, "mm")
    ),
    
    # Other Stuff
    panel.grid = element_blank(),
    plot.margin = margin(15,5,15,5, "mm"),
    
    # Labels
    plot.title.position = "plot",
    plot.title = element_text(
      family = "title_font",
      margin = margin(0,0,0,0, "mm"),
      size = 2.7 * bts,
      hjust = 0.5
    ),
    plot.subtitle = element_text(
      margin = margin(0,0,0,0, "mm")
    ),
    plot.caption = element_textbox(
      margin = margin(-40,0,0,0, "mm"),
      hjust = 1,
      family = "caption_font",
      size = 0.5 * bts
    ),
    axis.title = element_text(
      margin = margin(0,0,0,0, "mm")
    ),
    axis.text = element_text(
      margin = margin(0,0,0,0, "mm"),
      colour = text_col,
      size = 0.5 * bts
    )
  )

ggsave(
  filename = here::here("data_vizs", "tidy_power_rangers.png"),
  plot = g,
  width = 400,    # Best Twitter Aspect Ratio = 5:4
  height = 500,   
  units = "mm",
  bg = "white"
)

Savings the graphics

Code
library(magick)
# Saving a thumbnail for the webpage
image_read(here::here("data_vizs", "tidy_power_rangers.png")) |> 
  image_resize(geometry = "400") |> 
  image_write(here::here("data_vizs", "thumbnails", "tidy_power_rangers.png"))