4

I would like to express the edges between vertices 1 and 2 in the following graph as two separate edges with arrows in opposite directions.

require(igraph) 
e <-  c(1,2, 2,3, 3,1, 3,4 , 4,1 , 2,1)
g <- graph(e, n=5, directed = TRUE)
tkplot(g, layout=layout.circle )

What I get is just one edge with an arrow on both ends. It should be easy but I haven't found it anywhere and can't figure it out. Any idea?

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    I could only think about `edge.curved=rep(0.5, ecount(g))` which makes curved edges but I would like them straight. – CaffeRistretto Feb 11 '16 at 18:08
  • I think igraph support multple edges between nodes--maybe try to create two directional edges in opposite directions? – Ott Toomet Feb 11 '16 at 18:36
  • Directional edge? in `e <- c(1,2, 2,3, 3,1, 3,4 , 4,1 , 2,1) ` there is edge from 1 to 2 (first two) and also from 2 to 1 (last two). Or what do you mean by directional edges? – CaffeRistretto Feb 11 '16 at 19:42
  • Yes, this is what I mean. But I haven't tried it myself... – Ott Toomet Feb 11 '16 at 20:15
  • 1
    That's two separate edges. igraph just happens to draw them on top of each other, so you see them as a single edge with two arrowheads. Unfortunately there's no way in igraph to draw them as two *straight* edges, so your best bet is still `edge.curved`. You can set it to 0.0 for "single" edges and 0.5 for edges that exist in both directions. – Tamás Feb 11 '16 at 22:58
  • Thank you. I did exactly what you are saying and it looks ok even if straight lines would be nicer for me :) Do you know if there is a way to avoid overlapping of the labels of edges? If there are two edges then they are placed one over the other which looks very bad and it is impossible to read them. – CaffeRistretto Feb 12 '16 at 15:46

1 Answers1

1

You should plot the graph with curved edges. Note that in plot(), you curve with edge.curved, but edge-attributes to individually curve edges should be curved only.

The below function curve.reciprocal.edges() I wrote after finding this answer by Sacha Epskamp who apparently has written a package, qgraph, which you might find useful to explore if your needs exceed what can be achieved by this:

require(igraph)
e <-  c(1,2, 2,3, 3,1, 3,4 , 4,1 , 2,1)
g <- graph(e, n=5, directed = TRUE)

curve.reciprocal.edges <- function(g, curve=.3){
    # Return a graph where the edge-attribute $curved is reset to highlight reciprocal edges
    el <- t(apply(get.edgelist(g),1,sort))
    E(g)$curved <- 0
    E(g)[duplicated(el) | duplicated(el,fromLast =TRUE)]$curved <- curve
    (g)
}

plot(g, layout=layout.circle, edge.curved=.2)
plot(curve.reciprocal.edges(g), layout=layout.circle)
nJGL
  • 819
  • 5
  • 17