如何使用预定义的顶点ID创建igraph?

时间:2019-01-15 03:36:05

标签: r igraph

我有顶点的id's(x,y)坐标。边缘列表el也是已知的。我需要创建一个空间图。

我的尝试在下面:

library(igraph)

id <- c(106, 110, 107, 102, 104, 103, 108, 111, 109, 101)

        cx <- c(270.2798, 156.4124, 270.2798, 100.0332, 156.4124, 100.0332, 156.4124, 156.4124, 156.4124, 100.0035)

cy <- c(401.2903, 420.2904, 470.4120, 401.2903, 348.2904, 348.2904, 470.4120, 401.2903, 436.8926, 470.1633)

el <- c(101,102, 102,103, 103,104, 104,111, 111,106,
                106,107, 107,108, 108,101, 108,109, 110,111)

dfw <- data.frame(id, cx, cy)

g2 <- make_empty_graph() %>%
add_vertices(nrow(dfw)) %>%
add_edges(el) # error is here

V(g2)$names <- dfw$id

mylayout2 <- as.matrix(cbind(dfw$cx, -dfw$cy))

plot(g2, layout=mylayout2,
               vertex.size = 10,
               vertex.label = V(g2)$name,  
               vertex.label.cex=.95, 
         )

我有错误

Error in add_edges(., el) : 
  At type_indexededgelist.c:272 : cannot add edges, Invalid vertex id

预期结果是:

enter image description here

问题。如何添加具有预定义的id's个顶点的边?

1 个答案:

答案 0 :(得分:1)

首先,要将101、102等转换为socket.on('vote', (ballet) => { console.log(messageList[ballet.id].message, messageList[ballet.id].rank); messageList[ballet.id].rank += ballet.vote; console.log(messageList[ballet.id].message, messageList[ballet.id].rank); io.to('main room').emit('new-message', messageList[ballet.id]); }); 的行号,我们可以使用dfw。其次,应该是

match

代替

V(g2)$name <- dfw$id

所以,我们有

V(g2)$names <- dfw$id

enter image description here