4 min read

How to make a single cover in R

I finally managed to finish writing and producing a new song just in time for the new year! I decided I was going to try and put together a cover for the new song in R. What follows shows exactly how I did.

I’ve previously done some artwork that I used in a previous single cover. For that one I just copy and pasted the artwork into the rest of the cover. This time I wanted to do more in R.

The big eureka moment was when I realized that I could use the fantastic patchwork package to arrange a bunch of plots into a single plot. The first thing was composing the plots that would be my artist name and the name of the single.

Create artist name and name of single

The heavens really opened up when I ran into the string2path package. This package makes it a piece of cake turn a string into a ggplot object.

Let’s load the package along with some other packages that make things a little easier. I’m also going to put together a list of colors I played with for the cover.

library(pacman)
p_load_gh("yutannihilation/string2path")
#devtools::install_github("yutannihilation/string2path")
p_load(here, tidyverse, ggforce, Rcpp, patchwork, jpeg, grid, gganimate)

alex_f_color <- c(
  "#DAD2D6",
  "#6B9849",
  "#1A3621",
  "#773532",
  "#C7A372"
)

Now I’ll use the string2path() function in the string2path package to draw the artist name using a font I have downloaded. I then turn the string2path object into a tibble which then makes it possible to throw into a ggplot and voila, we have the artist name done.

alex_f <- string2path(
  "Alex F",
  "index_files/Motter Ombra Normal.ttf",
  tolerance = 0.00001
  )

alex_f <- tibble::rowid_to_column(alex_f)

alex_f_plot <- 
  ggplot(alex_f) +
  geom_path(aes(x, y, group = id), color = alex_f_color[4], size = 3) +
  theme_minimal() +
  coord_equal() +
  theme_void() +
  theme(legend.position = "none") 

alex_f_plot

Now we do the same for the single name.

image <- string2path(
  "image",
  "index_files/ArtNouveauCaps.ttf",
  tolerance = 0.00001
  )

image <- tibble::rowid_to_column(image)

image_plot <- 
ggplot(image) +
  geom_path(aes(x, y, group = id), color = alex_f_color[4], size = 3) +
  theme_minimal() +
  coord_equal() +
  theme_void() +
  theme(legend.position = "none") 

image_plot

Main single cover image

With the artist name and the single name all done the next thing was to tackle the main image of the single cover.

A year or so back I ran into the prisma phone app and knew that this would be a great opportunity to use it. The app uses deep learning and neural networks to turn photos into art prints.

I took this profile selfie shot:

After processing it through the prisma app I got:

Perfect! The next step is to get this image into a ggplot and add some pizazz. I decided an easy and effective visual would be to add a white grid on top of the picture. This would hopefully give the sense that the image of me is that of a bunch of separate parts - sort of like a puzzle coming together to create an “image” (the title of the song I released).

In the following steps I bring in the picture. Then I create a data frame which I’ll need in the ggplot to create the grid. This step took a little playing around with but the end result was perfect, exactly what I had in mind.

img <- readJPEG("alex_selfie_processed.jpg")

x <- LETTERS[1:10]
y <- seq(1,10)
data <- expand.grid(X = x, Y = y)

window_plot <-
ggplot(data, aes(X, Y)) +
  annotation_custom(
    rasterGrob(
      img,
      width = unit(.95, "npc"),
      height = unit(.9, "npc")
    ),
    -Inf, Inf, -Inf, Inf
  ) +
  geom_tile(
    width = 1, 
    height = 1,
    color = "white",
    alpha = 0.01, 
    size = 4.75
      #3.75
  ) +
  theme_void() +
  theme(
    panel.background = element_rect(fill = "white")
  )

window_plot

Putting it all together

The last and final step was to use the patchwork package to put these 3 different elements together into the final single cover.

alex_image <- (alex_f_plot | plot_spacer() | image_plot) / window_plot +
  theme_void() +
  plot_layout(heights = c(1, 12))

alex_image

And that’s it! If you’d like to give a listen to the song I wrote which uses this cover you can listen on your streaming platform of choice here or listen here on Spotify:

This little project was a lot of fun and I hope to do more R artwork for these music projects of mine in the future. I know there is a ton more that can be done. Do you have any ideas? Let me know! I would love to hear them.

BONUS!

The gganimate package is the perfect companion to the string2path package! I can use it to do a slow reveal of the artist name and single name.

I used these 2 gifs to create a preview of the song I wrote to send to friends, family, and post on social media.

ggplot(alex_f) +
  geom_path(aes(x, y, group = id), color = alex_f_color[4], size = 1.5, lineend = "round") +
  theme_minimal() +
  coord_equal() +
  theme_void() +
  theme(legend.position = "none") +
  transition_reveal(rowid)

plot of chunk unnamed-chunk-6

ggplot(image) +
  geom_path(aes(x, y, group = id), color = alex_f_color[4], size = 1.5, lineend = "round") +
  theme_minimal() +
  coord_equal() +
  theme_void() +
  theme(legend.position = "none") +
  transition_reveal(rowid)

plot of chunk unnamed-chunk-7