Loading packages, data, fonts and difining colors
# Loading packages library(tidyverse)
library(tidytuesdayR) library(showtext)
library(glue) library(ggtext) # Loading data tuesdata <- tidytuesdayR::tt_load(2023, week = 35)
## ## Downloading file 1 of 2: `fair_use_cases.csv` ## Downloading file 2 of 2: `fair_use_findings.csv`
fair_use_cases <- tuesdata$fair_use_cases fair_use_findings <- tuesdata$fair_use_findings # Loading fonts and colors font_add_google("Montserrat", "montserrat") 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() bg <- "white" col1 <- "black"
Text generation
# 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>") hash <- glue("<span style='color:{col1};font-family:fa-solid;'>#</span>") data <- 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 t <- "<b>Copyright: Fair use founds by court</b>" s <- "Tidy Tuesday 2023 | Week 35" 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}U.S. Copyright Office Fair Use Index{space2} {space2}{hash}{space2}tidytuesday")
Plot
ggplot(data = fair_use_cases, aes(x = fair_use_found,fill = fair_use_found, color = fair_use_found)) + geom_bar(show.legend = F) + facet_wrap(~court, scales="free_y") + theme_bw() + labs(title = t, subtitle = s, caption = cap) + theme(strip.text = element_text(size = 8, family = "montserrat"), plot.margin = margin(10, 10, 10, 10), plot.title = element_markdown(size = 20, hjust = 0, lineheight = 1.3, family = "montserrat"), plot.subtitle = element_markdown(size = 18, hjust = 0, lineheight = 1.3, family = "montserrat"), plot.caption = element_markdown(size = 12, hjust = 0, lineheight = 1.3, family = "montserrat"), axis.text = element_text(size = 8, family = "montserrat"), axis.title = element_blank() )
R Code Documentation
Overview
This R script is designed to create png output that visualizes data related to fair use cases found by courts. It utilizes several R packages and custom fonts to customize the appearance of the plot and text elements. The script fetches data for Tidy Tuesday 2023, Week 35, and uses the `ggplot2` package to create a bar chart with facet wrapping. The chart's title, subtitle, and caption are styled using custom fonts and icons.
Packages Used
- `tidyverse`: A collection of data manipulation packages, including `ggplot2` for data visualization.
- `tidytuesdayR`: A package for loading Tidy Tuesday datasets.
- `showtext`: A package for using custom fonts in plots and text.
- `glue`: A package for string interpolation and formatting.
- `ggtext`: A package for styling text in `ggplot2` plots.
Loading Data
- The script loads data for Tidy Tuesday 2023, Week 35, specifically the `fair_use_cases` and `fair_use_findings` datasets from the Tidy Tuesday repository.
Loading Fonts and Colors
- Custom fonts are added using the `showtext` package.
- Three custom fonts are loaded: 'Montserrat', 'Font Awesome 6 Free-Regular', 'Font Awesome 6 Brands-Regular', and 'Font Awesome 6 Free-Solid'.
- Background color (`bg`) is set to white, and primary text color (`col1`) is set to black.
Text Creation
- Custom text strings are created using the `glue` package and styled with custom fonts and colors. These strings include icons for Twitter, Mastodon, links, data, and hashtags.
- The `space` and `space2` strings are used to create spaces with custom background colors.
Plot Creation
- A bar chart is created using `ggplot2` with the `fair_use_cases` dataset.
- The `x` aesthetic is set to `fair_use_found`, and the bars are filled and colored based on the same variable.
- Facets are wrapped by the `court` variable with free y-scales.
- The plot is styled with a black-and-white theme, custom fonts, and custom font sizes.
- The title, subtitle, and caption are set with the previously created text strings.
- Various theme elements such as text size, margin, and title alignment are customized.
Output
The output of this script is a png file that contains the styled bar chart along with the specified title, subtitle, and caption. The chart includes custom icons and fonts to enhance its appearance and readability. This HTML file can be easily shared or embedded in web pages for data visualization purposes.