What

What is a dynamic graph? A graph that includes an additional dimension which renders is dynamic. Here we’ll create a temporal graph

How

Start by making the graph, see “Get Started” vignette if you do not know how to get tweets.

tweets %>% 
  gt_edges(screen_name, mentions_screen_name, created_at) %>% 
  gt_nodes() %>% 
  gt_dyn() %>% 
  gt_collect() -> net

A few things to note.

  1. We pass created_at in gt_edges as we want to know the time at which the edge is created.
  2. We get the nodes with gt_nodes
  3. We use gt_dyn (short for “dynamic”) to get the dynamic graph.

Let’s look at the output.

knitr::kable(head(net$edges))
source target created_at n end
_colinfay colebemis 2018-08-23 20:17:23 1 2018-08-25 16:46:28
_colinfay joygraceeya 2018-08-24 09:55:08 1 2018-08-25 16:46:28
_colinfay winvectorllc 2018-08-24 13:00:37 1 2018-08-25 16:46:28
_evgeniy joelgrus 2018-08-25 08:51:06 1 2018-08-25 16:46:28
_evgeniy klmr 2018-08-25 08:51:06 1 2018-08-25 16:46:28
_evgeniy rstudio 2018-08-25 08:51:06 1 2018-08-25 16:46:28

The edges include the created_at column (date time when the tweet is posted).

knitr::kable(head(net$nodes))
nodes start end type n
_alexgs 2018-08-22 14:12:34 2018-08-25 16:46:28 user 1
_colinfay 2018-08-22 16:02:15 2018-08-25 16:46:28 user 4
_evgeniy 2018-08-25 08:51:06 2018-08-25 16:46:28 user 4
_inundata 2018-08-24 23:40:53 2018-08-25 16:46:28 user 1
_kaushik7 2018-08-24 01:23:30 2018-08-25 16:46:28 user 1
_lionelhenry 2018-08-22 14:13:17 2018-08-25 16:46:28 user 1

The nodes include start and end column, this is added by the gt_dyn function; which, in effect, simply computes when the node should appear on the graph.

I use sigmajs to visualise the dynamic graph, please see the website if you want to understand the code below, being a different package I do not get into this here. In brief, I convert date time to numeric and rescale edge appearances between 0 and 10 seconds (so that the animation lasts 10 seconds), I then add 5 seconds so that the animation starts after 5 seconds (to give you a chance to see it in action).

Click the trigger to add the edges.

library(sigmajs)

# convert to numeric & rescale
edges <- net$edges %>% 
  dplyr::mutate( 
    id = 1:n(),
    created_at = as.numeric(created_at),
    created_at = (created_at - min(created_at)) / (max(created_at) - min(created_at)),
    created_at = created_at * 10000
  )

nodes <- net$nodes %>% 
  dplyr::mutate(
    id = nodes,
    label = nodes,
    size = n
  )

# graph layout 
nodes <- sg_get_layout(nodes, edges)

nodes <- sg_get_cluster(nodes, edges, colors = c("#2780e3", "#d3d3d3")) # cluster

sigmajs() %>% 
  sg_nodes(nodes, id, size, label, x, y, color) %>% 
  sg_add_edges(edges, created_at, id, source, target, 
               cumsum = FALSE, refresh = TRUE) %>% 
  sg_button("<i class='fa fa-play'></i>", "add_edges", class = "btn btn-primary") %>% 
  sg_settings(defaultNodeColor = "#1967be")

Note that you can also dynamically add nodes, much of it explained in the twinetbook.

Twinetbook