R Stats Wrapped

After being inspired by the popular end-of-year tradition, Spotify Wrapped, I decided to create my own #RStatsWrapped using .Rmd files from 2022.
R
Author

Alex Reed

Published

December 31, 2022

Special thanks to Nicole Rennie for the inspiration for this blog post.1

Load Packages

show the code
library(NCmisc)
library(janitor)
library(tidyverse)
library(patchwork)
library(showtext)
library(ggtextures)
library(png)
library(cowplot)

Convert R Markdowns to R scripts

You can use the knitr::purl function to convert .Rmd files into .R scripts. If you prefer using .R scripts, this step is not necessary. To convert multiple files at once, you can create a function using the code below. Alternatively, if you prefer to convert the files manually, you can simply insert the filename for each .Rmd file one at a time.

show the code
#Convert R Markdown to R script
# knitr::purl("filepath/filename.Rmd")

Choose the directory where your converted .R scripts are located. Use the list.files function to create a character vector called all_files that contains the names of all files in the directory specified by file_path that have an “.R” or “.r” file extension.

show the code
file_path <- ("/Users/alexreed/Documents/MEDS/Website/reedalexandria.github.io/posts/2022-12-31-rwrapped/data")
all_files <- list.files(path = file_path, recursive = TRUE, pattern = "\\.[Rr]$")
all_functions <- map(.x = all_files, .f = ~ list.functions.in.file(file.path(file_path, .x)))

Select your top five packages

show the code
func_data <- unlist(all_functions) |>
  unname() |> 
  tabyl() |> 
  as_tibble() |> 
  rename(func = "unname(unlist(all_functions))") |>
  slice_max(n, n = 5, with_ties = FALSE) 

Find the hex sticker .png for each package

show the code
hex_imgs <- tibble(img = c("https://www.r-project.org/logo/Rlogo.png",
                       "https://www.r-project.org/logo/Rlogo.png",
                       "https://ggplot2.tidyverse.org/logo.png",
                       "https://ggplot2.tidyverse.org/logo.png",
                       "https://www.r-project.org/logo/Rlogo.png"))

Plotting top functions

First, add a font similar to the Spotify font. The font used by Spotify is Spotify Circular.

show the code
font_add("Circular", "/Users/alexreed/Downloads/Circular/CircularStd-Medium.otf")
showtext_auto()

Use ggplot2 to create the top function graphic.

show the code
top_functions <- ggplot() +
  # add text with the numbers 1 to 5
  geom_text(data = data.frame(),
            mapping = aes(x = rep(1, 5),
                          y = 1:5,
                          label = paste0("#", 1:5)),
            colour = "#00008b",
            size = 20,
            fontface = "bold",
            family = "Circular") +
  # add text with the names of the functions, and the number of times its used
  geom_text(data = func_data,
            mapping = aes(x = rep(2.25, 5),
                          y = 1:5,
                          label = paste0(func, "(), ", n, " times")),
            colour = "#00008b",
            hjust = 0,
            size = 11,
            fontface = "bold",
            family = "Circular") +
  # add images for each package
  geom_textured_rect(data = hex_imgs, 
                     aes(xmin = rep(1.5, 5), xmax = rep(2.1, 5),
                         ymax = 1:5-0.3, ymin = 1:5+0.3, image = img), 
                     lty = "blank",
                     fill="transparent",
                     nrow = 1,
                     ncol = 1,
                     img_width = unit(1, "null"),
                     img_height = unit(1, "null"),
                     position = "identity")  +
  # add title using geom_text() instead of labs()
  geom_text(data = data.frame(),
            aes(x = 2.45, y = 0, label = "My Top Functions"),
            colour = "#00008b",
            fontface = "bold",
            hjust = 0.5,
            size = 14,
            family = "Circular") +
  # set axis limits and reverse y axis
  scale_x_continuous(limits = c(0.9, 4)) +
  scale_y_reverse(limits = c(5.5, -0.2)) +
  # add a caption
  labs(caption = "#RStatsWrapped") +
  # set the theme
  theme_void() +
  theme(plot.background = element_rect(fill = "#ADD8E6", colour = "#ADD8E6"),
        panel.background = element_rect(fill = "#ADD8E6", colour = "#ADD8E6"),
        plot.margin = margin(40, 15, 10, 15),
        plot.caption = element_text(colour = "#46214a",
                                  margin = margin(t = 15),
                                  face = "bold",
                                  hjust = 1,
                                  size = 30,
                                  family = "Circular"))

top_functions

Add a geometry shape to the background

show the code
set.seed(123)
curve1 <- tibble(x = 1:100) %>% 
  mutate(y = 20 + smooth(cumsum(rnorm(100))))
inset1 <- ggplot(data = curve1,
       aes(x = x, y = y)) +
  geom_area(fill = "#46214a") +
  theme_void() +
  coord_fixed() +
  scale_y_reverse()

Using the patchwork package, you can overlay the geometry shape and the function graphic to create a single plot.

show the code
updated <- top_functions + inset_element(inset1, left = -1, right = 1.2, bottom = 0, top = 2.2, align_to = "full") &
  theme(plot.background = element_rect(fill = "#ADD8E6", colour = "#ADD8E6"),
        panel.background = element_rect(fill = "#ADD8E6", colour = "#ADD8E6"),
        plot.margin = margin(40, 7, 5, 7),
        plot.caption = element_text(colour = "#46214a",
                                    margin = margin(t = 5),
                                    face = "bold",
                                    hjust = 1,
                                    size = 30))

I then created an album cover in Canva. The photo is from a Justin Timberlake concert I attended in Las Vegas, Nevada.

show the code
album <- readPNG("/Users/alexreed/Documents/MEDS/Practice/wrapped/album_cover.png", native = TRUE)

Now, combine the graphic and album cover to complete the #RStatsWrapped graphic.

show the code
ggdraw(updated) + 
  draw_image(album,
             scale = 0.35,
             halign = 0.5,
             valign = 0.99)

show the code
#save final graphic
#ggsave("rwrapped_album.png", width = 2.5, height = 5, units = "in", )

Footnotes

  1. Nicola Rennie. December 3, 2022. nrennie.rbind.io/blog/2022-12-03-how-to-make-your-own-rstats-wrapped↩︎

Citation

BibTeX citation:
@online{reed2022,
  author = {Alex Reed},
  title = {R {Stats} {Wrapped}},
  date = {2022-12-31},
  url = {https://reedalexandria.github.io/2022-12-31-rwrapped},
  langid = {en}
}
For attribution, please cite this work as:
Alex Reed. 2022. “R Stats Wrapped.” December 31, 2022. https://reedalexandria.github.io/2022-12-31-rwrapped.