r语言 - 使用 DiagrammeR 时从图形中获取 DOT 文件



看起来DiagrammeR已经改变了他们的create_nodescreate_edges函数,而不是在文档中看起来。 此外,$dot_code属性也不再存在。我找不到这个的替代品。

这是他们在文档中的示例代码,但它不起作用。 以下示例可在其官方DiagrammeR网站上找到。

###
# Create a graph with both nodes and edges
# defined, and, add some default attributes
# for nodes and edges
###
library(DiagrammeR)
# Create a node data frame
nodes <-
create_nodes(nodes = c("a", "b", "c", "d"),
label = FALSE,
type = "lower",
style = "filled",
color = "aqua",
shape = c("circle", "circle",
"rectangle", "rectangle"),
data = c(3.5, 2.6, 9.4, 2.7))
edges <-
create_edges(from = c("a", "b", "c"),
to = c("d", "c", "a"),
rel = "leading_to")

graph <-
create_graph(nodes_df = nodes,
edges_df = edges,
node_attrs = "fontname = Helvetica",
edge_attrs = c("color = blue",
"arrowsize = 2"))
graph
#> $nodes_df
#>   nodes label  type  style color     shape data
#> 1     a       lower filled  aqua    circle  3.5
#> 2     b       lower filled  aqua    circle  2.6
#> 3     c       lower filled  aqua rectangle  9.4
#> 4     d       lower filled  aqua rectangle  2.7
#>
#> $edges_df
#>   from to        rel
#> 1    a  d leading_to
#> 2    b  c leading_to
#> 3    c  a leading_to
#>
#> $graph_attrs
#> [1] NULL
#>
#> $node_attrs
#> [1] "fontname = Helvetica"
#>
#> $edge_attrs
#> [1] "color = blue"  "arrowsize = 2"
#>
#> $directed
#> [1] TRUE
#>
#> $dot_code
#> [1] "digraph {nngraph [rankdir = LR]nnnode [fontnam...
#>
#> attr(,"class")
#> [1] "dgr_graph"
# View the graph in the RStudio Viewer
render_graph(graph)

查看DiagrammeR软件包的帮助,该网站确实不是最新的。 查看帮助中的函数 create_graph((,该函数显示了语法如何更改。

像这样的事情应该做同样的事情:

###
library(DiagrammeR)
# Create a node data frame (ndf) where the labels
# are equivalent to the node ID values (this is not
# recommended); the `label` and `type` node
# attributes will always be a `character` class
# whereas `id` will always be an `integer`
nodes <-
create_node_df(
n         = 4,
type      = "lower", 
label     = c("a", "b", "c", "d"),
style     = "filled",
fillcolor = "blue",
shape     = c("circle", "circle",
"rectangle", "rectangle"),
data      = c(3.5, 2.6, 9.4, 2.7))
# Create an edf with additional edge
# attributes (where their classes will
# be inferred from the input vectors)
edges <-
create_edge_df(
from = c(1, 2, 3),
to   = c(4, 3, 1),
rel  = "leading_to")
# With `create_graph()` we can
# simply create an empty graph (and
# add in nodes and edges later
# with other functions)
graph <-
create_graph(
nodes_df  = nodes,
edges_df  = edges)     %>%
set_edge_attrs(
edge_attr = color,
values    = "green")   %>%
set_edge_attrs(
edge_attr = arrowsize,
values    = 2)         %>%
set_node_attrs(
node_attr = fontname,
values    = "Helvetica")
graph
render_graph(graph)

坏消息

对于任何查看此僵尸线程的人来说,截至本文发布时,Rich Iannone的DiagrammeR文档页面上发布的信息仍然过时:http://rich-iannone.github.io/DiagrammeR/graph_creation.html。

好消息

但是,图表器包的小插图包含更新的信息。如名为"从 NDF/EDF 创建简单图形"的包小插图所示,您可以使用名为generate_dot()的函数查看和导出GraphViz点代码

相关内容

  • 没有找到相关文章

最新更新