gpt4 book ai didi

r - 如何在 DiagrammeR for R 中使用 GraphViz 图

转载 作者:行者123 更新时间:2023-12-03 23:21:38 32 4
gpt4 key购买 nike

我正在尝试在 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 环境中?

最佳答案

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

render_graph 使用使用 create_graph 函数创建的 dgr_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 代码:来自下面提到的文档:“如果您想返回 Graphviz DOT 代码(或许,共享它或直接与 Graphviz 命令行实用程序一起使用),只需使用 output = "DOT “在render_graph()”中

所以
  • 您可以使用 create_graph 生成 graphviz 点代码
  • 您可以在 DiagrammeR 中直接使用 grViz 使用 graphviz 点代码
  • 关于r - 如何在 DiagrammeR for R 中使用 GraphViz 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39133058/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com