Search code examples
ranimationsnastatnet

Dynamic vertex attribute in ndtv


I have been trying to implement dynamic vertex attributes in ndtv and I actually managed to create different sized vertices but upon closer inspection they did not correspond to the values I thought I had specified. Instead the vertex size was just proportional to the vertex ID with the first ID having the largest size. I tried to control the size by creating a vertex spell for each period with a different value for the size parameter

steps <- 500
num_agents <- 40
vs <- as.data.frame(cbind(rep(1:num_agents, each = steps + 1), rep(1:(steps + 1), times = num_agents), rep(1:(steps + 1), times = num_agents)))
names(vs) <- c("vertex.id", "onset", "terminus")
vs$dimension <- gather(means, key = "key", value = "value", names(means)[1]:names(means)[ncol(means)])[,2]

Resulting in the following, where vs are the vertex spells and es the edge spells

head(vs)
  vertex.id onset terminus dimension
1         1     1        1  5.000000
2         1     2        2  2.777778
3         1     3        3  2.040816
4         1     4        4  1.492537
5         1     5        5  1.388889
6         1     6        6  1.315789

head(es[,1:4])
  onset terminus head tail
1     2      501   12   24
2     2      501    2   24
3     2      501   38    2
4     2      501   21    2
5     2      501   28    2
6     2      501   11   30

trade.net <- network(links[,3:4], directed = TRUE, matrix.type = "edgelist", loops = FALSE, multiple = FALSE, ignore.eval = FALSE)

net.dyn <- networkDynamic(base.net = trade.net, edge.spells = es[,1:4], vertex.spells = vs[,c(2,3,1,4)], create.TEAs = TRUE, vertex.TEA.names = "dimension")


net.dyn <- compute.animation(net.dyn, animation.mode = "kamadakawai",
                  slice.par=list(start = 1, end = 10, interval=1, 
                         aggregate.dur=1, rule='any'))


render.d3movie(net.dyn, usearrows = TRUE, 
       displaylabels = F, 
       bg="#111111", 
       vertex.border="#dddddd",
        vertex.cex = net.dyn %v% 'dimension.active',
       edge.col = '#55555599',
       render.par=list(tween.frames = 10, show.time = FALSE),
       slice.par=list(start = 1 , end = 10, interval = 1, aggregate.dur = 1, rule='any'))

I only rendered part of the data (first 10 of 501 periods) but not only do the attributes not match with what I intended but they are also invariant across time.


Solution

  • I found a workaround using the fact that one can use functions inside the rendering command.

    render.d3movie(net.dyn, usearrows = TRUE, 
           displaylabels = F, 
           bg="#111111", 
           vertex.border="#dddddd",
            vertex.cex = function(slice){
                degree(slice)/sum(degree(slice)) * 10}, 
           edge.col = '#55555599',
           render.par=list(tween.frames = 10, show.time = FALSE),
           slice.par=list(start = 1 , end = 10, interval = 1, aggregate.dur = 1, rule='latest')
           )
    

    This works fine for what I intended to achieve but doesn't use the TEAs.