Reputation: 1335
I would like to maintain graph layout (vertex position) after removing edges. An example:
library(igraph)
library(ggraph)
# create sample graph
g <- structure(list(from = c(1, 1, 2, 2, 3, 3),
to = c(2, 3, 4, 5, 6, 7)),
class = "data.frame", row.names = c(NA, 6L))
g <- graph_from_data_frame(g)
# plot with all edges
ggraph(g, layout = 'tree') +
geom_edge_diagonal() +
geom_node_point(size = 10) +
theme_void()
Now remove a couple edges and replot
g2 <- delete.edges(g, c(3,5))
ggraph(g2, layout = 'tree') +
geom_edge_diagonal() +
geom_node_point(size = 10) +
theme_void()
Whereas this is the desired output:
Is there a simple way to maintain vertex positions after edge removal?
Upvotes: 2
Views: 344
Reputation: 1335
After writing out the question I figured it out, you just need to save the layout from the first graph using igraph::layout_as_tree
(or any other layout), which can be used for the second plot:
l <- igraph::layout_as_tree(g)
ggraph(g2, layout = l) +
geom_edge_diagonal() +
geom_node_point(size = 10) +
theme_void()
Hopefully this saves someone else a bit of time.
Upvotes: 1