设置网络圆图的方向为顺时针
Set the direction of network circle graph to clockwise
我有下面的图表,其中我总是在顶部显示节点 a
。我想改变的是用 igraph
将圆网络的方向更改为始终正确(顺时针)。这是一个基于
的示例
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)
# 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)))
ggraph
默认这样做
library(ggraph)
ggraph(a, layout = 'linear', circular = TRUE) +
geom_edge_link(arrow = arrow(length = unit(4, 'mm')), end_cap = circle(4, 'mm')) +
geom_node_label(aes(label = name)) +
coord_fixed()
您也可以手动设置位置:
library(ggraph)
library(tidygraph)
a %>%
as_tbl_graph() %>%
mutate(
alpha = pi/2 - 0:(gsize(a) - 1)*2*pi/gsize(a),
x = cos(alpha),
y = sin(alpha)
) %>%
ggraph(x = x, y = y) +
geom_edge_link(arrow = arrow(length = unit(4, 'mm')), end_cap = circle(4, 'mm')) +
geom_node_label(aes(label = name)) +
coord_fixed()
您可以通过order
参数确定方向:
plot(a, layout = rot(layout_in_circle(a, order = order(from,decreasing = T))))
请注意,order(from, decreasing =T)
在这里有效,因为节点名称按字母顺序递增。
有关更通用的解决方案,请参阅 .
我有下面的图表,其中我总是在顶部显示节点 a
。我想改变的是用 igraph
将圆网络的方向更改为始终正确(顺时针)。这是一个基于
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)
# 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)))
ggraph
默认这样做
library(ggraph)
ggraph(a, layout = 'linear', circular = TRUE) +
geom_edge_link(arrow = arrow(length = unit(4, 'mm')), end_cap = circle(4, 'mm')) +
geom_node_label(aes(label = name)) +
coord_fixed()
您也可以手动设置位置:
library(ggraph)
library(tidygraph)
a %>%
as_tbl_graph() %>%
mutate(
alpha = pi/2 - 0:(gsize(a) - 1)*2*pi/gsize(a),
x = cos(alpha),
y = sin(alpha)
) %>%
ggraph(x = x, y = y) +
geom_edge_link(arrow = arrow(length = unit(4, 'mm')), end_cap = circle(4, 'mm')) +
geom_node_label(aes(label = name)) +
coord_fixed()
您可以通过order
参数确定方向:
plot(a, layout = rot(layout_in_circle(a, order = order(from,decreasing = T))))
order(from, decreasing =T)
在这里有效,因为节点名称按字母顺序递增。
有关更通用的解决方案,请参阅