Happy Holidays 2019
As we gear up for Christmas day tomorrow, I wanted to take this time to wish a very happy holiday season to all of my fellow data geeks!
Rather than posting a happy holiday e-Card, I thought I might indulge in creating some gratuitous holiday themed graphs. In this blog we will showcase a tool called coordinator, created by Aliza Aufrichtig which allows us to convert SVG graphics to data co-ordinates. This will enable our graphed holiday message. We will leverage the ggimage and magick packages (again) to apply a background image. Finally, we will use gganimate, to create a little holiday gif.
Load The Packages
library(data.table)
library(tidyverse)
library(gganimate)
library(ggimage)
library(magick)
library(gifski)
Download The Data
The csv is created with a tool called coordinator, by Aliza Aufrichtig. It allows us to convert SVG graphics to data co-ordinates. More on this in a previous blog post.
df= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Holidays/Holidays.csv', stringsAsFactors = FALSE)
Create A Holiday Graph
Create a classic ggplot scatterplot. Remove all lines and chart aspects with theme_void(). Increase the chart size beyond the plotted points using the xlim() and ylim() functions. This will allow us to nicely center the text on our background image.
# Basic scatter plot
p <-ggplot(df, aes(x=x, y=y)) +
geom_point(colour = '#BF0000', size = 0.3) +
xlim(0, 18000) +
ylim(0, 10000) +
theme_void()
p
Add A background Holiday Image
Add the holiday background using the ggbackground() function in the ggimage package. I got this tip from a great blog post by Guangchuang Yu.
img <- "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Holidays/%20background.jpg"
imgRead <- image_read(img)
ggbackground(p, img)
Animation
Create an animated holiday greeting with the gganimate package. Use transition_manual() vs transition_reveal() because it allows us to specify cumulative=TRUE to keep all data already plotted. Use the gifski package and anim_save() function to save the gif.
t <-ggplot(df, aes(x=x, y=y)) +
theme_void() +
theme(plot.background = element_rect(fill = '#CA302F')) +
geom_point(colour = 'white', size = 1) +
transition_manual(as.numeric(x), cumulative=TRUE)
gif <- animate(t, end_pause = 25, width = 800, height = 400, fps = 8)
gif
anim_save("HappyHolidays.gif", gif)
Thank You!
Thank you again to my fellow data lovers for such a wonderful 2019. I can’t wait to tackle 2020 with y’all!
Please note that the full code is available on my github repo. If you have trouble downloading the files or cloning the repo from github, please go to the main page of the repo and select "Clone or Download" and then "Download Zip". Alternatively or you can execute the following R commands to download the whole repo through R
use_course("https://github.com/lgellis/MiscTutorial/archive)