Skip to content

Instantly share code, notes, and snippets.

@jalapic
Created April 5, 2016 22:15
Show Gist options
  • Save jalapic/2dd5a01bbf624604951890837e65d03b to your computer and use it in GitHub Desktop.
Save jalapic/2dd5a01bbf624604951890837e65d03b to your computer and use it in GitHub Desktop.
ggraph + gganimate
#devtools::install_github('thomasp85/ggforce')
#devtools::install_github("dgrtwo/gganimate")
#devtools::install_github('thomasp85/ggraph')
#devtools::install_github("hadley/ggplot2")
library(ggraph)
library(ggforce)
library(gganimate)
library(ggplot2)
library(igraph)
set.seed(787)
gr <- barabasi.game(100)
edges <- data.frame(from = get.edgelist(gr)[,1], to = get.edgelist(gr)[,2])
edges <- edges[order(edges$from),]
edges$time <- 1:nrow(edges)
edges$timebins <- as.numeric(cut(edges$time, breaks = 100))
# We want that nice fading effect so we need to add extra data for the trailing
edgesAnim <- lapply(1:10, function(i) {edges$timebins <- edges$timebins + i; edges$delay <- i; edges})
edges$delay <- 0
edgesAnim <- rbind(edges, do.call(rbind, edgesAnim))
edgesGraph <- graph_from_data_frame(edgesAnim, directed = F)
# We use only original data for the layout
subGr <- subgraph.edges(edgesGraph, which(E(edgesGraph)$delay == 0))
V(subGr)$degree <- degree(subGr)
V(subGr)$group <- cluster_edge_betweenness(subGr)$membership
E(subGr)$class <- V(subGr)$group[-length(V(subGr))] #add group membership to edge based on 'head'
lay <- createLayout(subGr, 'igraph', algorithm = 'lgl')
# Then we reassign the full graph with edge trails
attr(lay, 'graph') <- edgesGraph
# Now we create the graph with timebins as frame
p <- ggraph(data = lay) +
geom_node_point(aes(size = degree, colour = factor(group))) +
geom_edge_link0(aes(frame = timebins, alpha = delay, width = delay, colour = factor(class))) +
# geom_edge_link0(aes(frame = timebins, alpha = delay, width = delay), edge_colour = '#ffffff') +
scale_edge_alpha(range = c(1, 0), guide = 'none') +
scale_edge_width(range = c(0.5, 1.5), trans = 'exp', guide = 'none') +
scale_size(guide = 'none') +
ggtitle('Temporal Ordering of Edge Attachment') +
ggforce::theme_no_axes() +
theme(plot.background = element_rect(fill = '#000000'),
panel.background = element_blank(),
panel.border = element_blank(),
plot.title = element_text(color = '#000000'),
legend.position="none")
p
# And then we animate
animation::ani.options(interval=0.2)
#gg_animate(p, 'animation.gif', title_frame = FALSE)
gg_animate(p, 'animation.mp4', title_frame = FALSE)
@thomasp85
Copy link

See my fork at https://gist.github.com/thomasp85/c8e22be4628e4420d4f66bcc6c88ac87

Take note of the use of the gEdges function to automatically populate the edge data with node parameters (within the eom_edge_link0 function call)

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