Here we demonstrate a brief example of how to use the showtext package to customize fonts in a ggplot2 visualization. We first import a toy data set on Taylor Swift songs with data obtained from the Genius API.

# For custom Google fonts: https://fonts.google.com/
library(showtext)
library(tidyverse)

# Importing CSV file
swiftSongs <- read_csv("https://raw.githubusercontent.com/dilernia/STA418-518/main/Data/swiftSongsFull.csv")

We then create a bar chart showing the number of songs for each album using the satisfy font.

# Downloading Google font for plots
fontFamily <- "satisfy"
font_add_google(name = fontFamily, 
                family = fontFamily)

showtext_auto()

# Creating bar chart with custom font
swiftSongs %>% 
  ggplot(aes(x = album_name)) + 
    geom_bar() +
    labs(title = paste0(fontFamily, " font"),
         y = "Number of songs",
         x = "Album",
         caption = "Data source: Genius API") +
    theme_minimal() + 
    theme(legend.position = "none",
          text = element_text(family = fontFamily,
                              size = 20))

Then, a bar chart showing the number of songs for each album using the Dancing Script font.

# Downloading Google font for plots
fontFamily2 <- "Dancing Script"
font_add_google(name = fontFamily2, 
                family = fontFamily2)

# Creating bar chart with custom font
swiftSongs %>% 
  ggplot(aes(x = album_name)) + 
    geom_bar() +
    labs(title = paste0(fontFamily2, " font"),
         y = "Number of songs",
         x = "Album",
         caption = "Data source: Genius API") +
    theme_minimal() + 
    theme(legend.position = "none",
          text = element_text(family = fontFamily2,
                              size = 20))

Then, a bar chart showing the number of songs for each album using the Lobster font.

# Downloading Google font for plots
fontFamily3 <- "Lobster"
font_add_google(name = fontFamily3, 
                family = fontFamily3)

# Creating bar chart with custom font
swiftSongs %>% 
  ggplot(aes(x = album_name)) + 
    geom_bar() +
    labs(title = paste0(fontFamily3, " font"),
         y = "Number of songs",
         x = "Album",
         caption = "Data source: Genius API") +
    theme_minimal() + 
    theme(legend.position = "none",
          text = element_text(family = fontFamily3,
                              size = 20))