cjolley - 1 year ago 150
R Question

# R: How can I rearrange the order of edges in an igraph plot?

I'm trying to make a network plot in igraph that highlights certain important edges by coloring them differently than the others. For large graphs, they often get buried under the others. For example:

``````library(igraph)
test <- barabasi.game(200,m=2)
E(test)\$color <- "gray"
E(test)[1]\$color <- "red"
sort(order(E(test)\$color)[E(test)],decreasing=TRUE)
plot(test,
vertex.label=NA,
vertex.shape="none",
vertex.size=0,
edge.arrow.mode=0,
edge.width=2)
``````

gives me a plot where the single red edge is at the bottom.

If I choose to color a higher-numbered edge (rather than #1) it has a better chance of not being buried.

So it seems to me that one option is to somehow re-order the edges. I tried

``````E(test) <- E(test)[order(E(test)\$color)]
``````

but that gets me an "invalid indexing" error. Any ideas about what else I should try?

Answer Source

igraph plots the edges in the order they appear in the graph's edge list, so you are right, edges with higher IDs will be drawn on top of the edges with lower IDs. Unfortunately igraph does not provide an easy way to reorder the edges of the graph (although it has a function named `permute.vertices`, which will allow you to permute the vertices), so the only way I can think of right now is that you need to construct another graph in which the edges are in the "right order". `make_graph` ensures that the edges are stored in the graph exactly in the order you specify them, and I think so does `graph_from_data_frame`.

Another option (if you don't want to reconstruct the entire graph) is to plot the graph twice: first you plot the "not-so-important" edges and set the width of the important ones to zero, then you plot the important edges on top.

If you would like edge permutations to be supported in an upcoming version of igraph, please file a feature request on Github.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download