Stacked Bar charts, adding logo (images from {magick}) to ggplot2, using {ggpattern} and extracting a colour palette from an image.
#TidyTuesday
{imgpalr}
Colours
{ggimage}
{ggpattern}
Author
Aditya Dahiya
Published
December 31, 2024
About the Data
The James Beard Awards dataset provides an in-depth look at the annual awards recognizing excellence in culinary arts, journalism, and leadership within the United States. This dataset, curated by Jon Harmon and suggested by PythonCoderUnicorn, includes multiple facets of the awards spanning categories such as books, broadcast media, journalism, leadership, and restaurants and chefs. Each dataset captures variables such as award rank, year, subcategory, and affiliations, offering rich opportunities for analysis. Users can access these datasets via the tidytuesdayR package or directly from GitHub. Explore how award subcategories evolved over time, identify multi-category winners, and analyze prominent affiliations in culinary and media landscapes.
Figure 1: A bar chart showing the number of nominees and awardees under 4 different categories of the James Beard Awards. The {ggimage} has been used to add logos to the graphic, and the {imgpalr} has been used to generate a colour palette from the logo, to be used in the graphic.
How I made this graphic?
Loading required libraries, data import & creating custom functions. Downloading the logo.
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidy# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Compiling Plotslibrary(imgpalr) # Extracting palettes from images# Option 2: Read directly from GitHubbook <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-12-31/book.csv')broadcast_media <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-12-31/broadcast_media.csv')journalism <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-12-31/journalism.csv')leadership <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-12-31/leadership.csv')restaurant_and_chef <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-12-31/restaurant_and_chef.csv')# Get image logo of James Beard Awardsurl <-"https://imageio.forbes.com/blogs-images/johnmariani/files/2019/03/jb-medal-1200x1200.png"logo1 <- magick::image_read(url) |> magick::image_resize("x400")
# Font for titlesfont_add_google("Domine",family ="title_font") # Font for the captionfont_add_google("Stint Ultra Condensed",family ="caption_font") # Font for plot textfont_add_google("Oranienbaum",family ="body_font") showtext_auto()# Extract a colour palette from an image# set.seed(1)# magick::image_write(# logo1,# path = here::here("data", "temp_tidy_james_beard_awards.png")# )# mypal <- imgpalr::image_pal(# file = here::here("data", "temp_tidy_james_beard_awards.png"),# n = 7# )# unlink(here::here("data", "temp_tidy_james_beard_awards.png"))mypal <-c("#A68D6F", "#906A57", "#FCFCFE", "#521616", "#681A1C", "#7B473D","#A7976F")mypal |> seecolor::print_color()# A base Colourbg_col <- mypal[3]seecolor::print_color(bg_col)# Colour for highlighted texttext_hil <- mypal[4]seecolor::print_color(text_hil)# Colour for the texttext_col <- colorspace::darken(mypal[4], 0.3)seecolor::print_color(text_col)# Define Base Text Sizebts <-90# Caption stuff for the plotsysfonts::font_add(family ="Font Awesome 6 Brands",regular = here::here("docs", "Font Awesome 6 Brands-Regular-400.otf"))github <-""github_username <-"aditya-dahiya"xtwitter <-""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_caption <-paste0("**Data:** James Beard Foundation, Zane & Jon Harmon ", " | **Code:** ", social_caption_1, " | **Graphics:** ", social_caption_2 )rm(github, github_username, xtwitter, xtwitter_username, social_caption_1, social_caption_2)# Add text to plot-------------------------------------------------plot_title <-"Nominees' and Winners' numbers:\nJames Beard Awards"plot_subtitle <-"Over the last 3 decades, nominees and winners in different categories has varied. There were no awards in 2021."
Exploratory Data Analysis and Wrangling
Code
# Lets look at the years range for each data set# book$year |> range()# broadcast_media$year |> range()# journalism$year |> range()# restaurant_and_chef$year |> range()# leadership$year |> range()clean_up <-function(data1, text1){ data1 |>filter(year >1993) |>count(year, rank) |>mutate(class = text1)}df <-rbind(clean_up(book, "Books"),clean_up(broadcast_media, "Broadcast Media"),clean_up(journalism, "Journalism"),clean_up(restaurant_and_chef, "Restaurants and Chefs")) |>filter(rank %in%c("Nominee", "Winner")) |>mutate(rank =fct(rank, levels =c("Winner", "Nominee")),class =fct( class, levels =c("Broadcast Media","Books","Journalism","Restaurants and Chefs" ) ) )
# Saving a thumbnaillibrary(magick)# Saving a thumbnail for the webpageimage_read(here::here("data_vizs", "tidy_james_beard_awards.png")) |>image_resize(geometry ="x400") |>image_write( here::here("data_vizs", "thumbnails", "tidy_james_beard_awards.png" ) )
Session Info
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidy# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Compiling Plotssessioninfo::session_info()$packages |>as_tibble() |>select(package, version = loadedversion, date, source) |>arrange(package) |> janitor::clean_names(case ="title" ) |> gt::gt() |> gt::opt_interactive(use_search =TRUE ) |> gtExtras::gt_theme_espn()
Table 1: R Packages and their versions used in the creation of this page and graphics