2

当我用图表绘制器绘制网络图时,就像下面的玩具示例......

library(DiagrammeR)
Dia <- function(edg, nodesd) { 
         nodes <-   create_node_df(  n=length(nodesd), label=nodesd,  
         width=0.3) 
         edges <- create_edge_df(from = edg$EveFrom, to = edg$EveTo, 
                rel = "leading_to")   
graph <-   create_graph(  nodes_df = nodes, edges_df = edges)
render_graph(graph)
}


niv <- c("A","B","C","D","E","X","Y")

temp <- data.table(EveFrom=factor(c("A","A","A","A","B","C","D","E", 
     "X", "B"), levels=niv),
EveTo=factor(c("B","C","D","E","X","X","Y","Y","Y", "C"), levels=niv))

Dia(temp,niv)  

在此处输入图像描述

我如何告诉制图师自动最小化边缘交叉的数量?

在这个简单的示例中,如果交换 C 和 B 位置,则结果不会有任何边缘交叉。在更复杂的示例中,我们不能删除所有交叉点,但至少可以将其最小化。

使用 visnetwork 我可以重新定位节点

在此处输入图像描述

但它还有其他缺点,例如它不允许您将其导出为矢量图形。

PD:这是带有 bergant 解决方案的输出(隐藏标签): 在此处输入图像描述 在此处输入图像描述

4

2 回答 2

1

我认为dot布局会产生更好的效果。像这样添加全局属性:

graph <- create_graph(  nodes_df = nodes, edges_df = edges)
graph <- set_global_graph_attrs(graph, "layout", "dot", "graph")
graph <- add_global_graph_attrs(graph, "rankdir", "LR", "graph")

G

于 2017-10-18T13:50:18.300 回答
1

使用dot布局选项(灵感来自这里

# install.packages(c("DiagrammeR"), dependencies = TRUE)
library(DiagrammeR)

grViz("
digraph dot {

graph [layout = dot] # dot, neato, twopi, and circo

A -> {B C D E}
B -> {C X}
C -> {X}
D -> {Y}
E -> {Y}
X -> {Y}
}")

点布局ttt

于 2017-10-18T14:03:46.557 回答