问题
The result of some process is a list of paths from A to C through B, e.g.:
which.effect(A1,A2,10,1,1)
[[1]]
[1] 10 2 1
[[2]]
[1] 10 28 1
[[3]]
[1] 10 6 9
[[4]]
[1] 10 24 9
[[5]]
[1] 10 28 9
What I would like to have is a graph with three parallel columns, the first for the origin, the second for the intermediate point, and the third for the destination. In this example, the first column would have only the node 10, the second 2, 6, 24, 28 and the third 1, 9. Then, directed edges (arrows) will go from nodes in the first column to nodes in the second column, and from nodes in the second column to nodes in the third one.
Is this even possible with igraph?
Thanks in advance.
回答1:
Not exactly sure if this is what you want but maybe gets you some of the way there.
The idea is to first form an edge list from your data, to then create an adjacency matrix and then plot.
library(igraph)
library(Rgraphviz)
# your data
lst <- list(c(10,2,1), c(10,28,1), c(10,6,9), c(10,24,9), c(10,28,9))
# create edge list (from in first column / to in the second)
d <- do.call(rbind, lst)
edges <- rbind(d[,1:2], d[,2:3])
# get adjacency matrix
g <- graph.data.frame(edges, directed=TRUE)
adj <- as.matrix(get.adjacency(g))
# convert to a graph object and plot
g2 <- new("graphAM", adjMat=adj, edgemode="directed")
plot(g2, attrs = list(graph = list(rankdir="LR"),
node = list(fillcolor = "lightblue")))
rankdir="LR" plots the graph from left to right
Above plot uses dot to give the strict structure.
EDIT
Use layout = layout.reingold.tilford to get a tree structure using igraph
E(g)$curved <- 0
plot.igraph(g, vertex.size=30, layout=layout.reingold.tilford)
来源:https://stackoverflow.com/questions/24441094/three-column-graph