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

时间:2017-10-18 13:23:55

标签: r graph diagrammer

当我使用chartmer绘制网络图时,如下面的玩具示例......

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)  

enter image description here

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

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

通过visnetwork,我可以重新定位节点

enter image description here

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

PD:这是带有bergant解决方案的输出(标签隐藏): enter image description here enter image description here

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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

dot layoutttt