-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphVizLayout.R
35 lines (33 loc) · 1.02 KB
/
graphVizLayout.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#' Create graphViz layout from graph
#'
#' Creates a matrix containing the graphViz layout for an igraph
#' object.
#'
#' @param g The graph to process.
#' @param mode The rank direction passed to the \link[Rgraphviz]{layoutGraph} function.
#'
#' @return A matrix with two columns containing the graph layout.
#'
#' @author Martin Garrido-Rodriguez, \email{[email protected]}
#'
#' @importFrom graph graphNEL
#' @importFrom Rgraphviz layoutGraph
#' @importFrom igraph get.data.frame
#'
graphVizLayout <- function(g, mode = "LR") {
# get simple graph format
simple <- igraph::get.data.frame(g)[,1:2]
# create graphNEL
rEG <- new("graphNEL", nodes = V(g)$name, edgemode = "directed")
for(i in 1:nrow(simple)){
rEG <- graph::addEdge(simple[i,1], simple[i,2], rEG, 1)
}
# set graph atts
att <- list(graph = list(rankdir = mode))
# render layout
rEG <- Rgraphviz::layoutGraph(rEG, attrs = att)
# get matrix with X and Y
l <- cbind(rEG@renderInfo@nodes$nodeX, y = rEG@renderInfo@nodes$nodeY)
colnames(l) <- c("x","y")
return(l)
}