1

Suppose we have an adjacency matrix, maybe something like this:

  A1 A2 A3
A1 0  1  0
A2 1  0  2
A3 0  2  0

Each number reflects the number of ties between people (people being A1-A3).

How would you use igraph to create edge weights which reflect the number of ties between people?

I tried E(matrix)$weight <- degree(matrix) but I suspect this isn't technically correct? Instead I just want the values in the adjacency matrix to reflect the edge weight.

2 Answers2

1

You weren't really clear on how you wanted the weight depicted. This lists the weights by the node on the edges.

g1 <- graph_from_adjacency_matrix(
  adjmatrix = matrix(c(0,1,0, 1,0,2, 0,2,0), 
                     nrow = 3, ncol = 3, byrow = T, 
                     dimnames = list(c("A1","A2","A3"), ("A1","A2","A3"))
                     ) # end matrix
  ) # end graph_from

plot(g1, 
     edge.label = E(g1)$weight)    # plot with edge weights

enter image description here

Kat
  • 15,669
  • 3
  • 18
  • 51
1

You can try to enable weighted = TRUE in graph_from_adjacency_matrix, e.g.,

g <- graph_from_adjacency_matrix(adjmat, "undirected", weighted = TRUE)
plot(g, edge.width = E(g)$weight, edge.label = E(g)$weight)

which gives

enter image description here

Data

> dput(adjmat)
structure(c(0, 1, 0, 1, 0, 2, 0, 2, 0), .Dim = c(3L, 3L), .Dimnames = list(
    c("A1", "A2", "A3"), c("A1", "A2", "A3")))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81