MatAff
MatAff

Reputation: 1331

R igraph Bipartite Graph, why are my weights not showing?

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)

Upvotes: 3

Views: 1267

Answers (1)

Sandipan Dey
Sandipan Dey

Reputation: 23101

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

Upvotes: 3

Related Questions