Tracking the emotional tone throughout each season (Seasons 1-14) Based on dialogue sentiment analysis | Values above 0 indicate positive emotional tone
Bob’s Burgers
{ bobsburgersR }
#Standalone
Author
Steven Ponce
Published
November 13, 2024
Steps to Create this Graphic
1. Load Packages & Setup
Show code
## 1. LOAD PACKAGES & SETUP ----if (!require("pacman")) install.packages("pacman")pacman::p_load( tidyverse, # Easily Install and Load the 'Tidyverse' ggtext, # Improved Text Rendering Support for 'ggplot2' showtext, # Using Fonts More Easily in R Graphs janitor, # Simple Tools for Examining and Cleaning Dirty Data skimr, # Compact and Flexible Summaries of Data scales, # Scale Functions for Visualization glue, # Interpreted String Literals bobsburgersR, # Bob's Burgers Datasets for Data Visualization tidytext, # Text Mining using 'dplyr', 'ggplot2', and Other Tidy Tools patchwork, # The Composer of Plots zoo # S3 Infrastructure for Regular and Irregular Time Series ) ### |- figure size ----camcorder::gg_record(dir = here::here("temp_plots"),device ="png",width =9,height =10,units ="in",dpi =320)### |- resolution ----showtext_opts(dpi =320, regular.wt =300, bold.wt =800)
2. Read in the Data
Show code
bobsburgersR::transcript_data
3. Examine the Data
Show code
glimpse(transcript_data)skim(transcript_data)
4. Tidy Data
Show code
# Sentiment analysis - AFINN Sentiment Lexiconsentiment_data <- transcript_data |>filter(!is.na(dialogue)) |>unnest_tokens(word, dialogue) |>inner_join(get_sentiments("afinn")) |>group_by(season, episode) |>mutate(position = line /max(line),rolling_sentiment = zoo::rollmean(value, k =30, fill =NA) ) |>ungroup()
5. Visualization Parameters
Show code
### |- plot aesthetics ----bkg_col <-"#f5f5f2"title_col <-"gray20"subtitle_col <-"gray30"text_col <-"gray30"caption_col <-"gray40"main_color <-"#2b8cbe"### |- titles and caption ----# iconstt <-str_glue("Source: {{bobsburgersR}}")li <-str_glue("<span style='font-family:fa6-brands'></span>")gh <-str_glue("<span style='font-family:fa6-brands'></span>")bs <-str_glue("<span style='font-family:fa6-brands'> </span>")# texttitle_text <-str_glue("Emotional Flow in Bob's Burgers")subtitle_text <-str_glue("Tracking the emotional tone throughout each season (Seasons 1-14)<br> Based on dialogue sentiment analysis | Values above 0 indicate positive emotional tone")caption_text <-str_glue("{li} stevenponce • {gh} poncest • #rstats #ggplot2 • {tt}")### |- fonts ----font_add("fa6-brands", here::here("fonts/6.4.2/Font Awesome 6 Brands-Regular-400.otf"))font_add_google("Oswald", regular.wt =400, family ="title")font_add_google("Source Sans Pro", family ="subtitle")font_add_google("Source Sans Pro", family ="text") font_add_google("Roboto Mono", family ="numbers") font_add_google("Noto Sans", regular.wt =400, family ="caption")showtext_auto(enable =TRUE)### |- plot theme ----theme_set(theme_minimal(base_size =14, base_family ="text")) theme_update(plot.title.position ="plot",plot.caption.position ="plot",legend.position ="plot",plot.background =element_rect(fill = bkg_col, color = bkg_col),panel.background =element_rect(fill = bkg_col, color = bkg_col),plot.margin =margin(t =10, r =10, b =10, l =10),axis.title.x =element_text(margin =margin(10, 0, 0, 0), size =rel(1.1), color = text_col, family ="text", face ="bold", hjust =0.5),axis.title.y =element_text(margin =margin(0, 10, 0, 0), size =rel(1.1), color = text_col, family ="text", face ="bold", hjust =0.5),axis.text =element_text(size =rel(0.6), color = text_col, family ="numbers"),strip.text =element_text(size =rel(1), face ="bold", margin =margin(b =10), family ="text"),panel.grid.major.y =element_line(color ="#d3d3d3", linewidth =0.2), panel.grid.major.x =element_blank(), panel.grid.minor =element_blank(),panel.spacing.x =unit(2, "lines"), panel.spacing.y =unit(1, "lines"), )
# There was some issues between patchwork and ggsave# Make sure these pkgs are installedif (!require("ggplotify")) install.packages("ggplotify")if (!require("grid")) install.packages("grid")# Use Arial (Windows system font)windowsFonts(Arial =windowsFont("Arial"))font_add("fa6-brands", here::here("fonts/6.4.2/Font Awesome 6 Brands-Regular-400.otf"))# Convert patchwork plot to grobplot_grob <-as.grob(combined_plot)# Set up the PNG device with proper font handlingpng(filename = here::here("projects/standalone_visualizations/sa_2024-11-13.png"),width =9, height =10, units ="in", res =320,type ="cairo")# Enable showtext with specific settingsshowtext::showtext_begin()showtext::showtext_opts(dpi =320, regular.wt =300, bold.wt =800)# Draw the plotgrid::grid.draw(plot_grob)# Clean upshowtext::showtext_end()invisible(dev.off())# Create thumbnailmagick::image_read(here::here("projects/standalone_visualizations/sa_2024-11-13.png")) |> magick::image_resize(geometry ="400") |> magick::image_write(here::here("projects/standalone_visualizations/thumbnails/sa_2024-11-13.png"))