This code creates a visually appealing graphic output with a boxplot showcasing the educational scores of English towns and cities by region. The use of Font Awesome icons and custom text formatting adds a unique touch to the title, subtitle, and caption.
The Code
# load libraries library(tidyverse)
library(tidytuesdayR) library(showtext)
library(glue) library(ggtext) # load data tuesdata <- tidytuesdayR::tt_load(2024, week = 4)
## ## Downloading file 1 of 1: `english_education.csv`
english_education <- tuesdata$english_education # 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 <- "darkgrey" # Text creation using Font Awesome icons and custom styles 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 # Title, subtitle, and caption text t <- "<b>Educational Score of English towns and cities by region</b>" s <- glue("Tidy Tuesday 2024 | Week 4") 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}The{space2}UK{space2}Office{space2}for{space2}National{space2}Statistics{space2} {space2}{hash}{space2}tidytuesday") # Plotting the educational scores using ggplot2 english_education %>% select(rgn11nm,education_score) %>% na.omit() %>% ggplot(aes(x=education_score, y = rgn11nm, color = rgn11nm,fill=rgn11nm)) + geom_boxplot(show.legend = F, alpha = 0.5, outlier.shape = NA) + geom_jitter(show.legend = F) + scale_color_manual(values = c(thematic::okabe_ito(),"grey")) + scale_fill_manual(values = c(thematic::okabe_ito(),"grey")) + facet_wrap(~rgn11nm,scales = "free_y") + theme( axis.title.y = element_blank(), axis.text.y = element_blank(), plot.title = element_markdown(hjust = 0.5, lineheight = 1.3), plot.subtitle = element_markdown(hjust = 0.5, lineheight = 1.3), plot.caption = element_markdown(hjust = 0, lineheight = 1.3) ) + labs(title = t, subtitle = s, caption = cap, x = "Education Score")
R Code Documentation
This R code generates an output that includes a boxplot visualization of the educational scores of English towns and cities by region. The code utilizes various R packages for data manipulation, visualization, and font handling. Below is a detailed explanation of the code.
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.
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.The code loads data from the TidyTuesday dataset for the year 2024 and week 04. It fetches information about English towns and cities 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 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.
Additional Information
The chart is accompanied by a caption that includes contact information and references to external resources, further enhancing its informativeness and usability.