Skip to content

Instantly share code, notes, and snippets.

@thomasp85
Last active October 13, 2021 01:33
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasp85/e6280e554c08f00c9e46f8efca2a5929 to your computer and use it in GitHub Desktop.
Save thomasp85/e6280e554c08f00c9e46f8efca2a5929 to your computer and use it in GitHub Desktop.
Temporal Network Viz
library(ggraph)
library(tidygraph)
library(gganimate)
# Data from http://konect.uni-koblenz.de/networks/sociopatterns-infectious
infect <- read.table('out.sociopatterns-infectious', skip = 2, sep = ' ', stringsAsFactors = FALSE)
infect$V3 <- NULL
names(infect) <- c('from', 'to', 'time')
infect$time <- as.POSIXct(infect$time, origin = Sys.time() - as.numeric(Sys.time()))
gr <- tbl_graph(edges = infect, directed = FALSE) %>%
mutate(degree = centrality_degree())
# Animation Code
p <- ggraph(gr, 'lgl') +
geom_edge_link0(edge_colour = '#991814', edge_width = 1, edge_alpha = 1) +
geom_node_point(aes(size = degree), colour = '#1B2733', show.legend = FALSE) +
scale_size_area(max_size = 4) +
ggtitle('Human Interactions', subtitle = 'Time of encounter {format(frame_time, "%H:%M")}') +
theme_graph() +
theme(plot.background = element_rect(fill = '#B5C9BA'),
plot.title = element_text(color = '#F77976')) +
transition_events(start = time, enter_length = hms::hms(hours = 3), exit_length = hms::hms(hours = 3)) +
enter_fade() +
exit_fade()
animate(p, 100, 10)
@thomasp85
Copy link
Author

gganimate_birth13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment