微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

设置某个节点的位置总是在环图的顶部

如何解决设置某个节点的位置总是在环图的顶部

我在下面有 nodesedges 数据框,我用它们创建了一个圆图。我想要实现的是使用 xy 坐标,无论 a 节点的总数如何,Roger Rabit 节点始终位于图的顶部{1}} 节点。我现在给出的 xy 位置是 randon,但理想情况下,我想创建仅包含 a 坐标集的环形图。

library('igraph')
nodes <- c('a','b','c','d')
x <- c(0,1,2,3)
y <- c(0,3)
from <- c('a','d')
to <- c('b','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)

enter image description here

解决方法

假设最上面的节点是nodes中的第一个节点,使用layout_in_circle得到一个布局,然后旋转它的行直到最大 y 位于顶部,使用它作为最终布局。

# 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)))

screenshot

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。