2015-10-30 53 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