您收到重复的顶点 ID 错误,因为您需要引用 vertices = 中的唯一节点数据。您可以使用unique(nodes),但这会给您带来另一个错误,因为您在邻接列表数据中引用的节点1 和2 不包含在您的nodes 数据中。
您的节点数据不能只包含来自edges$from 列的唯一值,它必须包括来自edges$from 和edges$to 的所有唯一值,因为您将adjacency list 数据传递给graph_from_data_frame() 函数。
所以在edges$to 中,您还需要按名称引用顶点,如edges$from 中那样,例如12341 或 23435。
这里有一些 R 代码,可能包括您想要实现的目标。
#graph from your data frame
MANAGER_LOC <- graph_from_data_frame(
d = edges
,vertices = unique(c(edges$from, edges$to))
,directed = TRUE);
#plot also includes vertices 1 and 2
plot(
x = MANAGER_LOC
,main = "Plot from your edges data");
#plot from your data assuming you are referencing an id in edges$to
MANAGER_LOC <- graph_from_data_frame(
d = merge(
x = edges
,y = data.frame(
to_vertice_id = 1:length(unique(edges$from))
,to_vertice = unique(edges$from))
,by.x = "to"
,by.y = "to_vertice_id"
,all.x = T)[,c("from","to_vertice","weight")]
,vertices = unique(edges$from)
,directed = TRUE);
#plot does not include vertices 1 and 2
plot(
x = MANAGER_LOC
,main = "Plot assuming vertice ID
reference in edges$to");
#plot from your data assuming you are referencing the xth value of edges$from in edges$to
MANAGER_LOC <- graph_from_data_frame(
d = merge(
x = edges
,y = data.frame(
to_vertice_ref = 1:nrow(edges)
,to_vertice = edges$from)
,by.x = "to"
,by.y = "to_vertice_ref"
,all.x = T)[,c("from","to_vertice","weight")]
,vertices = unique(edges$from)
,directed = TRUE);
#plot does not include vertices 1 and 2
plot(
x = MANAGER_LOC
,main = "Plot assuming edges$from
reference in edges$to");