2017-10-18 77 views
2

當我繪製與製圖網絡圖,像下面的例子玩具...如何使用圖解器最小化邊緣交叉點的數量,R?

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

回答

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

+0

是否也可以模仿我的visnetwork結果呢? (例如,在每個邊上添加標籤或更改邊緣寬度) – skan

+1

10標籤:是,在'create_edge_df'(字符串向量)中添加'label'參數。 – bergant

+0

寬度:使用'penwidth' = 3例如 – bergant

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

+0

如果我想要一個圓形佈局,命令行是什麼?或者我怎樣才能將它的位置固定在一些座標上?我見過的唯一選項(create_node_df,x =和y =)似乎只在使用render_graph(graph,output =「visNetwork」)時才起作用,但該輸出不能保存爲矢量圖。 – skan

+0

我建議你問一個新問題。把它做成一件事,然後用這個問題和答案作爲參考。 –