Data Driven Christmas Card Animation with Voronoi Tiles
Data Imaginist - R posts
[This article was first published on Data Imaginist - R posts, 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.
I recently published a bit of self-promotion in the form of an animation based
on the new geom_voronoi_tile() and geom_voronoi_segment() functions in
ggforce:
This prompted someone to ask for the source code and I off-handedly said I would
make a blog post about. So here we are, but I’m not going to show you how I made
that animation (sorry). Instead I’m going to show you how to make your very own
animated Christmas card using ggplot2, ggforce, and tweenr (as well as
mgcv and deldir for some computations).
Let’s set the scene: we are going to do a cyclic animation switching between a
heart, the word “merry”, and the word “x-mas”. Each shape will be made up of
voronoi tiles with the colour of the tile defining the shape. The shapes has
been created in Illustrator and exported to a json representation using a
script.
Let’s start by importing the shapes and do a sanity check:
That seems sort-of right. The shape is on its head since Illustrator measures
its coordinates from the top-left corner and down and not from the bottom-left
corner and up. Well simply use scale_y_reverse() later to circumvent this.
Now that we have our shapes it is time to draw them with points. To do this
we’ll sample 25.000 points within the plot area and check which ones falls into
the different shapes. There are many ways to test whether a point lies inside a
shape – I’ll use the in.out() function from the mgcv package.
We could call this a day and continue with all those points, but we wont. First,
because voronoi tessellation (as implemented in deldir) does not scale well to
these number of points, and second, because the tiles would be so small that
they are almost indistinguishable from each other.
To fix this we are going to remove a lot of the points that do not help in
defining the shapes. In order to determine which points are safe to remove we
are going to calculate the delaunay triangulation (the “inverse” of a voronoi
tesselation) and find out which points only have neighbors of the same colour.
We are going to remove as many random point so well end up with 1.000 points of
each colour:
Now we’re getting somewhere!
The next step is to match the points from each stage together so we know which
one moves where. For the transition between the heart and the text I’m fine with
it being random, but for the transition between “merry” and “x-mas” I want it to
seem as smooth as possible. This requires that each point in merry_points get
matched to its nearest neighbor of equal colour in the xmas_points data.frame.
As some points might compete for the nearest neighbor some will have to settle
for neighbors a bit farther away. While it might be possible to optimise the
choice of neighbor we are just going to go through each in turn so the points
occuring first in the data.frame will get first pick:
Now each data.frame is lined up so the point encoded in row 1 is the same across
all. The last bit of work we need to do is to interpolate the transition between
each stage. This could be done in one line of code with tweenr using
tween_states() but I dont want all points to start and end their movements at
the same time. Thus we need to do something a bit more custom:
This function takes two data.frames along with the total transition length and
a stagger value that encodes the maximum offset in time each transition can
have in terms of starting and ending. It then interpolates the x and y
columns between the two data.frames, adding a frame column to the end result.
Let’s put it to good use. Our animation will take 12 seconds at a frame rate of
24 fps making a total of 288 frames and 96 frames for each stage along with the
transition to it. We’ll split it up so that each stage is hold for 36 frames,
ending up with 60 frames for each transition. We’ll allow the start and end time
to vary with 20 frames each:
In addition to this we need to repeat each stage 36 times and offset the frame
count. In the end we’ll combine everything into one data.frame:
We now have everything in place to create our animation – we just need to define
the plotting function (take note that we specify the bounding rectangle as the
range of our data might change between frames):
Now it is just a matter of looping over each frame and plotting that data.
Normally gganimate would be useful for this but it does not always play nice
when data is heavily transformed by the stat function (as is the case with
geom_voronoi_tile() and geom_voronoi_segment()). We’ll just do it the
oldschool way (if you’re running this yourself wrap the following expression
in animation::saveGIF()):
Merry Christmas!
To leave a comment for the author, please follow the link and comment on their blog: Data Imaginist - R posts.