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
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidytuesdayR)        # For accessing TidyTuesday datasets
library(showtext)            # For working with fonts
## Lade nötiges Paket: sysfonts
## Lade nötiges Paket: showtextdb
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.
## Linking to ImageMagick 6.9.12.3
## Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fontconfig, x11
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)
## --- Compiling #TidyTuesday Information for 2023-09-26 ----
## --- There is 1 file available ---
## --- Starting Download ---
## 
## 	Downloading file 1 of 1: `richmondway.csv`
## --- Download complete ---
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;'>&#xf099;</span>")
mastodon <- glue("<span style='color:{col2};font-family:fa-brands;'>&#xf4f6;</span>")
link <- glue("<span style='color:{col2};font-family:fa-solid;'>&#xf0c1;</span>")
data <- glue("<span style='color:{col2};font-family:fa-solid;'>&#xf1c0;</span>")
quote <- glue("<span style='color:{col2};font-family:fa-solid;'>&#xf10d;</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;'>&#xf1c0;</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'))
An intricate data visualization created using R. The visualization consists of two main plots: a 'Rain Cloud' plot on the left and a 'Treemap' on the right. The Rain Cloud plot displays Roy Kent's F-Count by season, using colorful violin plots, boxplots, and dot overlays. The Treemap visualizes Roy Kent's F-Score by season, with episodes grouped by season. The entire visualization is framed by custom text and symbols, including social media icons and links. A image of Roy Kent is displayed in the bottom-right corner.

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