如何在 R 中重新排列图表

How to rearrange diagram in R

我将 diagrammer 更新到版本 0.9.0 并开始从相同的数据渲染不同的图表。我的数据框现在看起来像这样:

df <- data.frame(col1 = c( "Cat", "Dog", "Bird"),
                 col2 = c( "Feline", "Canis", "Avis"), 
                 stringsAsFactors=FALSE)

其余代码如下所示:

uniquenodes <- unique(c(df$col1, df$col2))
library(DiagrammeR)
nodes <- create_node_df(n=length(uniquenodes), nodes = seq(uniquenodes),  type="number", label=uniquenodes)
edges <- create_edge_df(from=match(df$col1, uniquenodes), to=match(df$col2, uniquenodes), rel="related")

g <- create_graph(nodes_df=nodes, edges_df=edges)
render_graph(g)

使用代码时,我得到了这个图表:

什么时候应该是这样的:

创建图表 attr_theme = NULL:

g <- create_graph(nodes_df=nodes, edges_df=edges, attr_theme = NULL)

在当前版本中,DiagrammeR 将全局属性 layout 设置为 neato。 您可以通过以下方式检查:

g <- create_graph(nodes_df=nodes, edges_df=edges)
get_global_graph_attrs(g)

#           attr      value attr_type
# 1       layout      neato     graph
# 2  outputorder edgesfirst     graph
# 3     fontname  Helvetica      node
# 4     fontsize         10      node
# 5        shape     circle      node
# 6    fixedsize       true      node
# 7        width        0.5      node
# 8        style     filled      node
# 9    fillcolor  aliceblue      node
# 10       color     gray70      node
# 11   fontcolor     gray50      node
# 12         len        1.5      edge
# 13       color     gray40      edge
# 14   arrowsize        0.5      edge

您还可以在创建图形对象后使用 set_global_graph_attrs 设置这些属性。

You can also set these attributes with set_global_graph_attrs after creating the graph object.

我尝试了上面的方法,但在执行以下操作时失败了:

set_global_graph_attrs(
    graph = graph,
    attr = c("layout", "rankdir", "splines"),
    value = c("dot", "LR", "false"),
    attr_type = c("graph", "graph", "graph"))

render_graph(graph2)

输出仍具有与以前相同的图形属性。

使用 magrittr %>%然后对我有用。

graph1 <-
   create_graph(
      nodes_df = ndf,
      edges_df = edf) %>%
   set_global_graph_attrs(
      attr = c("layout", "rankdir", "splines"),
      value = c("dot", "LR", "false"),
      attr_type = c("graph", "graph", "graph"))

此处所有节点、边和图形属性的文档:http://www.graphviz.org/doc/info/attrs.html#h:uses

*set_global_graph*_attrs 现在 *add_global_graph_attrs*(2020 年 1 月)