<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/summertime-fun-r-graphs-with-lacroixcolor-magick-animations-more/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.littlemissdata.com/blog/lacroix"> Little Miss Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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.  </p>
<p> <img data-image-dimensions="1600x899" data-image-focal-point="0.5,0.5" src="https://i0.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e4c/1527602054498/3LaCroix.jpeg?w=456&ssl=1" alt=" Image courtesy of  medium.com  " data-load="false" data-type="image" data-image-id="5b0e10f5575d1f5bd3615e4c" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e4c/1527602054498/3LaCroix.jpeg"></p>
<p>Image courtesy of <a rel="nofollow" href="https://medium.com/@inaherlihy/beverage-innovation-more-healthy-gulps-d3a16e756ab5" target="_blank">medium.com</a></p>
<h2>Lacroix is for Summer</h2>
<p>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<a rel="nofollow" href="https://www.nasdaq.com/symbol/fizz/historical" target="_blank"> nasdaq.com</a>.  We will be exploring the data in the color palette of my favorite LaCroix flavor; Peach Pear.  This is made possible with the new <a rel="nofollow" href="https://github.com/johannesbjork/LaCroixColoR" target="_blank">LaCroixColoR</a> package created by <a rel="nofollow" href="https://twitter.com/johannesbjork" target="_blank">Johannes Björk</a>.  We will then be adding appropriate gif stickers to the graphs, courtesy of <a rel="nofollow" href="https://giphy.com/stickers" target="_blank">giphy.com</a> and the <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/vignettes/intro.html" target="_blank">magick</a> package created by <a rel="nofollow" href="https://twitter.com/opencpu" target="_blank">Jeroen Ooms</a>.  Note that when I was creating this blog, I referenced both the <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/vignettes/intro.html" target="_blank">magick tutorial</a> and an awesome <a rel="nofollow" href="https://www.danielphadley.com/ggplot-logo/" target="_blank">“how to” blog on adding animations</a> by <a rel="nofollow" href="https://twitter.com/danielphadley" target="_blank">Daniel Hadley</a></p>
<h2>Set Up</h2>
<p>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 <a rel="nofollow" href="https://www.littlemissdata.com/blog/r-package-install" target="_blank">here</a>. 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. </p>
<p> </p>
<pre>######## Set Up Work ########

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

#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 &lt;-mdy(df$date)
df$month &lt;- month(df$mdy, label = TRUE)
df$year &lt;- as.factor(year(df$mdy))
df$wday &lt;- wday(df$mdy, label = TRUE)
head(df)
attach(df)</pre>
<p> </p>
<h2>Create the LaCroix plot with the LaCroix color palette</h2>
<h2> </h2>
<p>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!</p>
<p> </p>
<pre>######## Image One - The LaCroix Curve ########

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

# Save the base plot
ggsave(fizz, file=&quot;fizz.png&quot;, width = 5, height = 4, dpi = 100)
plot &lt;- image_read(&quot;fizz.png&quot;)
plot

