2

Does anyone know of a way to visualize a network (from igraph) as it evolves (i.e., new connections are made)? I've looked on https://www.r-graph-gallery.com/network/ and searched online but haven't seen anything.

For example, if the network is:

library("tidyverse")
library("igraph")

net.bg <- sample_pa(20) 
V(net.bg)$size <- 8
V(net.bg)$label <- "" 
E(net.bg)$arrow.mode <- 0

net.bg.df <- igraph::as_data_frame(net.bg) 

net.bg.df <- net.bg.df %>%
  mutate(time_frame = 1:n())

l <- layout_randomly(net.bg)

plot(net.bg, layout=l)

Is there a way to transition the animation by the field time_frame similar to a normal plot animation like:

library(ggplot2)
library(gganimate)
library(gapminder)
theme_set(theme_bw())

p <- ggplot(
  gapminder, 
  aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)
) +
  geom_point(show.legend = FALSE, alpha = 0.7) +
  scale_color_viridis_d() +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(x = "GDP per capita", y = "Life expectancy")

p + transition_time(year) +
  labs(title = "Year: {frame_time}")
Frank B.
  • 1,813
  • 5
  • 24
  • 44
  • Just found `ndtv-d3` under the section `dynamic network visualisation` on this article : https://kateto.net/network-visualization. Hope it helps – Paul Endymion Jun 12 '19 at 12:50

1 Answers1

2

You can use the ggraph package, which can also be used together with gganimateand handles igraph objects well.

For this to work we need to specify the time points in which the edges should be active. This is not very elegantly done by creating a list of starting time and end points (which is the number rows in the original dataset).

library(tidyr)
library(ggraph)
library(gganimate)
df0 <- net.bg.df
df0$time_frame <- as.numeric(df0$time_frame)
for(i in 1:nrow(df0)){
  df0$time_frame[i] <- list(df0$time_frame[i][[1]]:19)
}
df <- unnest(df0, time_frame)
g2 <- graph_from_data_frame(df)

l <- as.data.frame(l)  # ggraph only accepts data.frame
colnames(l) <- c("x", "y") # ggraph needs these column names
ggraph(g2, layout = "manual", node.position = l) +
  geom_node_point(color = "blue", size =3) +
  geom_edge_link0(show.legend = F, width = 1) +
  theme_classic() +
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank()) +
  transition_states(time_frame) +
  ggtitle(paste0("time point: ", "{closest_state}"))




And the rest is plotting the network and using the transition_states- function. Here are additional sources.

appearing edges

Ben Nutzer
  • 1,082
  • 7
  • 15