Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding edge weights from dataframe with igraph in R

Tags:

r

igraph

I created a graph object

ig <- graph(df$ig)

And would like to be able to plot the graph with weighted edges according to the network matrix

ig[]

The ig[] is not weighted, how do I add the weights directly from the network matrix so I can access it with E(ig)$weight?

like image 276
Bipa Avatar asked Oct 19 '25 03:10

Bipa


1 Answers

You can weight to your graph like this:

Example: gg3 <- graph.ring(10)

 [1,] . 1 . . . . . . . 1
 [2,] 1 . 1 . . . . . . .
 [3,] . 1 . 1 . . . . . .
 [4,] . . 1 . 1 . . . . .
 [5,] . . . 1 . 1 . . . .
 [6,] . . . . 1 . 1 . . .
 [7,] . . . . . 1 . 1 . .
 [8,] . . . . . . 1 . 1 .
 [9,] . . . . . . . 1 . 1
[10,] 1 . . . . . . . 1 .

E(gg3)$weight <- 15

 [1,]  . 15  .  .  .  .  .  .  . 15
 [2,] 15  . 15  .  .  .  .  .  .  .
 [3,]  . 15  . 15  .  .  .  .  .  .
 [4,]  .  . 15  . 15  .  .  .  .  .
 [5,]  .  .  . 15  . 15  .  .  .  .
 [6,]  .  .  .  . 15  . 15  .  .  .
 [7,]  .  .  .  .  . 15  . 15  .  .
 [8,]  .  .  .  .  .  . 15  . 15  .
 [9,]  .  .  .  .  .  .  . 15  . 15
[10,] 15  .  .  .  .  .  .  . 15  .

or

E(gg3)$weight <- c(20,10)

 [1,]  . 20  .  .  .  .  .  .  . 10
 [2,] 20  . 10  .  .  .  .  .  .  .
 [3,]  . 10  . 20  .  .  .  .  .  .
 [4,]  .  . 20  . 10  .  .  .  .  .
 [5,]  .  .  . 10  . 20  .  .  .  .
 [6,]  .  .  .  . 20  . 10  .  .  .
 [7,]  .  .  .  .  . 10  . 20  .  .
 [8,]  .  .  .  .  .  . 20  . 10  .
 [9,]  .  .  .  .  .  .  . 10  . 20
[10,] 10  .  .  .  .  .  .  . 20  .

Check this out: Using edge-lists with associated edge values to create a weighted network.

like image 124
Panos Kal. Avatar answered Oct 21 '25 18:10

Panos Kal.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!