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

时间:2016-09-12 13:11:27

标签: r graphviz hierarchical 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') 
")

Model tree

修改

我发现通过对未按预期放置的终端节点进行分组并定义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')
")

1 个答案:

答案 0 :(得分:2)

例如

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

给你

enter image description here