Search code examples
graphgraphvizdotgraph-visualization

unable to print the label and outline box of a cluster subgraph in graphviz


I am a newbie to graphviz and the DOT language. I was trying out generating graphs with cluster subgprahs. However, when I have clusters, each with node positions exactly specified in the script, graphviz does not generate the outline boxes or the labels for the clusters! Specifically, if I have the following DOT script:

digraph G{
subgraph cluster0{
label="Cluster 0"
a->b
}
subgraph cluster1{
label="Cluster 1"
c->d
}
}

then the graph generated is: Graph generated with clusters when nodes positions are not specified by the user

However, with the following DOT script, in which I specify the node positions of the 4 nodes exactly:

digraph G{
subgraph cluster0{
label = "Cluster 0"
a[pos="10,200"]
b[pos="100,200"]
a->b
}
subgraph cluster1{
label = "Cluster 1"
c[pos="10,100"]
d[pos="100,100"]
c->d
}
}

The graph generated is: Graph generated with clusters when nodes positions are specified by the user

Notice that in this case, neither the outline box for the clusters nor the labels for the clusters are printed!! In this case, there is a clear demarcation between the 2 clusters as you can see - the clusters don't overlap, so in principle graphviz should not have a problem showing them, am I right?

How can I tell graphviz to draw the cluster outline boxes and labels no matter what? Any help in this will be greatly appreciated!!

Thanks!


Solution

  • The dot layout engine doesn't support the pos attribute.

    To render a graph with positions of all nodes predefined, you should use neato or fdp with the -n option.

    neato doesn't support clusters (though it seems it should now). Fortunately, fdpdoes!

    Therefore, you may use the following command:

    dot -Tpdf -Kpdf -n -O filename.dot
    

    or

    fdp -Tpdf -n -O filename.dot
    

    Unfortunately, the positions of the nodes relative to the cluster are ok, but clusters still seem to get moved by fdp (-n switch didn't make a difference).

    I didn't try with the latest version (I used 2.29.20120504), but if the latest doesn't work neither, this may be a case for a bug report.

    Btw, since positions are assumed to be in inches, this will create a very large graph.


    Output I get with fdp (with or without -n switch) - I added size=20 to limit the image size:

    fdp output


    Alternative solution without using pos:

    digraph G{
    subgraph cluster0{
    label = "Cluster 0"
    {rank=same; a->b;}
    }
    subgraph cluster1{
    label = "Cluster 1"
    {rank=same; c->d; }
    }
    
    a -> c [style=invis];
    }