简体   繁体   中英

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. In more complex examples we cannot remove all crossings but at least minimize it.

With visnetwork I can relocate the nodes

在此处输入图片说明

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): 在此处输入图片说明 在此处输入图片说明

I think the dot layout will produce it better. 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 )

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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