2017-08-15 65 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 

正常工作:

我建议在本教程的源代码如下五个转变。