简体   繁体   中英

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

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.

Reading the documentation on the igraph plot function it seems that there is not a lot of options to manipulate edges labels but you can manipulate vertex labels with label.dist and label.degree for example.. You can do something like that:

g <- graph.empty(n = 3) 
g <- graph(c(1, 2, 3, 2, 1, 3), directed = TRUE)
labels <- c("A", "B", "C", "D", "E", "F")
coords <- matrix(c(1, 1, 2, 2, 3, 3), nrow = 3)
plot(g, layout = coords, vertex.label = labels, vertex.label.dist = 3.5, vertex.label.degree = c(-pi/4, pi/2, pi, -pi/2, 0, 3*pi/4))

Graph

You can use the edge.label and vertex.label options together.

library(igraph)
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.

图形图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM