For this week's TidyTuesday, I took inspiration from a tweet by Albert Rapp (Rapp, 2023b) and visualized the distribution of F-Counts across the various Ted Lasso seasons using a Raincloud plot. Additionally, I created a Treemap of the F-Scores.
I used the ggdist package (Kay, 2023) to create the Raincloud plot, and for the Treemap, I utilized the treemapify package (Wilkins, 2021).
The Code
library(tidyverse) # For data manipulation and visualization
library(tidytuesdayR) # For accessing TidyTuesday datasets library(showtext) # For working with fonts
library(glue) # For text formatting library(ggtext) # For enhanced text formatting in ggplot2 library(patchwork) # Used to arrange multiple plots. library(png) # Required to handle PNG images. library(magick) # Used to read and display image files.
library(grid) # Provides grid graphics functionality. library(ggdist) # For statistical raincloud. library(treemapify) # For statistical treemap. # Load Data # Data is loaded from the 'tidytuesdayR' package for a specific week (2023, week 38). tuesdata <- tidytuesdayR::tt_load(2023, week = 39)
## ## Downloading file 1 of 1: `richmondway.csv`
richmondway <- tuesdata$richmondway # Load Fonts and Define Colors # Fonts are loaded and colors are defined for text and symbols in visualizations. font_add_google("Poppins", "poppins") font_add('fa-reg', 'c:/Users/info/OneDrive/Dokumente/fonts/Font Awesome 6 Free-Regular-400.otf') font_add('fa-brands', 'c:/Users/info/OneDrive/Dokumente/fonts/Font Awesome 6 Brands-Regular-400.otf') font_add('fa-solid', 'c:/Users/info/OneDrive/Dokumente/fonts/Font Awesome 6 Free-Solid-900.otf') showtext_auto() bg <- "white" # background color col1 <- "#021E73" # for text col2 <- "darkgrey" # for text col3 <- "#0176F2" # season 1 col4 <- "#FFD45A" # season 2 col5 <- "#EA0406" # season 3 # Define Symbols # Symbols are defined using HTML-style code with appropriate colors and fonts. twitter <- glue("<span style='color:{col2};font-family:fa-brands;'></span>") mastodon <- glue("<span style='color:{col2};font-family:fa-brands;'></span>") link <- glue("<span style='color:{col2};font-family:fa-solid;'></span>") data <- glue("<span style='color:{col2};font-family:fa-solid;'></span>") quote <- glue("<span style='color:{col2};font-family:fa-solid;'></span>") space <- glue("<span style='color:{bg}'>-</span>") space2 <- glue("<span style='color:{bg}'>--</span>") # This creates horizontal lines for formatting. space3 <- glue("<span style='color:{bg};font-family:fa-solid;'></span>") season1 <- glue("<span style='color:{col3};'>Season 1</span>") season2 <- glue("<span style='color:{col4};'>Season 2</span>") season3 <- glue("<span style='color:{col5};'>Season 3</span>") # Define Title # A formatted title for the analysis is defined using glue. subplot1.title <- glue("<b style='size: 12'>Exploring Roy Kent's F-Count by Season: A Rain Cloud Perspective</b>") subplot2.title <- glue("<b style='size: 12'>Treamap of Roy Kent's F-Score by Season</b>") subplot1.x <- "Season" subplot1.y <- "Roy Kent's F-Count" t <- glue("<b style='size: 20'>Roy Kent's F-Bomb Journey Through Ted Lasso Seasons</b>") s <- glue("<b style='size: 16'>{season1} | {season2} | {season3}</b>") cap <- glue("{twitter}{space2}@web_design_fh{space2} {space2}{mastodon}{space2}@frankhaenel @fosstodon.org{space2} {space2}{link}{space}{space2}www.frankhaenel.de<br> {data}{space2}Menghani{space}D{space}(2023).{space}<i>richmondway:{space}A{space}dataset{space}containing{space}the{space}number{space}of{space}timesvthe{space}word{space}f*ck{space}{space}was{space}used{space}in<br> {space3}{space2}Ted{space}Lasso{space}by{space}Roy{space}Kent</i>.{space}R{space}package{space}version{space}0.0.0.9000") # create plot #create rain cloud plot subplot1 <- richmondway %>% ggplot(aes(x = factor(Season), y = F_count_RK, fill = factor(Season))) + # add half-violin from {ggdist} package stat_halfeye( # adjust bandwidth adjust = 0.5, # move to the right justification = -0.2, # remove the slub interval .width = 0, point_colour = NA ) + geom_boxplot( width = 0.12, # removing outliers outlier.color = NA, alpha = 0.5 ) + stat_dots( # ploting on left side side = "left", # adjusting position justification = 1.1, # adjust grouping (binning) of observations binwidth = 0.25 ) + scale_fill_manual(values = c("1"= col3, "2"=col4, "3"=col5)) + labs(title=subplot1.title,x=subplot1.x,y=subplot1.y) + coord_flip() + theme_minimal() + theme( plot.title = element_markdown(size = 15, hjust = 0.5, lineheight = 1.3, family = "poppins", color = col1), axis.title.x = element_markdown(size = 10, color = col1, family = "poppins"), axis.text.x = element_markdown(size = 10, color = col1, family = "poppins"), axis.text.y = element_markdown(size = 10, color = col1, family = "poppins"), axis.title.y = element_markdown(size = 10, color = col1, family = "poppins"), ) + guides(fill = "none") # create treemap subplot2 <- richmondway %>% mutate(label = paste("E", Episode)) %>% ggplot(aes(area = F_score, fill = as.factor(Season), label = label, subgroup = Season)) + geom_treemap() + geom_treemap_subgroup_border(colour = "white", size = 5) + scale_fill_manual(values = c("1"= col3, "2"=col4, "3"=col5)) + guides(fill = "none") + geom_treemap_text(colour = col1, place = "centre", size = 15, grow = TRUE) + labs(title=subplot2.title) + theme( text = element_text(family = "poppins", color = col1), plot.title = element_markdown(size = 15, hjust = 0.5, lineheight = 1.3, family = "poppins", color = col1), axis.title.x = element_markdown(size = 10, color = col1, family = "poppins"), axis.text.x = element_markdown(size = 10, color = col1, family = "poppins"), axis.text.y = element_markdown(size = 10, color = col1, family = "poppins"), axis.title.y = element_markdown(size = 10, color = col1, family = "poppins"), ) subplot3 <- subplot2 + plot_spacer() + plot_layout(widths = c(3, 1)) plot <- subplot1 / subplot3 + plot_layout(heights = c(2, 1)) plot + plot_annotation( title = t, subtitle = s, caption = cap ) & theme( plot.title = element_markdown(size = 15, hjust = 0.5, lineheight = 1.3, family = "poppins", color = col1), plot.subtitle = element_markdown(size = 12, hjust = 0.5, lineheight = 1.3, family = "poppins", color = col1), plot.caption = element_markdown(size = 10, hjust = 0, lineheight = 1.3, family = "poppins", color = col1)) logo <- image_read("roykent.png") grid.raster(logo, x = 0.98, y = 0.1, just = c('right', 'bottom'), width = unit(2, 'inches'))
Code Documentation
Purpose
This R script generates a complex data visualization that explores Roy Kent's F-Count and F-Score by season, based on data from the "Ted Lasso" TV series. The visualization includes a "Rain Cloud" plot for F-Count and a Treemap for F-Score.
Libraries Used
- tidyverse: Used for data manipulation and visualization.
- tidytuesdayR: Used to access TidyTuesday datasets.
- showtext: Enables working with fonts.
- glue: Used for text formatting.
- ggtext: Provides enhanced text formatting in ggplot2.
- patchwork: Used to arrange multiple plots.
- png: Required to handle PNG images.
- magick: Used to read and display image files.
- grid: Provides grid graphics functionality.
- ggdist: Used for statistical graphics.
Data Loading
- Data is loaded from the "TidyTuesday" package for a specific week (2023, week 39).
- The dataset relevant to the analysis is extracted and assigned to the variable richmondway.
Font and Color Setup
- Fonts for text and symbols are loaded.
- Colors are defined for text, symbols, and visual elements.
- Symbols (such as Twitter and Mastodon icons) are defined in HTML-style code with the specified colors and fonts.
Title, Subtitle, and Caption
- A formatted title for the analysis, subtitle, and caption are defined using the glue package.
- The caption includes social media icons and links.
Rain Cloud Plot (subplot1)
- A rain cloud plot is created using the ggplot2 package.
- It displays Roy Kent's F-Count by season, providing insights into the distribution of F-Count across seasons.
- The plot includes half-violin, boxplot, and dot elements.
- Custom colors, labels, and themes are applied.
Treemap Plot (subplot2)
- A treemap plot is created using the ggplot2 package.
- It visualizes F-Scores by season, displaying episodes as subgroups.
- The plot uses custom colors, labels, and themes.
Combining Plots
- The two plots (rain cloud and treemap) are combined and arranged using patchwork.
- The resulting combined plot includes titles and captions.
Image
- An image ("roykent.png") is read and displayed at the bottom-right corner of the plot.
Visualization Output
- The final visualization is a composite of the rain cloud and treemap plots, along with titles, subtitles, and captions.
- The output provides an insightful exploration of Roy Kent's F-Count and F-Score by season.
References
- Kay, M. (2023). {ggdist}: Visualizations of Distributions and Uncertainty (R package version 3.3.0) [Software]. https://mjskay.github.io/ggdist/
- Menghani, D. (2023a). richmondway: A dataset containing the number of times the word f*ck was used in Ted Lasso by Roy Kent (R package version 0.0.0.9000) [Software]. https://github.com/deepshamenghani/richmondway
- Menghani, D. (2023b, September 20). Data Viz animation and interactivity in Quarto. Retrieved September 27, 2023, from https://deepshamenghani.github.io/posit_plotly_crosstalk/#/title-slide
- Rapp, A., [Albert Rapp]. (2023a, September 24). Create Raincloud Plots with ggplot2 | A Step by Step Guide [Video]. YouTube. Retrieved September 27, 2023, from https://www.youtube.com/watch?v=qeys6CQBzxo
- Rapp, A., [@rappa753]. (2023b, September 24). Raincloud plots are a great way to visualize uncertainty. 🌧️🌧️ Not only do they show you key summaries using a box plot, they also show you the distribution of your data. In this week’s video, I show you how to create such a plot with ggplot2. You can find the link below. 🥳. www.twitter.com. Retrieved September 27, 2023, from https://twitter.com/rappa753/status/1705945283501445436
- Wilkins, D. (2021). treemapify: Draw Treemaps in “ggplot2” (R package version 2.5.5) [Software]. https://CRAN.R-project.org/package=treemapify