matthias matthias - 2 months ago 13
R Question

How to specify vertical alignment of nodes in R-package "DiagrammeR"

Using the

R
package
DiagrammeR
, I would like to create a hierarchical representation of regression model trees.

I have four splits of my original data (blue ellipses) and five terminal nodes (red squares). As you can see from the image below, models 2, 7 and 8 are terminal nodes, but they do not appear on the same level (with respect to their vertical alignment) as models 4 and 5. How can I achieve this?

library("DiagrammeR")

grViz("
digraph CFA {
# Multiple level nodes
node [shape = ellipse, color=CornflowerBlue]
a [label = '@@1'];
b [label = '@@2'];
c [label = '@@3'];
d [label = '@@4'];

# Terminal branch nodes
node [shape = box, color = Crimson]
e [label = 'Model 2'];
f [label = 'Model 4'];
g [label = 'Model 5'];
h [label = 'Model 7'];
i [label = 'Model 8'];

# Connect nodes with edges and labels
a -> b [label = 'Condition 1a']
a -> d [label = 'Condition 1b']
b -> e [label = 'Condition 2a']
b -> c [label = 'Condition 2b']
c -> f [label = 'Condition 3a']
c -> g [label = 'Condition 3b']
d -> h [label = 'Condition 4a']
d -> i [label = 'Condition 4b']
}

[1]: 'Split 1'
[2]: paste0('Model 1\\n Split 2')
[3]: paste0('Model 3\\n Split 3')
[4]: paste0('Model 6\\n Split 4')
")


Model tree




Edit



I have found to adjust the length of the edges manually by grouping the terminal nodes that were not placed as expected and defining the
minlength
argument (see below). However, I would prefer an approach that requires no fiddling around with parameters.

grViz("
digraph CFA {
# latent variables
node [shape = ellipse, color=CornflowerBlue]
a [label = '@@1'];
b [label = '@@2'];
c [label = '@@3'];
d [label = '@@4'];

node [shape = box, color = Crimson]
e [label = 'Model 2'];
f [label = 'Model 4'];
g [label = 'Model 5'];
h [label = 'Model 7'];
i [label = 'Model 8'];

# Define arrow length for first group
edge [color = grey, minlen = 1]
a -> b [label = 'Condition 1a']
a -> d [label = 'Condition 1b']
b -> c [label = 'Condition 2b']
c -> f [label = 'Condition 3a']
c -> g [label = 'Condition 3b']

# Define edge length for models 2, 7 and 8
edge [color = grey, minlen = 2]
b -> e [label = 'Condition 2a']
d -> h [label = 'Condition 4a']
d -> i [label = 'Condition 4b']
}

[1]: 'Split 1'
[2]: paste0('Model 1\\n Split 2')
[3]: paste0('Model 3\\n Split 3')
[4]: paste0('Model 6\\n Split 4')
")

Answer

For example

library("DiagrammeR")

grViz(" 
  digraph CFA {
    # Multiple level nodes
    node [shape = ellipse, color=CornflowerBlue]
    a [label = '@@1']; 
    b [label = '@@2']; 
    c [label = '@@3']; 
    d [label = '@@4'];
    {rank = same; b; d}

    # Terminal branch nodes
    node [shape = box, color = Crimson] 
    e [label = 'Model 2'];
    f [label = 'Model 4'];
    g [label = 'Model 5'];
    h [label = 'Model 7'];
    i [label = 'Model 8'];
    {rank = same; e; f; g; h; i}

    # Connect nodes with edges and labels
    a -> b [label = 'Condition 1a']
    a -> d [label = 'Condition 1b'] 
    b -> e [label = 'Condition 2a'] 
    b -> c [label = 'Condition 2b']
    c -> f [label = 'Condition 3a']
    c -> g [label = 'Condition 3b']
    d -> h [label = 'Conddition 4a'] 
    d -> i [label = 'Condition 4b'] 
  }

[1]: 'Split 1' 
[2]: paste0('Model 1\\n Split 2') 
[3]: paste0('Model 3\\n Split 3') 
[4]: paste0('Model 6\\n Split 4') 
")

gives you

enter image description here