This R code generates an eye-catching chart that explores audio metrics in Taylor Swift's albums. It uses various libraries, custom fonts, and symbols to create an aesthetically pleasing visualization. The chart showcases metrics like danceability, energy, and more, providing valuable insights in a visually appealing format.
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(tayloRswift) # Additional package, for color palette # Load Data # Load TidyTuesday dataset for the specified year and week tuesdata <- tidytuesdayR::tt_load(2023, week = 42)
## ## Downloading file 1 of 3: `taylor_album_songs.csv` ## Downloading file 2 of 3: `taylor_all_songs.csv` ## Downloading file 3 of 3: `taylor_albums.csv`
taylor_album_songs <- tuesdata$taylor_album_songs taylor_all_songs <- tuesdata$taylor_all_songs taylor_albums <- tuesdata$taylor_albums # Load Fonts and Define Colors # Load custom fonts and define color codes for use in visualizations font_add_google("IM Fell DW Pica", "pica") 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('ts', 'c:/Users/info/OneDrive/Dokumente/fonts/Satisfaction.ttf') showtext_auto() bg <- "white" col1 <- "black" col2 <- "#b8396b" col3 <- "#ffd1d7" col4 <- "#fff5cc" col5 <- "#76bae0" col6 <- "#b28f81" col7 <- "#54483e" # Define Symbols # Define symbols using HTML-style code with specified colors and fonts twitter <- glue("<span style='color:{col3};font-family:fa-brands;'></span>") mastodon <- glue("<span style='color:{col3};font-family:fa-brands;'></span>") link <- glue("<span style='color:{col3};font-family:fa-solid;'></span>") data <- glue("<span style='color:{col3};font-family:fa-solid;'></span>") quote <- glue("<span style='color:{col3};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. taylor <- glue("<span style='color:{col1};font-family:ts;'>Taylor Swift</span>") acousticness <- glue("<span style='color:{col2}'>Acousticness</span>") danceability <- glue("<span style='color:{col3}'>Danceability</span>") energy <- glue("<span style='color:{col4}'>Energy</span>") instrumentalness <- glue("<span style='color:{col5}'>Instrumentalness</span>") speechiness <- glue("<span style='color:{col6}'>Speechiness</span>") valence <- glue("<span style='color:{col7}'>Valence</span>") # Define Title t <- glue("{taylor}'s Album Song Metrics") s <- glue("Exploring {acousticness}, {danceability}, {energy}, {instrumentalness},<br>{speechiness}, and {valence} in Taylor Swift's Albums") 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} taylor R package from W. Jake Thompson") # Data Manipulation and Visualization df <- taylor_album_songs %>% select(album_name,danceability,energy,speechiness,acousticness,instrumentalness,valence) %>% pivot_longer( cols = !album_name, names_to = "score", values_to = "value" ) %>% group_by(album_name,score) %>% summarise(mean = mean(value)) %>% na.omit()
df %>% ggplot(aes(x=score,y=mean,fill=score)) + geom_bar(stat="identity") + ylim(-0.2,0.8) + coord_polar(start = 0) + facet_wrap(~album_name,nrow(2)) + scale_fill_taylor(palette = "lover", guide = "none") + theme_minimal() + theme( strip.text = element_text(size = 8), plot.margin = margin(20, 20, 20, 20), axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank(), plot.title = element_markdown(size = 26, hjust = 0.5, lineheight = 1.3, family = "pica", color = col1), plot.subtitle = element_markdown(size = 22, hjust = 0.5, lineheight = 1.3, family = "pica", color = col1), plot.caption = element_markdown(size = 12, hjust = 0.5, lineheight = 1.3, family = "pica", color = col2) ) + labs(title=t,subtitle=s,caption=cap)
R Code Documentation
This R code is designed to generate a visualization using ggplot2 to explore and compare various audio metrics in Taylor Swift's albums. The code uses different libraries and custom fonts to create an aesthetic and informative chart.
Libraries Used
tidyverse
: Used for data manipulation and visualization.
tidytuesdayR
: Enables access to TidyTuesday datasets.
showtext
: Facilitates working with fonts in plots.
glue
: Used for text formatting.
ggtext
: Enhances text formatting in ggplot2.
tayloRswift
: (Custom library, if available) Provides additional functionality related to Taylor Swift data.
Load Data
tidyverse
: Used for data manipulation and visualization.tidytuesdayR
: Enables access to TidyTuesday datasets.showtext
: Facilitates working with fonts in plots.glue
: Used for text formatting.ggtext
: Enhances text formatting in ggplot2.tayloRswift
: (Custom library, if available) Provides additional functionality related to Taylor Swift data.The code loads data from the TidyTuesday dataset for the year 2023 and week 42. It fetches information about Taylor Swift's albums, album songs, and all songs from the dataset.
Load Fonts and Define Colors
Custom fonts are loaded to enhance text appearance, and specific color codes are defined for use in visualizations. These fonts and colors add a unique style to the chart.
Define Symbols
HTML-style code is used to define symbols with specified colors and fonts. These symbols are used in the chart to represent various elements and enhance visual appeal.
Define Title
The title and subtitle for the chart are defined using custom fonts and symbols. These elements contribute to the chart's aesthetics and provide context for the visualization.
Data Manipulation and Visualization
The code performs data manipulation to calculate the mean of specific audio metrics for each album. It then creates a polar bar chart using ggplot2 to visualize and compare these metrics. The chart is designed to be minimalistic and visually appealing.
Additional Information
The chart is accompanied by a caption that includes contact information and references to external resources, further enhancing its informativeness and usability.