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
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(grid) # Provides grid graphics functionality. library(magick) # Used to read and display image files.
# load data historical_spending <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-02-13/historical_spending.csv')
gifts_age <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-02-13/gifts_age.csv')
gifts_gender <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-02-13/gifts_gender.csv')
# 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;'></span>") mastodon <- glue("<span style='color:{col1};font-family:fa-brands;'></span>") link <- glue("<span style='color:{col1};font-family:fa-solid;'></span>") data <- glue("<span style='color:{col1};font-family:fa-solid;'></span>") hash <- glue("<span style='color:{col1};font-family:fa-solid;'>#</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;'></span>") clothing <- glue("<span style='color:{col2};font-family:fa-solid;'></span>") eveout <- glue("<span style='color:{col3};font-family:fa-solid;'></span>") flower <- glue("<span style='color:{col4};font-family:fa-solid;'></span>") giftcard <- glue("<span style='color:{col5};font-family:fa-solid;'></span>") greetingcard <- glue("<span style='color:{col6};font-family:fa-solid;'></span>") jewelry <- glue("<span style='color:{col7};font-family:fa-solid;'></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'))
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
- Valentine`s Day Trends: Celebrating Percentage and Adjusted Spending Over the Years
- 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
- Load necessary libraries.
- Load TidyTuesday data for week 7, 2024.
- Define symbols and styling elements.
- Create ttwo subplots, each focusing on different aspects of Customer Data.
- Combine subplots into a final visualization.
- Apply formatting and styling to the overall plot.
- Display the plot with a title, subtitle, and caption.
References
- Hester, Jim, and Jennifer Bryan. 2022. Glue: Interpreted String Literals. https://CRAN.R-project.org/package=glue.
- Ooms, Jeroen. 2023. Magick: Advanced Graphics and Image-Processing in r. https://CRAN.R-project.org/package=magick.
- Pedersen, Thomas Lin. 2023. Patchwork: The Composer of Plots.
- Qiu, Yixuan. 2023. Showtext: Using Fonts More Easily in r Graphs. https://CRAN.R-project.org/package=showtext.
- Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
- Murrell, P. 2022. R Graphics. Chapman & Hall/CRC Press..
- Wilke, Claus O., and Brenton M. Wiernik. 2022. Ggtext: Improved Text Rendering Support for ’Ggplot2’. https://CRAN.R-project.org/package=ggtext.
- Urbanek Simon. 2022. png: Read and write PNG images. https://CRAN.R-project.org/package=png.