Explore the evolution of Valentine's Day trends with two insightful plots. The first reveals the percentage of celebrants and adjusted spending per person, while the second breaks down expenditures across categories like candy and flowers. Dive into the nuances of changing preferences over the years in this captivating visual journey through Valentine's Day spending patterns.

The Code

# load libraries
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.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── 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(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(grid)                # Provides grid graphics functionality.
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
# load data
historical_spending <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-02-13/historical_spending.csv')
## Rows: 13 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (10): Year, PercentCelebrating, PerPerson, Candy, Flowers, Jewelry, Gree...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
gifts_age <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-02-13/gifts_age.csv')
## Rows: 6 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Age
## dbl (8): SpendingCelebrating, Candy, Flowers, Jewelry, GreetingCards, Evenin...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
gifts_gender <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-02-13/gifts_gender.csv')
## Rows: 2 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Gender
## dbl (8): SpendingCelebrating, Candy, Flowers, Jewelry, GreetingCards, Evenin...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# load fonts
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()

# define colors
bg <- "white"
col1 <- "#d60000"
col2 <- "#79133B"
col3 <- "#B6284E"
col4 <- "#E54A46"
col5 <- "#F39E44"
col6 <- "#F8C765"
col7 <- "#4F2477"

# text creation
twitter <- glue("<span style='color:{col1};font-family:fa-brands;'>&#xf099;</span>")
mastodon <- glue("<span style='color:{col1};font-family:fa-brands;'>&#xf4f6;</span>")
link <- glue("<span style='color:{col1};font-family:fa-solid;'>&#xf0c1;</span>")
data <- glue("<span style='color:{col1};font-family:fa-solid;'>&#xf1c0;</span>")
hash <- glue("<span style='color:{col1};font-family:fa-solid;'>&#x23;</span>")
space <- glue("<span style='color:{bg}'>-</span>")
space2 <- glue("<span style='color:{bg}'>--</span>") # can't believe I'm doing this
candy <- glue("<span style='color:{col1};font-family:fa-solid;'>&#xf786;</span>")
clothing <- glue("<span style='color:{col2};font-family:fa-solid;'>&#xf553;</span>")
eveout <- glue("<span style='color:{col3};font-family:fa-solid;'>&#xf561;</span>")
flower <- glue("<span style='color:{col4};font-family:fa-solid;'>&#xf4d8;</span>")
giftcard <- glue("<span style='color:{col5};font-family:fa-solid;'>&#xf145;</span>")
greetingcard <- glue("<span style='color:{col6};font-family:fa-solid;'>&#xf0e0;</span>")
jewelry <- glue("<span style='color:{col7};font-family:fa-solid;'>&#xf3a5;</span>")

col <- c(col1,col2,col3,col4,col5,col6,col7)
l <- c(candy,clothing,eveout,flower,giftcard,greetingcard,jewelry)


t.sub1 <- glue("<strong>Valentine`s Day Trends: <span style='color:{col1};'>
               Celebrating Percentage</span> and <span style='color:{col2};'>
               Adjusted Spending</span> Over the Years</strong>")
t.sub2 <- glue("<strong>Valentine`s Day Spending Breakdown Across Categories Over the Years
               </strong><br>Spending Categories: Candy({candy}), Clothing ({clothing}), 
               Event Out ({eveout}), Flower ({flower}), Gift Card ({giftcard}), Greeting Card 
               ({greetingcard}) and Jewelry ({jewelry})")
t <- "<strong>Visualizing Valentine`s Day Spending Trends</strong>"

# Define Caption
# A formatted caption is defined, including social media icons and links.
cap <- glue("{twitter}{space2}@web_design_fh{space2} 
	{space2}{mastodon}{space2}@frankhaenel @fosstodon.org{space2}
	{space2}{link}{space}{space2}www.frankhaenel.de{space2}
	{space2}{data}{space2}National Retail Federation")

# create plot
# subplot 1

coeff <- 3 # Value used to transform the data
sub.1 <- ggplot(data=historical_spending) +
  geom_line(aes(y=PercentCelebrating, x=Year),linewidth=1.3,color=col1) +
  geom_line(aes(y=PerPerson/coeff, x=Year),linewidth=1.3,color=col2) +
  scale_y_continuous(
    name = "Celebrating [%]<br>",
    sec.axis = sec_axis(~.*coeff,name="Spending [$]<br>")
  ) + scale_x_continuous(name = "Year", breaks = c(2010,2012,2014,2016,2018,2020,2022)) +
  theme_bw() +
  theme(
    plot.subtitle = element_markdown(size=16),
    axis.title.y = element_markdown(color=col1,size=13),
    axis.title.y.right = element_markdown(color=col2,size=13)
  ) +
  labs(subtitle = t.sub1)


df <- historical_spending %>%
  select(-PercentCelebrating,-PerPerson) %>%
  gather(key = "spending_category", value = "spending", -Year)

sub.2 <- ggplot(df,aes(x=spending_category,y=spending,fill=spending_category)) +
  geom_col(show.legend = FALSE) +
  scale_fill_manual(values = col) +
  scale_x_discrete("Spending Category",labels = l) +
  theme_bw() +
  theme(axis.text.x = element_markdown(size = 14),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        plot.subtitle = element_markdown(size = 16)) +
  facet_wrap(.~Year, ncol = 5) +
  labs(subtitle = t.sub2)

# arrange subplots
p <- sub.1 / sub.2
p + plot_annotation(
    title = t,
    caption = cap) &
  theme(plot.title = element_markdown(size = 20, hjust = 0.5, lineheight = 1.3),
        plot.caption = element_markdown(size = 10, hjust = 0, lineheight = 1.3))
# add logo :)
logo <- image_read("image.png")
grid.raster(logo, x = 0.98, y = 0, just = c('right', 'bottom'), width = unit(1.5, 'inches'))
Visual representation of Valentine s Day trends with two subplots.

R Code Documentation

Overview

This R code generates a comprehensive visualization of Valentine's Day spending trends using `historical_spending`-dataset.

Libraries Used

  • tidyverse (Wickham et al. 2019): A collection of packages for data manipulation and visualization.
  • showtext (Qiu 2023): Used for incorporating custom fonts into the plot.
  • glue (Hester and Bryan 2022): Enables the creation of HTML-like text strings.
  • ggtext (Wilke and Wiernik 2022): Allows the use of rich text formatting in ggplot2 plots.
  • magick (Ooms 2023): Utilized for image processing and circular cropping.
  • patchwork (Pedersen 2023): Facilitates the arrangement of multiple plots.
  • png (Urbanek 2022): Required to handle PNG images.
  • grid (Murrell 2005): Provides grid graphics functionality.

Symbols and Styling

  • Custom symbols from Font Awesome are used for Twitter, Mastodon, Link, and Data icons.
  • Glue is employed for creating stylized text strings and formatting elements.

Subplots

  1. Valentine`s Day Trends: Celebrating Percentage and Adjusted Spending Over the Years
  2. Valentine`s Day Spending Breakdown Across Categories Over the Years

Fonts and Styling

  • Font Awesome fonts (fa-reg, fa-brands, fa-solid) are added for custom styling.
  • showtext is used to automatically load and utilize these custom fonts.
  • Markdown-like styling is applied to titles, subtitles, and captions for enhanced visual appeal.

Execution Steps

  1. Load necessary libraries.
  2. Load TidyTuesday data for week 7, 2024.
  3. Define symbols and styling elements.
  4. Create ttwo subplots, each focusing on different aspects of Customer Data.
  5. Combine subplots into a final visualization.
  6. Apply formatting and styling to the overall plot.
  7. Display the plot with a title, subtitle, and caption.

References