Skip to content

Instantly share code, notes, and snippets.

@jnolis
Created January 25, 2021 15:56
Show Gist options
  • Save jnolis/2b1b1360944e7febffdfdf201ccbb2d6 to your computer and use it in GitHub Desktop.
Save jnolis/2b1b1360944e7febffdfdf201ccbb2d6 to your computer and use it in GitHub Desktop.
Creating a Star Trek: Deep Space Nine Netflix analytics plot in R
# Creating a Star Trek: Deep Space Nine Netflix analytics plot in R
#
# FONT SETUP INSTRUCTIONS
# Before running this you'll need to go to this website,
# download the Context Ultra Condensed font, then install it to your system.
# https://www.st-minutiae.com/resources/fonts/index.html
#
# You'll need to register the font by running extrafont::font_import().
# I found that I had to manually import the ttf file--it wasn't picked up by reading the default
# directory. Instead I did running
# extrafont::font_import("path to R project folder where I had a copy of the ttf file")
#
# NETFLIX DATA INSTRUCTIONS
# Go to https://www.netflix.com/account/getmyinfo and follow the instructions to download the
# data. It may day hours before the data is ready to download. Once you have done so, extract
# the file "PlaybackRelatedEvents.csv" and put it in the same folder as this script
#
# DEEP SPACE NINE COLORS AND OUTER IMAGE
# Using the image from this wiki page, I was able to get the outer border I added in a photo
# editing program, and the colors to use in the chart.
# https://en.wikipedia.org/wiki/LCARS#/media/File:Lcars_wallpaper.svg
library(tidyverse)
library(ggchicklet) # this is used to make the rounded columns
library(extrafont) # this is used to get the special font loaded
# this probably doesn't need the "win" if you aren't in windows
loadfonts(device = "win")
# I got these colors from the source image
console_colors <- c("#ff9966","#cc99cc", "#9999ff", "#ff9900", "#f0f3d4", "#cc6666", "#66b9cc")
# this reads all of your Netflix viewing data in then converts it to camel case
# play-traces actually tells you the play/pause events that happened during the viewing.
# I ignored that column by you could convert it to json and it would be interesting, I suspect.
raw_data <- read_csv("PlaybackRelatedEvents.csv", col_types = cols(
`Profile Name` = col_character(),
`Title Description` = col_character(),
Device = col_character(),
Country = col_character(),
`Playback Start Utc Ts` = col_datetime(format = ""),
Playtraces = col_character()
)) %>%
janitor::clean_names()
# This table filters down to just the DS9 episodes and extracts the season and episode name
# Since in theory you could watch an episode multiple times, it also filters to just the first
# viewing of an episode.
ds9_data <-
raw_data %>%
filter(str_detect(title_description,"Star Trek: Deep Space Nine")) %>%
mutate(season =
str_match(title_description,
"Star Trek: Deep Space Nine: Season ([0-9])")[,2] %>%
factor(levels=1:7),
episode =
str_match(title_description,
"Star Trek: Deep Space Nine: Season [0-9]: \"([[:graph:] ]*)\"")[,2]) %>%
select(title_description,
season,
episode,
datetime = playback_start_utc_ts) %>%
group_by(title_description) %>%
arrange(datetime) %>%
slice(1) %>% #get just the first viewing
ungroup()
# this is the data frame for the plot, which has a row for each season.
# The "fake_title" will be used for a facet to get the nice background rectangle,
# compared to the standard labs(title = ...)
#
# The season has a leading zero to look more science-y
plot_data <- ds9_data %>%
group_by(season) %>%
summarize(start_season = min(datetime),
end_season = max(datetime),
episodes = n(),
length = as.numeric(difftime(end_season, start_season, units = "days") + 1)) %>%
mutate(episodes_per_day = episodes/length,
fake_title = "VIEWING CONSUMPTION OF\nSTAR TREK: DEEP SPACE NINE",
season = fct_relabel(season, ~ str_pad(.x, 2, "0", side = "left")))
ggplot(plot_data,
aes(x = season, y = episodes_per_day, fill = season)) +
geom_chicklet(show.legend = FALSE, color = NA,radius = grid::unit(4, 'mm'), width = 0.8) +
labs(y = "EPISODES WATCHED PER DAY",
x = "SEASON OF DEEP SPACE NINE") +
facet_wrap(~ fake_title) +
theme_minimal() +
scale_color_manual(values = console_colors) +
scale_fill_manual(values = console_colors) +
theme(
strip.background = element_rect(fill = console_colors[4]),
strip.text = element_text(hjust = 0.1, size = 24),
text = element_text(family = "Context Ultra Condensed SSi", size = 20),
axis.text.x = element_text(color = console_colors[5]),
axis.title.x = element_text(color = console_colors[5]),
axis.text.y = element_text(color = console_colors[3]),
axis.title.y = element_text(color = console_colors[1]),
panel.grid.major.x = element_blank(),
plot.background = element_rect(fill = "#000000"))
ggsave("output.png", type = "cairo-png", width = 6, height = 4.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment