I would like to animate the same network using different layouts and having a smooth transition between layouts. I'd like to do this inside the gganimate
framework.
library(igraph)
library(ggraph)
library(gganimate)
set.seed(1)
g <- erdos.renyi.game(10, .5, "gnp")
V(g)$name <- letters[1:vcount(g)]
l1 <- create_layout(g, "kk")
l2 <- create_layout(g, "circle")
l3 <- create_layout(g, "nicely")
long <- rbind(l1,l2,l3)
long$frame <- rep(1:3, each =10)
Following the ggplot
approach, I store the node positions in the long format (long
) and add a frame
variable to each layout.
I tried to make it work with the following code, which is working fine and almost what I want. However, I cannot seem to find a way to include the edges:
ggplot(long, aes(x, y, label = name, color = as.factor(name), frame = frame)) +
geom_point(size = 3, show.legend = F) +
geom_text(show.legend = F) +
transition_components(frame)
I also tried to add the edges as geom_segment
but ended up with them being static while the nodes kept moving. This is why I use the ggraph
package and fail:
ggraph(g, layout = "manual", node.position = long) +
geom_node_point() +
geom_edge_link() +
transition_components(frame)
I'd like to have an animation of one network with changing node positions that both displays nodes and edges.
Any help is much appreciated!
Edit: I learned that one can include the layout directly into ggraph (and even manipulate the attributes). This is what I've done in the following gif. Additionally geom_edge_link0'
instead of geom_edge_link
is being used.
ggraph(long) +
geom_edge_link0() +
geom_node_point() +
transition_states(frame)
Note that the edges are not moving.