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(text, screen_name, status_id, "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 end
dataandme Emil_Hvitfeldt 2018-07-14 13:02:06 2018-07-15 11:25:09
dataandme smarterpoland 2018-07-13 02:09:05 2018-07-15 11:25:09
dataandme mitchoharawildhttps//tco/TL0rc18f0v 2018-07-12 21:17:03 2018-07-15 11:25:09
dataandme rdpeng’s 2018-07-13 18:46:04 2018-07-15 11:25:09
dataandme wwwDSLA 2018-07-13 18:46:04 2018-07-15 11:25:09
dataandme djnavarro 2018-07-13 13:59:13 2018-07-15 11:25:09

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

knitr::kable(head(net$nodes))
source start end type n_edges
__jsta 2018-07-13 17:07:12 2018-07-15 11:25:09 user 1
alkesh 2018-07-14 08:52:58 2018-07-15 11:25:09 user 2
_ColinFay 2018-07-14 07:57:43 2018-07-15 11:25:09 user 4
_ColinFay)Make 2018-07-14 17:17:15 2018-07-15 11:25:09 user 1
_J_sinclair 2018-07-14 05:33:35 2018-07-15 11:25:09 user 3
lacion 2018-07-15 11:15:53 2018-07-15 11:25:09 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 = source,
    label = source,
    size = n_edges
  )

# graph layout 
l <- suppressMessages(sg_get_layout(nodes, edges))
nodes$x <- l$x
nodes$y <- l$y

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) %>% 
  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.