2017-08-15 87 views
1

我試圖在我自己的data.frames中使用example code here for doing iGraph network graphs in plotly和shoehorn,而不是使用示例空手道俱樂部數據。當繪製圖形時,似乎忽略了邊緣列表,並且正在建立一堆隨機連接。我認爲無論標籤還是邊緣都是錯誤的,但我無法分辨。iGraph + Plotly創建隨機連接

library(igraph) 
library(plotly) 

setwd('C:/Users/Andrew Riffle/Documents/MEGAsync/code/R/link_analysis') 

ID <- c(1:50) 
nodes <- data.frame(ID) 

Source <- c(23, 24, 36, 20, 36, 41, 12, 8, 18, 28) 
Target <- c(5, 7, 9, 35, 23, 12, 38, 29, 33, 45) 
links <- data.frame(Source, Target) 

net <- graph_from_data_frame(d=links, vertices=nodes, directed=FALSE) 

net <- simplify(net, remove.multiple = F, remove.loops = T) 

tkplot(net, vertex.label=nodes$id, vertex.label.color='white', layout=layout.fruchterman.reingold) 

#####Begin plotly example code unmodified unless commented##### 

G <- upgrade_graph(net) #put my iGraph object instead of the karate club one 
L <- layout.circle(G) 

vs <- V(G) 
es <- as.data.frame(get.edgelist(G)) 

Nv <- length(vs) 
Ne <- length(es[1]$V1) 

Xn <- L[,1] 
Yn <- L[,2] 

network <- plot_ly(x = ~Xn, y = ~Yn, mode = "markers", text = vs$name, hoverinfo = "text") 

edge_shapes <- list() 
for(i in 1:Ne) { 
    v0 <- es[i,]$V1 
    v1 <- es[i,]$V2 

    edge_shape = list(
    type = "line", 
    line = list(color = "#030303", width = 0.3), 
    x0 = Xn[v0], 
    y0 = Yn[v0], 
    x1 = Xn[v1], 
    y1 = Yn[v1] 
) 

    edge_shapes[[i]] <- edge_shape 
} 

axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE) 

p <- layout(
    network, 
    title = 'Test Network', #Changed the title 
    shapes = edge_shapes, 
    xaxis = axis, 
    yaxis = axis 
) 

p #added a call to display p instead of upload to plot.ly 

當我運行這個,我得到this nice pretty iGraph that has been plotted by Plotly。但是,邊緣不正確。看起來只有ID的1-10連接,並且只有其他ID小於10.這些連接都不在邊緣列表中,如下所示。

Source Target 
1  24  35 
2  12  23 
3  41  12 
4  23  7 
5  18  5 
6  20  9 
7  28  29 
8  36  45 
9  8  33 
10  36  38 

有沒有人看到我在做什麼錯了?幫助讚賞。

回答

1

發現該教程實際上是錯誤的。如果使用plot繪製相同的空手道網絡與使用plotly進行比較,則plot中有許多連接不在plotly中。

我已經放棄了嘗試做這項工作,visNetwork是一個很好的選擇,我已經有一個更容易的時間搞清楚。建議任何有類似問題的人閱讀本文。

+0

感謝visNetwork的建議,作品像一個魅力。與情節化的例子有類似的問題,在教程中的這一部分是有問題的。 'x0 = Xn [v0], y0 = Yn [v0]'它試圖在數值向量中查找一個頂點名稱,這會產生一些奇怪的結果。 – DMU

0

我知道這對OP來說有點太晚了,但對於那些偶然發現同樣問題的人來說(我也是如此)。現在

G <- upgrade_graph(net) 
L <- layout.circle(G) 
rownames(L) <- get.vertex.attribute(G)$name   #added line (#1 out of 5) 

vs <- V(G) 
es <- as.data.frame(get.edgelist(G)) 

Nv <- length(vs) 
Ne <- length(es[1]$V1) 

Xn <- L[,1] 
Yn <- L[,2] 

network <- plot_ly(x = ~Xn, y = ~Yn, mode = "markers", text = vs$name, hoverinfo = "text") 

edge_shapes <- list() 
for(i in 1:Ne) { 
    v0 <- es[i,]$V1 
    v1 <- es[i,]$V2 

    edge_shape = list(
    type = "line", 
    line = list(color = "#030303", width = 0.3), 
    x0 = L[which(v0==rownames(L)),][1],    #changed line (#2 out of 5)     
    y0 = L[which(v0==rownames(L)),][2],    #changed line (#3 out of 5) 
    x1 = L[which(v1==rownames(L)),][1],    #changed line (#4 out of 5) 
    y1 = L[which(v1==rownames(L)),][2]    #changed line (#5 out of 5) 
) 

    edge_shapes[[i]] <- edge_shape 
} 

axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE) 

p <- layout(
    network, 
    title = 'Test Network', #Changed the title 
    shapes = edge_shapes, 
    xaxis = axis, 
    yaxis = axis 
) 

p 

正常工作:

我建議在本教程的源代碼如下五個轉變。