How to rearrange diagram in R

▼魔方 西西 提交于 2020-01-11 06:36:08

问题


I updated my diagrammer to version 0.9.0 and started rendering different diagram from the same data. My data frame now looks like this:

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

The rest of code looks like this:

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)

When code is used I get this diagram:

When it should look like this:


回答1:


Create graph with attr_theme = NULL:

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

In current version, DiagrammeR sets the global attribute layout to neato. You can check this with:

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

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




回答2:


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

I tried the above and failed when doing the following:

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)

The output would still have the same graph attributes as before.

Using the magrittr %>%then worked for me.

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"))

Documentation for all node, edge and graph attributes here: http://www.graphviz.org/doc/info/attrs.html#h:uses



来源:https://stackoverflow.com/questions/42165735/how-to-rearrange-diagram-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!