2
votes

I want to plot a directed weighted network in which I could show stock transactions between participants. I'm not sure if we can add a 3rd columne ("TYPE") in a edgelist in the igraph R package. If so, can we differentiate the edges based on the purchased stock? e.g. Apple : blue edge, google : red edge, Pfizer: orange edge.

Edgelist

FROM TO VALUE TYPE
A    B  100   Apple
B    C  50    Pfizer
C    D  80    Apple
A    E  70    Google

    require(igraph)
test<-as.matrix(edge_list_test)
test[,1]<-as.character(test[,1])
test[,2]<-as.character(test[,2])
test[,4]<-as.character(test[,4])
g<-graph.edgelist(test[,1:2], directed=TRUE)
V(g)$size=degree(g)
E(g)$type=as.character(test[,4])
E(g)$weight=as.numeric(test[,3])
    plot(g, layout=layout.fruchterman.reingold, edge.width=E(g)$weight, vertex.color=V(g)$type, vertex.label.dist=0.5)][1]][1]

Why are the edges gray and can't see different colors on the edges based on the "TYPE"?

enter image description here

1

1 Answers

1
votes

When I run your code as provided, I get ridiculously big arrows so I will adjust edge.width. Nothing in your plot statement sets the edge color. You need to do that explicitly with the edge.color parameter.

CR = rainbow(length(unique(edge_list_test$TYPE)))
plot(g, layout=layout.fruchterman.reingold, 
    edge.width=E(g)$weight/15, vertex.color=V(g)$type, 
    vertex.label.dist=0.5, edge.color=CR[edge_list_test$TYPE])
legend("topleft", legend=levels(edge_list_test$TYPE), lwd=2, col=CR)

Colored Arrows

I used edge_list_test$TYPE to select the edge color instead of E(g)$type because edge_list_test$TYPE is a factor and E(g)$type is just a string.