在igraph中导入顶点属性R
问题描述:
我是R新手,很抱歉这样简单的问题,但我真的不知道我的问题在哪里... 我正在尝试构建网络:在igraph中导入顶点属性R
library(igraph)
matrix_try <- read.csv2("~/Documents/RStudio/Cedges.csv", header = T , row.names = 1)
nodes <- read.csv2("~/Documents/RStudio/Cnode.csv", header = TRUE)
文件,你可以找到here
matrix_try <- as.matrix(matrix_try)
net <- graph_from_adjacency_matrix(matrix_try, nodes, mode = "undirected", weighted = T)
但是没有顶点属性(类型,*):
IGRAPH UNW- 28 48 --
+ attr: name (v/c), weight (e/n)
+ edges (vertex names):
[1] BYT --Udar BYT --Front.zmin
[3] BYT --Svoboda (...)
如何'找到'他们?
预先感谢您!
答
您不能使用?graph_from_adjacency_matrix
添加节点属性,它们尚未添加,因此无法找到它们。
下载文件:
adj_mat <- read.csv("Cedges.csv", sep =";", row.names = 1)
nodes <- read.csv("Cnode.csv", sep =";")
net <- igraph::graph_from_adjacency_matrix(as.matrix(edges), mode = "undirected", weighted = T)
然后,您可以使用内置的FUN set_vertex_attr
像这样
set_vertex_attr(net, "name", index = V(net), as.character(nodes$name))
set_vertex_attr(net, "protests", index = V(net), nodes$protests)
set_vertex_attr(net, "type", index = V(net), as.factor(nodes$type))
在情节使用属性
plot(net, vertex.color = V(net)$protests)