0

看起来 DiagrammeR 已经改变了它们的create_nodes功能create_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 {\n\ngraph [rankdir = LR]\n\nnode [fontnam...
#>
#> attr(,"class")
#> [1] "dgr_graph"

# View the graph in the RStudio Viewer
render_graph(graph)
4

2 回答 2

0

坏消息

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

好消息

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

于 2021-05-24T14:20:59.700 回答
0

查看 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)
于 2019-06-08T19:03:13.290 回答