2015-10-30 52 views
3

我试图在igraph中制作一个网络图,通过对它们进行着色来突出显示某些重要边缘。对于大图,他们经常被埋在其他人之下。例如:如何重新排列igraph图中边缘的顺序?

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) 

给我一个情节,其中单个红色边缘在底部。 this plot makes me sad 如果我选择颜色较高的边缘(而不是#1),它有更好的机会不被埋葬。

所以在我看来,一种选择是以某种方式重新排序边缘。我试过

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

但这样会导致“无效索引”错误。关于我还应该尝试什么的任何想法?

回答

3

igraph按照它们在图形边缘列表中出现的顺序绘制边缘,所以您是正确的,具有较高ID的边缘将绘制在具有较低ID的边缘的顶部。不幸的是,igraph没有提供简单的方法来重新排列图的边缘(尽管它有一个名为permute.vertices的函数,它可以让你排列顶点),所以现在我唯一能想到的方法就是你需要构造另一个图形,其边缘处于“正确的顺序”。 make_graph确保边缘按照您指定的顺序精确地存储在图中,我认为graph_from_data_frame也是如此。

另一种选择(如果你不想重建整个图)是绘制两次图:首先绘制“不太重要”的边,并将重要的边的宽度设置为零,然后你在上面绘制重要的边缘。

如果您想在即将推出的igraph版本中支持边界排列,请在Github上使用file a feature request

+0

谢谢,陶!绘图 - 两次解决方案对我来说最合适。我使用'set.seed()'来确保我的布局是可重现的,在绘制一次后,我使用'E(test)[E(test)$ color ==“gray”] $ color < - NA'确保灰色边缘被隐藏起来,并且在第二次调用plot()时加入“add = TRUE”。 – cjolley

+0

您也可以显式调用布局函数并将其结果存储在变量中 - 然后您可以稍后在'plot()'的'layout = ...'参数中传递此变量。 –

+0

您也可以生成一次布局,然后将其存储到图形中。如果您设置了g $布局属性,该图将自动使用存储在那里的布局。在这种情况下,您可以将布局存储到图形中,然后将新图形布局设置为与旧图形布局相同。 – wmsmith

0

或者你可以简单地重新安排基础上要进行排序,然后喂它IGRAPH

1

可以重建,边缘曲线图中的可变数据使用“as_data_frame”和“graph_from_data_frame”重新排序很容易在与dplyr和tidyr套餐搭配:

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")) 

,如果你已经存储在旧图的布局,你需要手动将其传送...

new_graph$layout = old_graph$layout 
0

我认为E(test) <- E(test)[order(E(test)$color)]没有解决问题,因为我们不能在当前版本的igraph软件包中为其他现有的/ igraph.es变量赋值;不过,我们可以使用a <- E(test)[order(E(test)$color)]创建一个新的“igraph.es”varibale。因此,我建议做一个新的图形继承边缘从原单图一个新秩序:

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) 

随着剧情显示,红边变成顶部: plot of the new graph:test2