如何增加DiagrammeR R中节点之间的距离

时间:2016-06-27 13:48:50

标签: r graphviz diagrammer

我在R studio中有一个很好的图表与DiagrammeR,但是节点太集群了。我到处搜索,但我找不到增加它们之间距离的方法。我可以出席吗?

这是我的代码:

library(magrittr)
library(DiagrammeR)

# Create a simple NDF
nodes <- create_nodes(nodes = c("Index", "Surveillance", "Intervention","Lost"),
                     label = TRUE,
                     fontsize=55,
                     type = "lower",
                     style = "filled",
                     color = "aqua",
                     shape = c("circle", "circle",
                               "rectangle", "rectangle"),
                     data = c(30.5, 2.6, 9.4, 2.7))

edges <- create_edges(from = c("Index", "Surveillance","Surveillance","Intervention", "Surveillance", "Index" ), 
                  to = c("Surveillance", "Intervention","Surveillance","Intervention", "Lost", "Lost"),
                  rel = c(99, 6.7, 99, 99, 27, 22),
                  arrowhead = rep("normal", 6),
                  color = c("green", "green", "red", "red", "red", "red"))


graph <-
  create_graph(
    nodes_df = nodes,
    edges_df = edges,
    graph_attrs <-
      c("layout = dot","overlap = FALSE","outputorder = edgesfirst"),
    node_attrs <-
      c("shape = circle",
        "fixedsize = TRUE",
        "width = 100",
        "penwidth = 1",
        "color = DodgerBlue",
        "style = filled",
        "fillcolor = Aqua",
        "alpha_fillcolor = 0.5",
        "fontname = Helvetica",
        "fontcolor = Black"),
    edge_attrs = "color = gray20")

# View the graph
render_graph(graph,layout=constant,output="visNetwork")

1 个答案:

答案 0 :(得分:2)

您可以设置不同节点之间箭头的长度:

edges <- create_edges(from = c("Index", "Surveillance","Surveillance","Intervention", "Surveillance", "Index" ), 
                      to = c("Surveillance", "Intervention","Surveillance","Intervention", "Lost", "Lost"),
                      rel = c(99, 6.7, 99, 99, 27, 22),
                      arrowhead = rep("normal", 6),
                      color = c("green", "green", "red", "red", "red", "red"), 
                      length = c(200,200,50,50,200,200))

enter image description here

或者您可以为每个节点定义一个精确的点:

nodes <- create_nodes(nodes = c("Index", "Surveillance", "Intervention","Lost"),
                     label = TRUE,
                     fontsize = 55,
                     type = "lower",
                     style = "filled",
                     color = "aqua",
                     shape = c("circle", "circle",
                               "rectangle", "rectangle"),
                     data = c(30.5, 2.6, 9.4, 2.7),
                     x = c(-80,80,-80,80),
                     y = c(-80,80,80,-80))

enter image description here