如何在 R 包中指定节点的垂直对齐 "DiagrammeR"

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

使用 RDiagrammeR,我想创建回归模型树的分层表示。

我有四个原始数据拆分(蓝色椭圆)和五个终端节点(红色方块)。从下图中可以看出,模型 2、7 和 8 是终端节点,但它们与模型 4 和 5 不在同一水平面上(相对于它们的垂直对齐)。我如何实现这一点?

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


编辑

我发现可以通过对未按预期放置的终端节点进行分组并定义 minlength 参数来手动调整边的长度(见下文)。但是,我更喜欢一种不需要摆弄参数的方法。

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

例如

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

给你