Question

I have a standard edgelist:

[1]    1 ->  2
[2]    1 ->  3
[3]    1 ->  6
[4]    2 ->  1
[5]    2 ->  3
[6]    2 ->  4
[7]    2 ->  5

I would like to colour the edges according to three rules:

i) if the first value is less than the second -> blue

ii) if the first value is greater than the second ->red

This seems to be quite straightforward:

y.ed<-as.edgelist(am.ed)
z<-as.data.frame(y.ed)
m<-z[,1]
n<-z[,2]
o<-ifelse(m<n, "red", "blue")

However I would like to iii) colour reciprocal ties i.e. 1->2, 2->1 (as above) a further colour e.g. orange. I cannot work out how to do this.

Was it helpful?

Solution

After thinking about this a little, it occurred to me that this could be tricky and that someone had probably already done this, since networks are an area of pretty heavy application in R. In package igraph I found an approach that will work if you can get your edge list reformatted slightly. Here is an example using made up data:

library("igraph")
set.seed(11)
ed <- sample(1:10, 30, replace = TRUE)
g <- graph(edges = ed)
str(g)
plot(g)
m <- is.mutual(g)

The T/F vector in m can then be used to color your edges as desired.

Note that in this example an edge starting and ending on the same node/vertex and duplicated edges are counted as "mutual" by the function. If you have those as well as reciprocal edges you might have to do some additional fiddling. This example has one reciprocal edge (1 <-> 3).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top