将圆形布局的顺序设置为减小将更改igraph网络中的节点位置 [英] Setting the order of circle layout to decreasing changes the nodes position in the igraph network
问题描述
我在左下方创建了一个圆形网络.节点"Robert Zero"
始终设置为位于顶部.每次节点的数量可能会有所不同,所以我必须得到一个通用的解决方案.这种情况发生在以下代码中:
I create the circle network below with the left direction. The node "Robert Zero"
is always set to be on top. The nummber of nodes may fiffer every time so I must get a generic solution.This happens with this code:
library('igraph')
id <- c("Steve Sweet", "Tom Thompson", "Roger Rabbit", "Robert Zero" )
label <- c("Steve Sweet", "Tom Thompson", "Roger Rabbit", "Robert Zero" )
color<-c("gray", "gray", "gray", "yellow")
nodes <- data.frame(id,label,color)
from <- c("Steve Sweet", "Tom Thompson", "Roger Rabbit", "Robert Zero" )
to <- c("Tom Thompson", "Roger Rabbit", "Robert Zero", "Steve Sweet")
group<-c("Residential Mortgage", "Real Estate Law","Divorce Attorney","Personal Injury Law" )
width<-c(4.00, 1.00, 3.00, 0.01)
color<-c("cyan", "cyan", "cyan", "cyan")
lty<-c("solid", "solid", "solid", "dashed")
label<-c(NA,NA,NA,"bridge")
edges <- data.frame(from,to,group,width,color,lty,label)
#Bringing the to name in this case main_name in the 1st row of nodes dataframe
nodes<-nodes[order(nodes$id != "Robert Zero"),]
#creating the graph object of nodes and edges simple paths
gph2 <- graph_from_data_frame(edges, directed=TRUE, vertices=nodes)
rot <- function(mat, mx = which.max(mat[, 2])) {
if (mx == 1) mat else mat[c(mx:nrow(mat), 1:(mx-1)), ]
}
#plot every network
plot(gph2, layout = rot(layout_in_circle(gph2)))
但是随后我想使用 igraph
将此网络的方向转为顺时针(正确)方向
But then I want to turn the direction of my network to clockwise (right) direction using igraph
with this
plot(gph2, layout = rot(layout_in_circle(gph2, order = order(from,decreasing = T))))
但是当我希望它们创建如上所述的但方向相反的网络时,我得到的网络具有彼此交叉的边缘:
but then I get this network with edges that cross each other while I want them to create a network like the above but with opposite direction:
推荐答案
您可以获取顶点数字序列并将其求逆:
You could get the vertices number sequence and inverse it :
vertices <- as.numeric(V(gph2))
inverse_order <- vertices[length(vertices):1]
plot(gph2, layout = rot(layout_in_circle(gph2, order = inverse_order)))
这篇关于将圆形布局的顺序设置为减小将更改igraph网络中的节点位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!