Erdogan CEVHER - 8 months ago 42

R Question

How to draw a directed network graph with texts on both sides of edges in R (via iGraph, network or some other package)?

Variables (in vector autoregressive model (VAR)) are in nodes.

The Granger causalities (p values of the F statistics) are on both sides of edges. I put p values to the beginning of each arrow. I draw near-significant causalities with added emphasize.

I could not figure out how to get such a network diagram via iGraph or network package or something else.

Answer

You can use the `edge.label`

and `vertex.label`

options together.

```
el <- data.frame(sender = c("lnbist1f","lnbist1f","lnbist1f",
"kur1f","kur1f","kur1f",
"lnaltin","lnaltin","lnaltin",
"mfaiz1f","mfaiz1f","mfaiz1f"),
receiver = c("mfaiz1f","lnaltin","kur1f",
"lnbist1f","lnaltin","mfaiz1f",
"mfaiz1f","lnbist1f","kur1f",
"lnbist1f","lnaltin","kur1f"),
pval = c(0.5,0.6,0.1, #I just typed random p-vals here
0.45,0.88,0.24,
0.12,0.51,0.99,
0.001,0.056,0.123)
)
arrows = c(2,1,0,0,1,1,1,0,0,2,1,1)
el <- as.matrix(el)
g <- graph_from_edgelist(el[,1:2], directed = T)
coordinates <- matrix(c(4, 4, 1, 1, 4, -2,7,1), nrow = 4, byrow=TRUE)
plot(g, edge.label=el[,3],
vertex.shape="crectangle",
vertex.size=45,
edge.arrow.mode=arrows,
layout = coordinates)
```

The `edge.arrow.mode`

lets you control the arrowheads. You can move the edge labels around with `edge.label.y`

and `edge.label.x`

options.

Source (Stackoverflow)