I have the nodes
and edges
dataframes below and I create a circle graph out of them. What I would like to achieve is to use x
and y
coordinates in a way that always the a
node will be on top of the graph regardless of the number of total nodes like the Roger Rabit
node below. The x
and y
positions I gave is randon right now but ideally I would like to create the ring graph with only the coordinates of a
set.
library('igraph')
nodes <- c('a','b','c','d')
x <- c(0,1,2,3)
y <- c(0,1,2,3)
from <- c('a','b','c','d')
to <- c('b','c','d','a')
NodeList <- data.frame(nodes, x ,y)
EdgeList <- data.frame(from, to)
a<- graph_from_data_frame(vertices = NodeList, d= EdgeList, directed = TRUE)
plot(a)
Assuming that the node to be at the top is the first node in nodes
use layout_in_circle to get a layout and then rotate its rows until the maximum
y is at top using that as the final layout.
# rotate rows of matrix mat so that row number mx is at top
# where mx defaults to row having largest value in 2nd column
rot <- function(mat, mx = which.max(mat[, 2])) {
if (mx == 1) mat else mat[c(mx:nrow(mat), 1:(mx-1)), ]
}
plot(a, layout = rot(layout_in_circle(a)))