r - iGraph + Plotly 创建随机连接

标签 r plotly igraph network-analysis

我正在尝试使用 example code here for doing iGraph network graphs in plotly和鞋拔子在我自己的 data.frames 中,而不是使用示例空手道俱乐部数据。当绘制图表时,它似乎忽略了边列表,并且正在建立一堆随机连接。我认为标签或边缘错误,但我无法判断。

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 正在连接,并且只连接到小于 10 的其他 ID。这些连接都不在下面的边缘列表中。

   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

有人看到我做错了什么吗?感谢帮助。

最佳答案

我知道这对于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

现在工作正常。

关于r - iGraph + Plotly 创建随机连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45701184/

相关文章:

python - 图形解析错误

r - 如何访问属性[例如R 中 terra 栅格的时间]?

javascript - 绘制一条垂直线来显示平均值

javascript - Plotly.js 图没有响应

javascript - 使用 Plotly.js 动态创建/删除/重新设计图形的最佳方式?

igraph - 社区检测算法中权重的含义

r - 使用 "same"幂律度分布创建随机图

r - 在双边案例交叉设计中创建控制日期

r - 将数字向量拆分为 R 中的连续 block

通过全局变量选择R数据框