</pre>
<p> </p>
<p> <img data-image-dimensions="1014x824" data-image-focal-point="0.5,0.5" src="https://i2.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e4e/1527600141437/LaCroix-FIZZ.png?w=456&ssl=1" alt="LaCroix-FIZZ.png" data-load="false" data-type="image" data-image-id="5b0e10f5575d1f5bd3615e4e" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e4e/1527600141437/LaCroix-FIZZ.png"></p>
<p> </p>
<h2>Adding Animations</h2>
<p>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? <a rel="nofollow" href="https://www.danielphadley.com/ggplot-logo/" target="_blank">Or have Vincent Vega pointing at your x-axis?</a>  This type of activity is known as data nerd humor, or graph comedy.  </p>
<p>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.  </p>
<p>To add an animation to your saved plot, you need to do 5 key parts:  </p>
<ul>
<li>Bring in your gif and apply any transformations (scale, rotation, etc)</li>
<li>Set the background image</li>
<li>Flatten the animation and background into frames</li>
<li>Turn the frame into an animation</li>
<li>Print and save</li>
</ul>
<h3>Animation 1 –  The curve starts climbing</h3>
<p>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.  </p>
<p> </p>
<pre>#Bring in the gif - scale and rotate
laCroixAnimation &lt;- image_read(&quot;https://media.giphy.com/media/h7ZuxGCxXTRMQ/giphy.gif&quot;) %>%
  image_scale(&quot;150&quot;) %>%
  image_rotate(-30)
laCroixAnimation

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

#Save gif
image_write(animation, &quot;laCroixImage1.gif&quot;)</pre>
<p> <img data-image-dimensions="500x400" data-image-focal-point="0.5,0.5" src="https://i1.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e50/1527600067473/laCroixImage1.gif?w=456&ssl=1" alt="laCroixImage1.gif" data-load="false" data-type="image" data-image-id="5b0e10f5575d1f5bd3615e50" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e50/1527600067473/laCroixImage1.gif"></p>
<h3>Animation 2 – Climbing the Mountain</h3>
<p>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.  </p>
<p> </p>
<pre>#Use base plot previously created
#Bring in the gif - scale and rotate
laCroixAnimation &lt;- image_read(&quot;https://media.giphy.com/media/l378xFKDBZO9Y5VUk/giphy.gif&quot;) %>%
  image_scale(&quot;300&quot;) %>%
  image_rotate(10)
laCroixAnimation
# Combine the plot and animation
# Background image
background &lt;- image_background(image_scale(plot, &quot;500&quot;), &quot;white&quot;, flatten = TRUE)
# Combine and flatten frames
frames &lt;- image_composite(background, laCroixAnimation, offset = &quot;+100+50&quot;)
# Turn frames into animation
animation &lt;- image_animate(frames, fps = 5)
print(animation)

#Save gif
image_write(animation, &quot;laCroixImage2.gif&quot;)


</pre>
<p> <img data-image-dimensions="500x400" data-image-focal-point="0.5,0.5" src="https://i2.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e52/1527602891908/laCroixImage2.gif?w=456&ssl=1" alt="laCroixImage2.gif" data-load="false" data-type="image" data-image-id="5b0e10f5575d1f5bd3615e52" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e52/1527602891908/laCroixImage2.gif"></p>
<p> </p>
<h3>Animation 3 – The Sad Decline</h3>
<p>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.</p>
<p> </p>
<pre>######## Image Three - Sad Decline ########

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

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

#Save gif
image_write(animation, &quot;laCroixImage3.gif&quot;)</pre>
<p> <img data-image-dimensions="500x400" data-image-focal-point="0.5,0.5" src="https://i0.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e54/1527604390541/laCroixImage3.gif?w=456&ssl=1" alt="laCroixImage3.gif" data-load="false" data-type="image" data-image-id="5b0e10f5575d1f5bd3615e54" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e54/1527604390541/laCroixImage3.gif"></p>
<h2>Thank You and Enjoy Your LaCroix</h2>
<p> <img data-image-dimensions="1166x874" data-image-focal-point="0.5,0.5" src="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e56/1527647877613/?format=1000w" data-load="false" data-type="image" data-image-id="5b0e10f5575d1f5bd3615e56" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b0e10f5575d1f5bd3615e56/1527647877613/"></p>
<p>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 <a rel="nofollow" href="https://twitter.com/LittleMissData" target="_blank">twitter</a>. </p>
<p>Note that the full code is available on my  <a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/lacroix" target="_blank">github</a><a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/lacroix" target="_blank"> repo</a><a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/Heatmap" target="_blank">.</a>  If you have trouble downloading the file from github, go to the <a rel="nofollow" href="https://github.com/lgellis/MiscTutorial" target="_blank">main page of the repo</a> and select “Clone or Download” and then “Download Zip”.</p>
<p> </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.littlemissdata.com/blog/lacroix"> Little Miss Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/65c9W2kHiUM" height="1"></a></div>