Subgraph layout in graphviz
Asked Answered
R

1

6

I have code to display two subgraphs:

graph {
    rankdir=LR;
    subgraph cluster01 {
        label="t=0"
        a0 [label="A"];
        a1 [label="B"];
        a2 [label="C"];
        a5 [label="E"];
        a0 -- a1;
        a1 -- a2 ;
        a2 -- a0;
    };

    subgraph cluster02
    {
        label="t=10"
        b0 [label="A"];
        b5 [label="E"];
        b1 [label="B"];
        b2 [label="C"];

        b0 -- b1;
        b2 -- b5;
    };

    a0--b0 [style=dotted];
    a1--b1 [style=dotted];
    a2--b2 [style=dotted];
    a5--b5 [style=dotted];
}

This code displays two subgraphs like this:

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMxcF_AZVLQamyA/F23SY.png

But I want to have it like this:

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMxcF_AZVLQamyA/jUpIp.png

I hope someone will help me fix the "rankdir" to get it done.

Robedechambre answered 17/9, 2012 at 16:34 Comment(0)
P
12

The following was achieved by using invisible edges and constraint=false on some edges:

graphviz output

graph {
    rankdir=LR;
    subgraph cluster01 {
        label="t=0";
        a0 [label="A"];
        a1 [label="B"];
        a2 [label="C"];
        a5 [label="E"];
        a0 -- a1;
        a1 -- a2;
        a2 -- a5 [style=invis];
        a2 -- a0 [constraint=false];
    };

    subgraph cluster02
    {
        label="t=10"
        b0 [label="A"];
        b5 [label="E"];
        b1 [label="B"];
        b2 [label="C"];

        b0 -- b1;
        b1 -- b2 [style=invis];
        b2 -- b5;
    };

    edge[constraint=false];
    a0--b0 [style=dotted];
    a1--b1 [style=dotted];
    a2--b2 [style=dotted];
    a5--b5 [style=dotted];
}
Potheen answered 17/9, 2012 at 18:49 Comment(2)
Is there definitely no way to set positions of subgraphs? (As above or below each other?)Hargett
Technically, you can set the bb (bounding box) attribute of the cluster, but you also have to set the positions (pos) of all the nodes in the graph. Then use neato -n. Doable for a graph like above, but not trivial.Brachylogy

© 2022 - 2024 — McMap. All rights reserved.