It’s time for a Twitter book draw again: every month, a random Locke Data Twitter follower wins an excellent data science book! This month’s book was Weapons of Math Destruction : How Big Data Increases Inequality and Threatens Democracy. The animation I chose to create was inspired by the idea of destruction and by my wanting to try out the fantastic new API of the gganimate
package, and a very fast new gif encoder, gifski
.
I am not a designer nor an artist, but I like imagining new animations to announce the book winner, that I want to be fun, and useful by illustrating the use of some nifty R tools. That’s a good way for me and you to learn new R skills! I’ve reported on my efforts with magick
to create a crystal ball for chibi Steph, R package for image manipulation, and with particles
, R package for simulating, well, particles, to move followers’ names around!.
This month I thought about what destruction meant to me, and got the rather simple idea to have glass shatter, thus revealing the winner’s name.
Feel free to skip over this section and go to the code directly, unless you want to know more about my search and process to write said code!
I wondered how I could shatter glass, i.e. cut a blue square in small pieces and make these pieces fall and somehow ended up thinking that I could draw random points and then use their Voronoi tiles as fragments. I’m not sure glass shattering really looks like this but I decided against experimenting with my windows.
I wasn’t too sure how to make and use the fragments though. I re-read Thomas Lin Pedersen’s excellent post about making a Christmas card using Voronoi tesselation but as nice and well-explained as it was, it wasn’t exactly what I was after. I set out to find a way to create Voronoi tiles as polygons and then to shift at least some of them downwards to create the animation.
I tried using the deldir
package a bit to create Voronoi tesselation out of random points but its output didn’t make me too happy since it was segments rather than polygons. Re-creating polygons from them didn’t sound too easy. I ditched this idea and googled keywords such as Voronoi and polygons and R and then sf
the geospatial package since the first results indicated Voronoi tesselation was well supported for mapping stuff, and found this very useful Stack Overflow post that made me adapt an example from sf
documentation.
Once I had the polygons, the rest was animation as usual, well not really, since gganimate
new API by Thomas Lin Pedersen is different, and so powerful! I haven’t watched Thomas’ useR! keynote talk about The Grammar of animation yet, but I look forward to it since I both admire his packages, and his blog writing. I only use very basic gganimate
stuff here. A good intro to its grammar can be found at https://github.com/thomasp85/gganimate/tree/master#gganimate- .
I was also glad to read that gifski
is now on CRAN for all your gif making needs (it’s actually the default gif renderer of gganimate
). This package by Jeroen Ooms is not only a wrapper to the fastest gif renderer around which is cool enough in itself, but also the first CRAN package that interfaces a Rust library! Read more about gifski
in this tech note.
The first part of the code consisted of drawing random points and using them as a basis for a Voronoi tesselation inside a box.
1 |
|
The animation will consist of two parts:
-
The fragments appearing by drawing their borders with different shades from blue like the background to white.
-
The fragments falling.
The code below creates the PNGs corresponding to the first part of the animation.
1 |
|
Then I made the data.frame
a bit bigger by adding a second state with most tiles fallen at the bottom. I first create a variable border
that indicates if the tile pertains to the sides or top because magically these tiles won’t fall.
1 |
|
This was followed by the animating of the second part with gganimate
, here after creating a fake winner (sorry Steph, you won’t get to gift yourself the book). Because we’ll need to use PNGs from the first part, we don’t use gganimate
’s built-in gif or video renderers, but instead save PNGs.
Important note: gganimate
latest version isn’t on CRAN yet, follow the instructions in its GitHub repository or download a built source version of the package from Appveyor.
1 |
|
Now we can use all the PNGs that are in the “august_frames” folder, with gifski
!
1 |
|
And voilà , a glass shattering animation!
Glass shattering book winner reveal
In this post I explained how I created an animation of glass shattering.
R packages that are important to know for producing animations are:
Have fun animating R visualizations! And don’t forget that next month, again a random Locke Data follower will win a great book: you should follow Locke Data on Twitter to be in with a chance of winning!