简体   繁体   中英

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?

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.

You can reconstruct the graph with the edges reordered using "as_data_frame" and "graph_from_data_frame" fairly easily in conjunction with the dplyr and tidyr packages:

new_graph=graph_from_data_frame(d=as_data_frame(old_graph,what="edges") %>%
    arrange(desc(ordering_attribute)),
    vertices=as_data_frame(old_graph,what="vertices"))

if you had a layout stored on the old graph you would need to transfer it manually...

new_graph$layout = old_graph$layout

或者您可以根据您希望订购的变量重新排列数据,然后将其提供给igraph

The reason why E(test) <- E(test)[order(E(test)$color)] can not work out is that we are not allowed to assign an 'igraph.es' variable to another existing one or itself; however, we can create a new 'igraph.es' varibale using: a <- E(test)[order(E(test)$color)] . Thus, we need create a new graph to inherit a new order of edges from the orginal graph:

library(igraph)
test <- barabasi.game(200,m=2)
E(test)$color <- "gray"
E(test)[1]$color <- "red"

# to make a new graph:test2 whose edges are descended from the ordering edges of the orginal graph:test
test2 <- make_graph(as.vector(t(get.edgelist(test)[order(E(test)$color),])))
# to assign the attribute value to the edges of the new graph
E(test2)$color <- E(test)$color[order(E(test)$color)]

plot(test2,
 vertex.label=NA,
 vertex.shape="none",
 vertex.size=0,
 edge.arrow.mode=0,
 edge.width=2) 

As the plot shows, the red edge becomes the toppest one: 新图的图:test2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM