Dynamic graphs

John Coene

2018-11-18

You also add nodes and edges to static graphs. Below we graph the nodes then add (sg_add_edges) at random intervals (0.5 to 2 seconds between each edge added). We also stop the layout algorithm after all edges have been aded (+ 100 milliseconds to make sure it stops when all edges are plotted).

# generate data using convenience functions
nodes <- sg_make_nodes(50)
edges <- sg_make_edges(nodes)
edges$delay <- runif(15, 500, 750) # between .5 and .75 seconds between each edge

last_edge <- sum(edges$delay) + 100

sigmajs() %>%
  sg_force_start() %>%
  sg_nodes(nodes, id, size, color) %>%
  sg_add_edges(edges, delay, id, source, target, refresh = TRUE) %>% # read delay documentation
  sg_force_stop(last_edge) %>% 
  sg_button(
    "add_edges", # event
    class = "btn btn-primary", # button class
    tag = tags$a,
    "Add edges" # label
  )
Add edges

You can also show helpful text as elements appear on the graph. Let’s add somewhat random dates to our example to simulate edges being added over the course of 25 days.

# adding random but ordered dates to edges
dates <- seq.Date(from = Sys.Date(), Sys.Date() + 24, "days")
dates <- sample(dates, nrow(edges), replace = TRUE)

edges$dates <- dates[order(dates)]

sigmajs() %>%
  sg_force_start() %>%
  sg_nodes(nodes, id, size, color) %>%
  sg_add_edges(edges, delay, id, source, target, refresh = TRUE) %>% # read delay documentation
  sg_progress(edges, delay, dates, class = "text-warning", position = "bottom") %>% # add progress text
  sg_force_stop(last_edge) %>% 
  sg_button(
    "add_edges", # event
    class = "btn btn-primary",
    tag = tags$a,
    "Add edges" # label
  )
Add edges

You can also drop nodes or edges with sg_drop_edges and sg_drop_nodes, same principles, I’ll let you explore the examples.