如何在DiagrammeR for R中使用GraphViz图

时间:2016-08-24 21:08:32

标签: r graphviz diagrammer

我正在尝试在DiagrammeR中使用GraphViz图。我怎么能这样做?

myGraph = grViz("
digraph boxes_and_circles {

  # a 'graph' statement
  graph [overlap = true, fontsize = 10]

  # several 'node' statements
  node [shape = box,
        fontname = Helvetica]
  A; B; C; D; E; F

  node [shape = circle,
        fixedsize = true,
        width = 0.9] // sets as circles
  1; 2; 3; 4; 5; 6; 7; 8

  # several 'edge' statements
  A->1 B->2 B->3 B->4 C->A
  1->D E->A 2->4 1->5 1->F
  E->6 4->6 5->7 6->7 3->8
}
")

然后我想在DiagrammeR中使用它,但它不会允许它。

render_graph(myGraph)

给出:

Error: class(graph) == "dgr_graph" are not all TRUE

我是否需要将GraphViz图转换或输入DiagrammeR环境?

2 个答案:

答案 0 :(得分:2)

grViz采用描述图形的字符串(vis.js样式):它是由vis.js解释的。它的返回值是一个htmlwidget对象。

render_graph采用dgr_graph对象,使用create_graph函数创建。

您可以看到in the DiagrammeR doc

library(DiagrammeR)

# Create a simple NDF
nodes <-
  create_nodes(
    nodes = 1:4,
    type = "number")

# Create a simple EDF
edges <-
  create_edges(
    from = c(1, 1, 3, 1),
    to = c(2, 3, 4, 4),
    rel = "related")

# Create the graph object,
# incorporating the NDF and
# the EDF, and, providing
# some global attributes
graph <-
  create_graph(
    nodes_df = nodes,
    edges_df = edges,
    graph_attrs = "layout = neato",
    node_attrs = "fontname = Helvetica",
    edge_attrs = "color = gray20")

# View the graph
render_graph(graph)

DiagrammeR可以生成Graphviz代码:来自下面提到的文档:&#34;如果您想返回Graphviz DOT代码(也许,可以共享它或直接使用Graphviz命令行实用程序) ),只需使用输出=&#34; DOT&#34;在render_graph()&#34;

所以

  1. 您可以使用create_graph生成graphviz点代码
  2. 您可以直接使用graphviz点代码与DiagrammeR中的grViz

答案 1 :(得分:0)

这里是 {render_graph(myGraph) 的问题,仅使用myGraph就像魅力一样。

library(DiagrammeR)

myGraph = grViz("
digraph boxes_and_circles {

  # a 'graph' statement
  graph [overlap = true, fontsize = 10]

  # several 'node' statements
  node [shape = box,
        fontname = Helvetica]
  A; B; C; D; E; F

  node [shape = circle,
        fixedsize = true,
        width = 0.9] // sets as circles
  1; 2; 3; 4; 5; 6; 7; 8

  # several 'edge' statements
  A->1 B->2 B->3 B->4 C->A
  1->D E->A 2->4 1->5 1->F
  E->6 4->6 5->7 6->7 3->8
}
") 

myGraph

render_graph(myGraph)在R中不起作用。

myGraph正常工作即可。