Animated graphs, another alternative for Data Visualization

[This article was first published on My Data Atelier » R, 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.

peli 8mm

The world of Data Visualization offers infinite variants to display our Data. However, there is still some reluctance in exploiting all the possibilities that computers give us nowadays in this field, probably not because of a rejection of novelties but due to certain tendency of sticking to a vision related to “paper support”, i.e. to representations that could be put into paper. Even if interactive options are included (such as menus with filters, pivot tables, etc.), the way the data is finally displayed does not differ very much from what could have been done a hundred years ago; pie charts, bars, axes plots, etc.

This has of course several limitations: there is no possibility to show something dynamic, which is definetly not merely just an aesthetic detail. Animations can sometimes evidence considerably easier the incidence of time in the data we are exhibiting than presenting any static model.

Of course, this can be done in R. Below you will find the code for a hipothetic network graph and how the connections (edges) occur throughout time. In this post, the idea is to focus on the animation features, which can be used in any other type of graph. In the future, I will go on deeper in social networks and graphs.

Any comments, suggestions, critics or corrections please feel free to write. Enjoy!!

#load libraries

#This is an invented Dataset that describes the edges generated throughout time between the different nodes. In order to understand this example, we have to assume that this is the order in which the edges where generated. In order to understand it, you have to read it by column, i.e., the first edge origined is 1 and 2, the second 3 and 4, the third 5 and 6, etc.

a <- c(1, 3, 5, 7, 1, 3, 2, 4, 1, 3, 2, 4, 1, 2, 5, 6)
b <- c(2, 4, 6, 8, 5, 7, 6, 8, 6, 8, 5, 7, 3, 4, 7, 8)

DB <- as.matrix(data.frame(a,b))

#create the graph

graph <- graph.edgelist(DB, directed=FALSE)

#reset the record of frames. In order to ensure that what we are going to animate does not include something else, it is always a good practice to reset before starting


# loop created to generate the animation. As there is no way to do it “cummulative”, it is always the same graph depicted (with all its nodes) but some of the edges are white to give the idea of progressive connection

for ( i in 1:nrow(DB))
colour <- c(rep(‘red’,length = i), rep(‘white’, length = nrow(DB)-i))
plot(graph,, edge.color= colour)

#There are many possibilities to save the animation created. Below you will find only 2

#This is the best option. as I do not have the chance to store the images in a server and use the saveHTML function which is indeed considerably better. If you are going to run the animation locally or you have a server to store it, I would highly recommend this option.


#This is the one I used in the post. It creates an mp4 file. Therefore, you will need to download ffmpeg.

saveVideo(ani.replay(), = “graphanimation.mp4″, ffmpeg = “(the path where you have your ffmpeg archive)”)

This is the final result uploaded in Youtube. The quality is definetly not the best one.

To leave a comment for the author, please follow the link and comment on their blog: My Data Atelier » R. 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)