This question is something of a follow-on from a previous question of mine and I have been wrestling with for a while.
I am attempting to visualise networks in R using the iGraph package, however, I want to visualise different subsets of the same network in the exact same layout. So there will be vertices which differ between the subsets, but I want each vertex and edge to be in the same place in each plot.
Initially I tried to do this by creating the coordinates of an overall graph and then plotting subgraphs with the same coordinates. However, this ended up having several issues:
- iGraph bases vertex positions on their number, so as soon as you remove one vertex from a graph for a subgraph, the numbers of all subsequent vertices change and therefore their positions will be wrong.
- By giving the coordinates of vertices which no longer exist, iGraph (I think) still tries to plot them so you can end up with nodes unconnected to the subgraph.
So, instead I tried a different tactic. Now I plot the overall graph but try to simply colour our the parts of the graph I do no want. So, in the following example:
set.seed(123)
g_overall = erdos.renyi.game(25, 0.3)
removals = c("2" ,"5" ,"13", "19", "25")
coords = layout_as_tree(g_overall,
root = 1,
circular = FALSE,
flip.y = FALSE,
mode = "all"
)
V(g_overall)$colour = ifelse(V(g_overall) %in% removals, "blue", "red")
plot.igraph(g_overall,
layout = coords,
vertex.shape = "none",
vertex.label = V(g_overall),
vertex.label.color = V(g_overall)$colour
)
This gives the following:
Then if I change the code to:
plot.igraph(g_overall,
layout = coords,
vertex.shape = "none",
vertex.label = ifelse(V(g_overall)$colour == "red", V(g_overall), NA),
vertex.label.color = V(g_overall)$colour
)
This becomes:
This is ALMOST right now, however obviously there are now edges which need to be removed and I don't know how to approach that. Ideally I would use a similar ifelse loop with something like: ifelse(starting_node$colour == "red", edge_colour = "black", edge_colour = "white"
so that the edge colour is based on the nodes its connected to (and goes invisible like the nodes). But I cannot think of a way to do this.
Any advice appreciated.
P.s. I will, of course, look into answers which are based around the complete removal of edges, but I suspect that this will change the layout and so would rather stick to "hiding" them.