Search code examples
rdiagramflowchartmermaid

Using DiagrammeR to place one rows of nodes below another


Given the following flow chart derived via DiagrammeR package:

DiagrammeR::DiagrammeR(diagram =
                       "
                       graph LR;
                       A[Start]-->B;
                       B --> C;
                       C --> D;
                       D --> E;
                       E --> F;
                       ")

Sample Diagram

Would it be possible to wrap it so the node E is below node D and node F follows to the left as in the picture below:

enter image description here


Solution

  • You can do this using the graphviz option.

    For example, you can get the basic node positions of the graph using

    library(DiagrammeR)
    
    grViz("
      digraph flow {
           D -> E;
           {rank=same ; A -> B -> C -> D};
           {rank=same ;           F -> E[dir=back]};
       }
    ")
    

    And you can add a few attributes to prettify it

    grViz('
      digraph flow {
    
           # set characteristics 
           node[shape=square, 
                height=1,
                color=skyblue, 
                penwidth=2,
                fillcolor=lavender, 
                style=filled,
                fontsize=25, 
                fontcolor=grey46,
                fontname = "helvetica" # this may be OS dependent
                ];
           edge[minlen=2, 
                color=grey,
                penwidth=2
                ];
           nodesep=0.5; #hack
           A[label=Start];
    
           # Graph
           D -> E;
           {rank=same ; A -> B -> C -> D};
           {rank=same ;           F -> E[dir=back]};
      }
    ')
    

    Which produces

    enter image description here