Interactive and Dynamic Social Network Visualization in R

Prof James P. Curley
6th April 2016

Section III. Temporal Networks

Some Current Options


static img → compiled video
e.g. png → mp4 (using ffmpeg)

gganimate (for ggplot based packages)

ndtv & networkDynamic

There are pros and several cons for all methods. It can be very hard to get the look you want.

Static images to video


  1. Produce and set initial layout of network nodes.

  2. Iterate through each time interval of the network, remaking the network using the initial layout of the nodes (some jitter ok).

  3. Optional: Hide nodes/edges that are not active yet or not active in a time bin.

  4. Generate static images for each time unit (or several images per time unit for smoother transitions)

  5. Compile them into a movie using ffmpeg or something similar.

igraph + ffmpeg


# Set Output for each frame 
png(file="output_%03d.png", width=800,height=450)


#Time loop starts
for(time in seq(1, total_time,dt)){   #dt is the defined interval between successive plots

  gt <- delete_edges(g,which(E(g)$time > time)) #remove absent edges

  layout.new <- layout_with_fr(gt,coords=layout.old,niter=10,start.temp=0.05,grid="nogrid") #jitter layout

  plot(gt,layout=layout.new,vertex.label="",vertex.size=1+2*log(degree(gt)),
       vertex.frame.color=V(g)$color,edge.width=1.5,asp=9/16,margin=-0.15) #make plot

  layout.old <- layout.new #keep layout for next image
}


This code is based on an example by Esteban Moro. My version is here.

Improve Video Quality


The video examples here are for demo purposes - they aren't optimized!

I chose the level of video quality to fit into a talk. There are several things that can be done to improve video quality including:

  • improving the quality/size of original images (e.g. use svg)
  • use more transitional images between time bins to smooth transitions.
  • increasing the frame rate in ffmpeg
  • specify video bitrates and codecs that optimize performance
  • etc.

ggnetwork/ggplot + ffmpeg


Can also do this for ggnetwork output:

  1. Flatten igraph/network object to a dataframe using ggnetwork

  2. Identify time point when nodes first appear

  3. Make list of dataframes for each time, adding in rows with only nodes that have appeared by this time-point (use “NA” for all variables except x/y/vertex.name)

  4. Add in other nodes to df but you NA for x/y (to ensure color consistency)

5.. Use ggsave to store all images.

ggnetwork + ffmpeg

df.sp <- split(df, df$time)

for(i in 1:max(df$time)){

 #to ensure color consistency among groups
tmp<-rbind(df.sp[[i]],
           data.frame(x=NA,y=NA,Group=1:max(df$Group),
                      na.x=NA,vertex.names=NA,xend=NA,yend=NA,na.y=NA,time=NA))

  pp <- ggplot(tmp, aes(x = x, y = y, xend = xend, yend = yend)) +
    geom_edges(color = "black") +
    geom_nodes(aes(color = factor(Group)), size = 8) + 
    geom_nodetext(aes(label = vertex.names),color="black", fontface = "bold") +
    theme_blank() +
    xlim(0,1)+ylim(0,1) +
    theme(legend.position="none") +
    guides(color=guide_legend(keyheight=0.3,default.unit="inch",override.aes = list(size=6)))

   ggsave(pp,filename=paste0("output_",i,".png", sep=""))

}

This example can be found here.

This output is too fast - I did not put images between changes in nodes yet to make it smoother - will update

ggnetwork + gganimate


The same plot as above can be made directly in R using David Robinson's gganimate package which wraps the animation package to animate ggplot2 plots. My example here.

It also requires ffmpeg being installed.

There are various options that can be used to change the quality of output (it can output to other format types e.g. gif)

ggraph + gganimate


Similarly we can use gganimate with ggraph - though I have yet to fully explore this option.

Here is one example.

  • this visualizes edges over time but fades older ones out using a delay.

  • the delay effect is achieved by adjusting the edge weight by 'delay' time.

  • edge color is achieved using the gEdges function.

ggraph + gganimate


We use frame to determine the 'timebin' to compile images by, and set edge attributes using gEdges and geom_edge_link0 by time using the 'delay' variable.

geom_node_point(aes(size = degree, colour = factor(group))) + 

geom_edge_link0(aes(frame = timebins, alpha = delay, width = delay, colour = factor(node1.group)), data = gEdges(nodePar = 'group')) +  


Thomas Pedersen’s example is available here.