Making Network Animations Using NDTV package

The following commands are for making a network animation (moving networks) using R packages statnet and ndtv.

Load packages

library(statnet)
library(ndtv)
library(rio)
library(knitr)

Load data

riv <- import("C:/Users/bomim/Documents/website-hugo/content/post/toyriv.dta")
vtx <- import("C:/Users/bomim/Documents/website-hugo//content/post/node.csv")
vtx87 <- import("C:/Users/bomim/Documents/website-hugo/content/post/vtx1987.csv")

Look at the data

head(riv)
##   year stateabb1 stateabb2 no
## 1 1987       AFG       PAK  1
## 2 1992       AFG       PAK  2
## 3 1997       AFG       PAK  3
## 4 2002       AFG       PAK  4
## 5 1997       AFG       TAJ  3
## 6 1997       AFG       UZB  3
head(vtx87)
##   stateabb ccode dem        cinc
## 1      AAB    58   0 0.000002740
## 2      AFG   700   0 0.001255717
## 3      ALB   339   0 0.000529850
## 4      ALG   615   0 0.004066098
## 5      AND   232   0 0.000000000
## 6      ANG   540   0 0.001150265

Make a list of networks

nv <- nrow(vtx)

rivlist <- list()

for(i in 1:4){
  rivi <- subset(riv, riv$no == i)
  rivii<-cbind(rivi$stateabb1, rivi$stateabb2)
  netyr <- network.initialize(nv, directed=FALSE)
  network.vertex.names(x=netyr) <- vtx$stateabb
  netyr[rivii] <- 1
  set.vertex.attribute(x=netyr,attrname="order",val=rep(i,nv))
  rivlist[[i]] <- netyr
}
  • Set vertex attribute
set.vertex.attribute(rivlist[[1]],names(vtx87),vtx87)

cols1<-vector()
for(i in 1:204){
  ifelse(rivlist[[1]]$val[[i]]$dem==0, 
         cols1[i] <- "red", cols1[i]<-"blue")
}
  • Make a plot
plot(rivlist[[1]], 
     vertex.col=cols1, 
     edge.col="grey50",
     xpd=T)

Make GIFs!

rivdyn <- networkDynamic(network.list = rivlist)
rivgif <- compute.animation(rivdyn, 
                            default.dist = 3,
                            animation.mode = "MDSJ",
                            verbose = FALSE)
render.d3movie(rivgif,
               displaylables=FALSE, 
               vertex.col=cols1)

You can look at the network animation by clicking Video button above.

Bomi K. Lee
Bomi K. Lee
Ph.D candidate

My research interests include international conflict, rivalry, and political methodology.