始终将某些节点的位置设置在环形图的顶部 [英] Set the position of certain node always on top of ring graph
问题描述
我在下面有 nodes
和 edges
数据框,并用它们创建了一个圆形图.我想实现的是使用 x
和 y
坐标,以使 a
节点始终位于图的顶部总节点数,如下面的 Roger Rabit
节点.我给的 x
和 y
位置现在是randon,但理想情况下,我想创建一个仅设置了 a
坐标的环形图.
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)
推荐答案
假定位于顶部的节点是 nodes
中的第一个节点,请使用layout_in_circle获取布局,然后旋转其行直到最大y位于最上方,将其用作最终布局.
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)))
这篇关于始终将某些节点的位置设置在环形图的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!