This R code explores and visualizes data related to haunted places in the United States. It uses various libraries for data manipulation, visualization, and text formatting to create an interactive US map plot. The plot showcases the distribution of haunted places across different states with a Halloween-themed design.
The Code
# Load necessary libraries library(tidyverse) # For data manipulation and visualization
library(tidytuesdayR) # For accessing TidyTuesday datasets library(showtext) # For working with fonts
library(glue) # For text formatting library(ggtext) # For enhanced text formatting in ggplot2 library(usmap) # For US map plots # Load Data # Load TidyTuesday dataset for the specified year and week tuesdata <- tidytuesdayR::tt_load(2023, week = 41)
## ## Downloading file 1 of 1: `haunted_places.csv`
haunted_places <- tuesdata$haunted_places # Load Fonts and Define Colors # Load custom fonts and define color codes for use 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') font_add('halloween', 'c:/Users/info/OneDrive/Dokumente/fonts/halloween_a.ttf') font_add('spooky', 'c:/Users/info/OneDrive/Dokumente/fonts/Spooky Stories.ttf') showtext_auto() bg <- "black" col1 <- "#ec7d11" col2 <- "#982608" col3 <- "yellow" # Define Symbols # Define symbols using HTML-style code with specified colors and fonts 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>") quote <- glue("<span style='color:{col1};font-family:fa-solid;'></span>") space <- glue("<span style='color:{bg}'>-</span>") space2 <- glue("<span style='color:{bg}'>--</span>") # This creates horizontal lines for formatting. # Define Title t <- "HAUNTED PLACES ACROSS U.S. STATES" s <- "Exploring the Spooky Landscape<br>of America's Paranormal Phenomena" 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}Tim Renner on data.world") # Count haunted places by state df <- haunted_places %>% count(state) # Create and customize the US map plot plot_usmap(data = df, values = "n", color=bg) + scale_fill_continuous( low = col3, high = col1, name = "Number of<br>haunted places", label = scales::comma) + theme( legend.position = "right", legend.title = element_markdown(color = col1), legend.text = element_text(color = col1), legend.background = element_rect(fill = bg), plot.background = element_rect(fill = bg), panel.background = element_rect(color = bg, fill = bg), plot.title = element_text(size = 26, hjust = 0.5, lineheight = 1.3, family = "halloween", color = col1), plot.subtitle = element_markdown(size = 22, hjust = 0.5, lineheight = 1.3, family = "spooky", color = col1), plot.caption = element_markdown(size = 12, hjust = 0.5, lineheight = 1.3, family = "poppins", color = col2) ) + labs(title = t, subtitle = s, caption = cap)
R Code Documentation
This R code is designed to load and visualize data related to haunted places in the United States using various libraries for data manipulation, visualization, and text formatting.
Libraries Used
tidyverse
: Used for data manipulation and visualization.tidytuesdayR
: Enables access to TidyTuesday datasets.showtext
: Used for working with custom fonts.glue
: Facilitates text formatting.ggtext
: Provides enhanced text formatting for ggplot2.usmap
: Allows for the creation of US map plots.
Data Loading
The code loads the TidyTuesday dataset for the specified year and week, focusing on haunted places data in the United States.
Fonts and Colors
Custom fonts are loaded for text elements, and color codes are defined for use in visualizations. The background color (bg
), primary text color (col1
), secondary text color (col2
), and fill color (col3
) are set.
Symbols and Text Styling
Various symbols are defined using HTML-style code with specified colors and fonts. These symbols are used for enhancing text formatting in the visualizations. Additionally, formatting for spaces and horizontal lines is defined.
Title and Captions
The title (t
), subtitle (s
), and caption (cap
) for the visualizations are defined. These texts incorporate custom fonts and symbols for a spooky and Halloween-themed appearance.
Data Processing and Visualization
The code counts the number of haunted places by state and then creates a customized US map plot to visualize the data. Various theme elements, such as legend styling, background colors, and text sizes, are adjusted to achieve the desired visual effect.