library(tidyverse) # Data Wrangling and Plottinglibrary(here) # Files location and loadinglibrary(showtext) # Using Fonts More Easily in R Graphslibrary(ggimage) # Using Images in ggplot2library(fontawesome) # Social Media iconslibrary(ggtext) # Markdown Text in ggplot2library(patchwork) # For compiling plotslibrary(magick) # Work with Images and Logoslibrary(scales) # ggplot2 labels and scalinglibrary(sf) # Maps and converting coordinateslibrary(ggiraph) # Interactive visualizationlibrary(usmap) # Easily plot map of USA# Loading Datagroundhogs <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-01-30/groundhogs.csv')# Using predictions data from 2024 (latest) with the cleaning script given # at https://github.com/rfordatascience/tidytuesday/blob/master/data/2024/2024-01-30/readme.md# Credits: @tidytuesday and @jonthegeek predictions <-read_csv(here::here("data", "predictions2024.csv"))
Data Wrangling
Code
# Finding the groundhogs who predicted in year 2024relevant_groundhogs <- predictions |>filter(year ==2024) |>pull(id) |>unique()# Improving tibble to use in plottingdf1 <- groundhogs |>filter(id %in% relevant_groundhogs) |>left_join(predictions |>filter(year ==2024)) |>mutate(predict =if_else( shadow,"Groundhog saw its shadow: Extended Winters","No shadow: An Early Spring!"),predict =if_else(is.na(predict),"No prediction", predict ) )# Retaining only groundhogs within US borders + US Map transforming themdf2 <- df1 |>usmap_transform(input_names =c("longitude", "latitude") ) |>filter(country =="USA")image1 <-image_read("https://img.freepik.com/free-vector/adorable-groundhog-cartoon-with-groundhog-day-banner_1308-153480.jpg")
Some optional visualization parameters
Code
# Load fontsfont_add_google("Freckle Face", family ="title_font") # Font for titlesfont_add_google("Saira Extra Condensed", family ="caption_font") # Font for the captionfont_add_google("Barlow Condensed", family ="body_font") # Font for plot textshowtext_auto()# Icons to use in graph# Credits: Used code from# Creating a Colour Palette for the Visualizationmypal <-c("#0ab6f0", "grey", "#00990a")# Define coloursbg_col <-"white"# Background Colourtext_col <-"#4f2b00"# Colour for the texttext_hil <-"#c46c00"# Colour for highlighted text# Define Text Sizets =24# Text Size# Caption stuffsysfonts::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 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{github};</span> <span style='color: {text_col}'>{github_username} </span> <span style='font-family:\"Font Awesome 6 Brands\";'>{xtwitter};</span> <span style='color: {text_col}'>{xtwitter_username}</span>")# Add text to plot--------------------------------------------------------------plot_title <-"Groundhog Day: 2024"subtitle_text <-"Hover on a location to read about Groundhog's prediction."plot_subtitle <-paste(strwrap(subtitle_text, 100), collapse ="\n")plot_caption <-paste0("**Data & Inspiration:** groundhog-day.com | ", "**Graphics:** ", social_caption)
Below, in ?@fig-int1, I use ggiraph package with usmaps package to make an interactive map of the Groundhogs in USA, along with their details and predictions.