64

I am trying to draw a graph with Graphviz.

I wish to draw a dotted line around a set of vertices, symbolizing that they are a part of a bigger entity.

Say for example, if I have three vertices as eggs, chicken and ham, I should be able to draw a dotted line around all three of them, and label that border as food.

Nicolas Kaiser
  • 1,628
  • 2
  • 14
  • 26
user916315
  • 1,017
  • 2
  • 9
  • 13

1 Answers1

109

It is possible to group nodes in graphviz by putting them into a cluster. A cluster is a special kind of subgraph which certain layout engines (dot, fdp, ...) support, and if supported, the nodes in a cluster will be drawn together.

From the documentation:

If the name of the subgraph begins with cluster, Graphviz notes the subgraph as a special cluster subgraph. If supported, the layout engine will do the layout so that the nodes belonging to the cluster are drawn together, with the entire drawing of the cluster contained within a bounding rectangle.

Note that, for good and bad, cluster subgraphs are not part of the DOT language, but solely a syntactic convention adhered to by certain of the layout engines.


Important: The ID of the subgraph has to start with cluster.


Example:

graph g{
 subgraph cluster_food {
    eggs; chicken; ham;
    label="Food";
    graph[style=dotted];
 }
}

ham, chicken and eggs

marapet
  • 54,856
  • 12
  • 170
  • 184
  • 4
    So I'm guessing subgraph has no effect with dot, as I tried adding this and it has resulted in no changes at all. :( – Hakanai Oct 18 '12 at 03:44
  • @Trejkaz It works as displayed with dot. You should ask a question and specify the exact graph, command line and version of graphviz you're using. – marapet Oct 18 '12 at 06:43
  • Yeah, if I can't figure out what I'm doing wrong I will definitely post a new question. – Hakanai Oct 18 '12 at 21:59
  • 3
    @Trejkaz Did you figure it out? The `graph[style=dotted]` attribute does nothing for me either. – jameshfisher Apr 02 '14 at 15:08
  • 63
    Ah, it's because it wasn't a *cluster*. @marapet, the `cluster_` prefix is apparently *not* just a convention; it is part of the syntax. – jameshfisher Apr 02 '14 at 15:11
  • 6
    @jameshfisher It _is_ a convention used by certain layout engines, but it is _not_ part of the dot language. From the link in my answer: _Note that, for good and bad, cluster subgraphs are not part of the DOT language, but solely a syntactic convention adhered to by certain of the layout engines_ . The point of my answer was to show that the name of the subgraph **has** to start with `cluster`. I'll emphasize that in the answer. – marapet Apr 02 '14 at 16:36
  • 2
    Is it possible to control the justification of the label? – user2023370 Jan 20 '16 at 15:38
  • 1
    I don't believe `neato` supports clusters unfortunately. – Timmmm Nov 23 '17 at 09:31