3

I am using R and the package igraph to create a bipartite graph based on an incidence matrix, but my weights are not showing? I’ve added an example of what I’m trying to do below. I’ve set weighted=TRUE, and would expect the edges to have different weights, but the lines are all the same thickness. Any suggestions as to what I'm doing wrong?

# Load packages
library(igraph)

# Create data
pNames <- paste("P", 1:4, sep="")
cNames <- paste("c", 1:3, sep="")
rData <- matrix(sample(4,12,replace=TRUE)-1,nrow=4,dimnames=list(pNames,cNames))
print(rData)

# Graph from matrix
b <- graph_from_incidence_matrix(rData,weighted=TRUE)

# Plot with layout
plot(b, layout=layout.bipartite,vertex.color=c("green","cyan")[V(b)$type+1],edge.width = b$weights)
thelatemail
  • 91,185
  • 12
  • 128
  • 188
MatAff
  • 1,250
  • 14
  • 26
  • 2
    You just need to call weights in the plot call using `edge.width = E(b)$weights`. `igraph` requires you to specify edges, `E(graph)`, or vertices, `V(graph)`, when calling attributes. – paqmo Feb 22 '17 at 00:29

1 Answers1

3

You can find the attributes of the edges using

get.edge.attribute(b)
#$weight
#[1] 2 1 1 3 2 1 2

As @paqmo mentioned, now you know the name of the attribute and you can use it to set the edge widths / labels:

plot(b, layout=layout.bipartite,vertex.color=c("green","cyan")[V(b)$type+1],
     edge.width = E(b)$weight, edge.label=E(b)$weight, edge.label.cex=2)

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63