简体   繁体   English

如何使用制图器 R 最小化边缘交叉的数量?

[英]How to minimize the number of edge crossings with diagrammer, R?

When I plot a network graph with diagrammer, like the following toy example...当我使用制图器绘制网络图时,如下面的玩具示例...

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)  

在此处输入图片说明

How can I tell diagrammer to minimize the number of edge crossings automatically?我如何告诉制图师自动最小化边缘交叉的数量?

In this simple example if C and B positions were exchanged the result wouldn't have any edge crossing.在这个简单的例子中,如果 C 和 B 位置交换,结果不会有任何边缘交叉。 In more complex examples we cannot remove all crossings but at least minimize it.在更复杂的例子中,我们不能删除所有交叉,但至少可以将其最小化。

With visnetwork I can relocate the nodes使用 visnetwork 我可以重新定位节点

在此处输入图片说明

But it has other drawbacks, for example it doesn't let you export it as a vector graphic.但它还有其他缺点,例如它不允许您将其导出为矢量图形。

PD: This is the output with bergant solution (with labels hidden): PD:这是带有 bergant 解决方案的输出(隐藏标签): 在此处输入图片说明 在此处输入图片说明

I think the dot layout will produce it better.我认为dot布局会产生更好的效果。 Add global attributes like this:像这样添加全局属性:

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

use dot layout option (made with inspiration from here )使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM