R code for igraph animation

For my personal code archive and everybody who finds interest in it I publish the R code which I used to create the frames for the animations showing the carpoolings formed through the booking system until a certain date. The graphs are created using igraph and plotted into frames which are later glued into an MPEG clip using ffmpeg.


Guidance for the code

The graph and its growth is all contained in one CSV file keeping three columns. A driver’s ID, a passenger’s ID and the the date the passenger took a ride with the driver – according to our booking system – this list defines the possible edges.

From this data set I extract two data frames – one keeping all ID’s for the drivers and the date of his earlieast offered ride (where a passenger was found for) and the other one keeping all ID’ for the passengers and the date of his earliest participation – this list defines the possible nodes.

Why do I need the date of earliest participation as driver or passenger? Because the animation goes through the dates sequentially, starting April 1 2011. So to create the graph  for a given date D I only keep the nodes (drivers/passengers) who participated on date D or earlier. For those nodes I then select the necessary edges and I have all I need to define the graph. Also I use the first date of participation to determine whether or not to emphasize the node as new.

Another key aspect about my chosen approach is to calculate the positions for all nodes together – using a force-directed algorithm called Fruchterman-Reingold. All the nodes not yet to be displayed will be set to transparent color. So the positions are determined for all nodes and edges (the final state) but only the ones with activity until D are displayed. That was the best solution I found using igraph.

The code!


Leave a Reply

Your email address will not be published. Required fields are marked *