Happy Holidays 2019

December 24, 2019
By

[This article was first published on Blog - Little Miss Data, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

unnamed-chunk-3-1.png

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)

unnamed-chunk-4-1.png

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)

HappyHolidays.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)

To leave a comment for the author, please follow the link and comment on their blog: Blog - Little Miss Data.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)