Iulia Dukach
Iulia Dukach

Reputation: 23

Import of vertices attributes in igraph in R

I'm new to R, so sorry for such simple question, but I really don't know where is my problem... I am trying to build a network:

library(igraph)
matrix_try <- read.csv2("~/Documents/RStudio/Cedges.csv", header = T , row.names = 1)
nodes <- read.csv2("~/Documents/RStudio/Cnode.csv", header = TRUE)

Files you can find here

matrix_try <- as.matrix(matrix_try)
net <- graph_from_adjacency_matrix(matrix_try, nodes, mode = "undirected", weighted = T)

But there are no vertices attributes (type, protests):

 IGRAPH UNW- 28 48 -- 
+ attr: name (v/c), weight (e/n)
+ edges (vertex names):
     [1] BYT --Udar                                   BYT --Front.zmin                            
     [3] BYT --Svoboda      (...)

How to 'find' them??

Thank you in advance!

Upvotes: 2

Views: 6627

Answers (1)

JohnCoene
JohnCoene

Reputation: 2261

You cannot add node attributes using ?graph_from_adjacency_matrix, they have not been added, hence not being able to find them.

Downloaded your files:

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)

You can then use the built-in FUN set_vertex_attr like so

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))

Use the attributes in plot

plot(net, vertex.color = V(net)$protests)

net plot

Upvotes: 3

Related Questions