igraph顶点属性graph_from_data_frame最佳做法

fae0ux8s  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(174)

我正在使用R中的igraph与一个巨大的网络,我有点害怕搞砸了df。我关注了Vertex/node attributes for igraph objects并阅读了R-igraph教程和文档。
然而,我错过了一些东西,假设我有这些数据:

toy_data = data.table(source = c(1,1,1,3,5,5,1,1,1,3,5,5), 
                      source_name=c(Milan,Milan,Milan,Frankfurt,London,London,Milan,Milan,Milan,Frankfurt,London,London), 
                      from=c("A","A","A","C","E","E","A","A","A","C","E","E"),
                      target=c(2,3,1,4,6,5,5,1,1,1,3,NA), target_name=c(Paris,3,1,4,6,5,5,1,1,1,3,NA),
                      to=c("B","C","A","D","F","E","E","A","A","A","C",NA))
edges <- toy_data[,.(source,target)]
v <- data.frame(labels=as.character(unique(unlist(toy_data[,.(source,target)]))),
                names = as.character(unique(unlist(toy_data[,.(source_name,target_name)]))),
                category = as.character(unique(unlist(toy_data[,.(from,to)]))))
graph <- graph_from_data_frame(edges, vertices = v, directed = FALSE)
plot(graph,vertex.label=v$names,vertex.color=c("pink","skyblue")[1+(V(graph)$category=="A")])

只要未列出的“唯一”向量具有相同的长度,所有这些都很好,但对我来说,将顶点属性单独加载为单独的列似乎不是一个很好的做法,因为有一个重复就足够了(这里的from和to字段具有“A”而不是“C”),这些向量不再大小相等:

toy_data = data.table(source = c(1,1,1,3,5,5,1,1,1,3,5,5), 
           source_name= c("Milan","Milan","Milan","Frankfurt","London","London","Milan","Milan","Milan","Frankfurt","London","London"), 
           from=c("A","A","A","A","E","E","A","A","A","A","E","E"), 
           target=c(2,3,1,4,6,5,5,1,1,1,3,NA), 
           target_name=c("Paris","Frankfurt","Milan","Dublin","Madrid","London","London","Milan","Milan","Milan","Frankfurt",NA),
           to=c("B","A","A","D","F","E","E","A","A","A","A",NA))
toy_data
edges <- toy_data[,.(source,target)]
v <- data.frame(labels=as.character(unique(unlist(toy_data[,.(source,target)]))),
                names = as.character(unique(unlist(toy_data[,.(source_name,target_name)]))),
                category = as.character(unique(unlist(toy_data[,.(from,to)]))))
graph <- graph_from_data_frame(edges, vertices = v, directed = FALSE)
plot(graph,vertex.label=v$names,vertex.color=c("pink","skyblue")[1+(V(graph)$category=="A")])

因此,如果我有一个以这种方式构造的data.table,我如何告诉igraph将节点id绑定到某些特性?python中的zip()函数)

8cdiaqws

8cdiaqws1#

提醒一下,我自己还在试着弄明白igraph。但是,看起来Source和Target字段可以重命名为公共属性。与创建边表类似,可以创建具有唯一ID和属性的顶点表。然后可以在图上使用set_vertex_attr
另外,注意NA被列为节点,并且没有被处理。考虑处理,否则igraph将抛出错误。

library(igraph)
library(data.table)

# using toy_data input from post
edges <- toy_data[,.(source,target)]

#setnames to rename the source and target columns to common attribute names
#rbind to union together
#unique to get unique nodes with attributes
v <- unique(rbind(setnames(toy_data[,.(source,source_name,from)],c("labels", "names", "category")), 
      setnames(toy_data[,.(target,target_name,to)], c("labels", "names", "category"))))

#can now add features to the data table
v[,color:= ifelse(category == "A", "pink", "skyblue")]

graph <- graph_from_data_frame(edges, vertices = v, directed = FALSE)

#set_[vertex|edge]_attr to set node and edge attributes with columns
graph <- set_vertex_attr(graph, "label", value = v$names) 
graph <- set_vertex_attr(graph, "color", value = v$color) 

plot(graph)

相关问题