Summertime Fun: R Graphs with LaCroixColoR, Magick Animations + More!

[This article was first published on 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.

Now that we are back from the Memorial day long weekend, does anyone else feel like we’ve kicked off summer?  I know that summer doesn’t begin officially until June 21, but I am already in the spirit.  It might seem strange to “be in the spirit” since I’m living in Austin, Texas and we get temperatures up to 95-99 degrees Fahrenheit (35 to 37 degrees Celsius).  But I think as a  Canadian, I’ve been programmed to believe summer is fun.  Heat is FUN darn it!   I mean what is really to complain about?  We’ve got AC, water parks, and LaCroix to keep us cool.  

 Image courtesy of  medium.com

Image courtesy of medium.com

Lacroix is for Summer

To get in the mood for summer, this blog is LaCroix themed!  We will be exploring the stock prices of National Beverage Corp (FIZZ) which is the LaCroix parent company.  Data was provided courtesy of nasdaq.com.  We will be exploring the data in the color palette of my favorite LaCroix flavor; Peach Pear.  This is made possible with the new LaCroixColoR package created by Johannes Björk.  We will then be adding appropriate gif stickers to the graphs, courtesy of giphy.com and the magick package created by Jeroen Ooms.  Note that when I was creating this blog, I referenced both the magick tutorial and an awesome “how to” blog on adding animations by Daniel Hadley

Set Up

The first thing we need to do is install and load all required packages for our summertime fun work with R.  These are all of the install.packages() and library() commands below.  Note that some packages can be installed directly via CRAN and some need to be installed from github via the devtools package.  I wrote a blog on navigating various R package install issues here. We then download the data set from my github profile through the fread() function. We can then create a few helper date columns which make it easier to display in the graph. 

 

######## Set Up Work ########

#Install necessary packages
install.packages("ggplot2")
install.packages("magrittr")
install.packages("magick")
install.packages("devtools")
install.packages("curl")
install.packages("data.table")
install.packages("lubridate")
install.packages("devtools")
library(devtools)
install_github("johannesbjork/LaCroixColoR")

#Load necessary packages
library(LaCroixColoR)
library(ggplot2)
library(magick)
library(magrittr) 
library(curl)
library(data.table)
library(lubridate)

#Bring in the nasdaq  data from Nasdaq.com for the LaCroix parent company:
#National Beverage Corp
#https://www.nasdaq.com/symbol/fizz/historical

df= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/lacroix/FIZZ_Stock.csv')
attach(df)
df$mdy <-mdy(df$date)
df$month <- month(df$mdy, label = TRUE)
df$year <- as.factor(year(df$mdy))
df$wday <- wday(df$mdy, label = TRUE)
head(df)
attach(df)

 

Create the LaCroix plot with the LaCroix color palette

 

Next, we want to create our graph which plots the FIZZ stock price over time.  To give the graph a more appropriate LaCroix style flair, we will employ the employ the LaCroixColoR package.  We will call on the lacroix_palette function when assigning the colors through the scale_color_manual function in ggplot 2.  When calling the lacroix_palette function, we need to specify a flavor and of course I picked my favorite flavor: Peach Pear!

 

######## Image One - The LaCroix Curve ########

# Create base plot
fizz <-ggplot(data=df, aes(x=mdy, y=close, color=year)) + 
  geom_point() + 
  ggtitle("FIZZ Stock (LaCroix Parent Company)") + 
  scale_color_manual(values=lacroix_palette("PeachPear",type = "continuous", n=11)) +
  labs(x = "Date", y="Closing Stock Price")

# Save the base plot
ggsave(fizz, file="fizz.png", width = 5, height = 4, dpi = 100)
plot <- image_read("fizz.png")
plot

  LaCroix-FIZZ.png  

Adding Animations

Now it's time to inject even more fun into these graphs - ANIMATIONS!  In case you are wondering, yes this is the most important thing to spend your time doing.  Come on this is fun!  Who doesn't want to have a cartoon character climbing their graph? Or have Vincent Vega pointing at your x-axis?  This type of activity is known as data nerd humor, or graph comedy.  

In this blog, we are breaking the graph into three parts of the stock journey and adding an animation for each phase: initial stock climb, climbing the mountain and the sad decline.  

To add an animation to your saved plot, you need to do 5 key parts:  

  • Bring in your gif and apply any transformations (scale, rotation, etc)
  • Set the background image
  • Flatten the animation and background into frames
  • Turn the frame into an animation
  • Print and save

Animation 1 -  The curve starts climbing

For this image, I wanted to bring in a sticker with a lot of zest!  This lucky guy just realized his stock is climbing.  He is stoked and doing a happy dance.  

 

#Bring in the gif - scale and rotate
laCroixAnimation <- image_read("https://media.giphy.com/media/h7ZuxGCxXTRMQ/giphy.gif") %>%
  image_scale("150") %>%
  image_rotate(-30)
laCroixAnimation

# Combine the plot and animation
# Set the Background image
background <- image_background(image_scale(plot, "500"), "white", flatten = TRUE)
# Combine and flatten frames
frames <- image_composite(background, laCroixAnimation, offset = "+150+120")
# Turn frames into animation
animation <- image_animate(frames, fps = 5)
print(animation)

#Save gif
image_write(animation, "laCroixImage1.gif")
laCroixImage1.gif

Animation 2 - Climbing the Mountain

For this image, I wanted to bring in a sticker with an earned sense of confidence.  This stock climb is step but the little buddy is sticking with it.  He deserves to give a triumphant wave.  

 

#Use base plot previously created
#Bring in the gif - scale and rotate
laCroixAnimation <- image_read("https://media.giphy.com/media/l378xFKDBZO9Y5VUk/giphy.gif") %>%
  image_scale("300") %>%
  image_rotate(10)
laCroixAnimation
# Combine the plot and animation
# Background image
background <- image_background(image_scale(plot, "500"), "white", flatten = TRUE)
# Combine and flatten frames
frames <- image_composite(background, laCroixAnimation, offset = "+100+50")
# Turn frames into animation
animation <- image_animate(frames, fps = 5)
print(animation)

#Save gif
image_write(animation, "laCroixImage2.gif")


laCroixImage2.gif

 

Animation 3 - The Sad Decline

For this image, I wanted to bring in a sticker that reflects the sad realization of a stock hitting somewhat of a free fall.   I am doing my part to boost the LaCroix stock prices, but I'm only one woman.  And therefore, we need a crying cat at the end of this curve to reflect the cooling off of stock prices in 2017/2018.

 

######## Image Three - Sad Decline ########

#Use base plot previously created
#Bring in the gif - scale
laCroixAnimation <- image_read("https://media.giphy.com/media/xUA7bfJ4OF11xXe4Fy/giphy.gif") %>%
  image_scale("80")
laCroixAnimation

# Background image
background <- image_background(image_scale(plot, "500"), "white", flatten = TRUE)
# Combine and flatten frames
frames <- image_composite(background, laCroixAnimation, offset = "+360+150")
# Turn frames into animation
animation <- image_animate(frames, fps = 10)
print(animation)

#Save gif
image_write(animation, "laCroixImage3.gif")
laCroixImage3.gif

Thank You and Enjoy Your LaCroix

Thanks for reading along while we took a silly little journey through LaCroix stock prices, the LaCroixColoR package and Magick animations.  Please feel free to let me know your thoughts in the comments or on twitter

Note that the full code is available on my  github repo.  If you have trouble downloading the file from github, go to the main page of the repo and select "Clone or Download" and then "Download Zip".

 

To leave a comment for the author, please follow the link and comment on their 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.

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)