Keep nodes at their position

I’m trying to make this graph with graphviz, but I’m unable to keep the nodes at the same level and order.
graph

My dot file is like this

digraph cfg{
    rankdir=LR;
    graph [pad=0.5; ranksep=1, nodesep=0.4];
    subgraph cluster_1{
		node [shape = doublecircle] D;
		node [shape = circle];
		color=white;
        rank = same; A; B; C; D;
        A [label = 1];
        B [label = 2];
        C [label = 3];
        D [label = 4];
        A -> B;
		B -> C;
		C -> D;
	}

	subgraph cluster_0{
		node [shape = doublecircle] 4;
		node [shape = circle];
		color=white;
		1 -> 2 [taillabel = <n-p<sub>a</sub>>  labeldistance=3.0];
		2 -> 3 [taillabel = <0-p<sub>b</sub>> labeldistance=3.0];
		3 -> 4 [taillabel = <n-p<sub>a</sub>> labeldistance=3.0];
        1 -> 1 [taillabel =<Np<sub>a</sub>/x-p<sub>a</sub>> labeldistance=5.0];
        2 -> 2 [taillabel =<Np<sub>a</sub>n> labeldistance=5.0];
        3 -> 2 [taillabel =<Np<sub>a</sub>n> labeldistance=5.0];
        4 -> 4 [taillabel =<N/x-p<sub>a</sub>> labeldistance=5.0];
        4 -> 2 [taillabel =<n-p> labeldistance=5.0];
	}
}

I almost got it right. I used neato to be able to control de position. I only have two problems now. The first one is the edges " 3 → 2", “4 → 2” that are straight lines, I was unable to make it curved. The second problem is the edge label position, it’s to closed to the edge.

graph2

digraph cfg{
    rankdir=LR;
    layout=neato;
    graph [pad=0.5; ranksep=1, nodesep=0.5];
    fontname="Arial";
    subgraph cluster_1{
		node [shape = doublecircle] D;
		node [shape = circle];
		color=white;
        A [label = 1, pos="0,-2!"];
        B [label = 2, pos="1.5,-2!"];
        C [label = 3, pos="3,-2!"];
        D [label = 4, pos="4.5,-2!"];
        A -> B [taillabel = <n-p<sub>a</sub>>  labeldistance=2.0 tailport=e, headport=w];
		B -> C [taillabel = <m-p<sub>b</sub>>  labeldistance=2.0 tailport=e, headport=w];
		C -> D [taillabel = <y-p<sub>b</sub>>  labeldistance=2.0 tailport=e, headport=w];
        A -> A [label =<Np<sub>b</sub>>, tailport=n, headport=n dir=back];
        C -> C [label =<Np<sub>b</sub>n/y-p<sub>b</sub>>, tailport=n, headport=n dir=back];
        D -> D [label =<N/n-p<sub>a</sub>>, tailport=n, headport=n dir=back];
        B -> A [label =<x-p<sub>a</sub>> tailport=s, headport=s splines=true];
        D -> B [label =<x-p<sub>a</sub>> tailport=s, headport=s splines=true];
	}

	subgraph cluster_0{
		node [shape = doublecircle] 4;
		node [shape = circle];
		color=white;
        1 [pos="0,0!"];
        2 [pos="1.5,0!"];
        3 [pos="3,0!"];
        4 [pos="4.5,0!"];
        1 -> 2 -> 3 -> 4 [ color="invis" ];
		1 -> 2 [taillabel = <x-p<sub>a</sub>>  labeldistance=2.0 tailport=e, headport=w];
		2 -> 3 [taillabel = <0-p<sub>b</sub>> labeldistance=2.0 tailport=e, headport=w];
		3 -> 4 [taillabel = <n-p<sub>a</sub>> labeldistance=2.0 tailport=e, headport=w];
        1 -> 1 [label =<Np<sub>a</sub>/x-p<sub>a</sub>>, tailport=n, headport=n dir=back];
        2 -> 2 [label =<Np<sub>a</sub>n> labeldistance=1.0 tailport=n headport=n dir=back];
        3 -> 2 [label =<Np<sub>a</sub>n> tailport=n headport=n splines=curved];
        4 -> 4 [label =<N/x-p<sub>a</sub>> tailport=n, headport=n dir=back];
        4 -> 2 [label =<x-p<sub>a</sub>> tailport=s, headport=s splines=true];
	}
}

Here is a dot version that is pretty close. Some of the changes are cosmetic (port specifications and clusters) but most seemed to be necessary.

  • rankdir attribute must be in in NON-cluster subgraph
  • xlabel seems to work better than taillable (better placement & edges)
  • it was a struggle to get desired text placement and label placement
  • Note that <sub> (subscripts) seems to be broken in recent versions of Graphviz, I will create an issue
// from https://forum.graphviz.org/t/keep-nodes-at-their-position/1134
digraph cfg{
    //rankdir=LR;  // not needed
    graph [ranksep=1.5, nodesep=0.5];
      // clusters not needed, but do not hurt
	//subgraph cluster_0{
	//node [shape = doublecircle] 4;
	node [shape = circle];
	color=white;
	{ rank=same
		// combined node attributes (e.g. shape, group, label)
		1 [group=A1]
		2 [group=B2]
		3 [group=xyz]
		4 [group=abc shape = doublecircle]
		1 -> 2 [label = <x-p<SUB>a</SUB>> ]
		2 -> 3 [xlabel = <0-p<sub>b</sub>> ]
		3 -> 4 [label = <n-p<sub>a</sub>> ]
	        1:n -> 1:n [label =<Np<sub>a</sub>/x-p<sub>a</sub>> ]
		2:n -> 2:n [label =<Np<sub>a</sub>n> constraint=false];
		3:nw -> 2:ne [taillabel =<Np<sub>a</sub>n> constraint=false  labeldistance=4.0]
		4:n -> 4:n [label =<N/x-p<sub>a</sub>>];
		4:s -> 2:s [xlabel =<x-p<sub>a</sub>> labeldistance=10]
	}
	//}

   //subgraph cluster_1{
	//node [shape = doublecircle] D;  
	node [shape = circle];
	color=white;
        {rank = same;
	// combined node attributes (e.g. shape, group, label)
	A [group=A1  label = 1]
	B [group=B2  label = 2]
	C [group=xyz label = 3]
	D [shape = doublecircle group=abc label = 4]
	A -> B [label = <n-p<sub>a</sub>>  ]
	B -> C [label = <m-p<sub>b</sub>>  ]
	C -> D [label = <y-p<sub>b</sub>>  ]
        A:n -> A:n [label =<Np<sub>b</sub>>,  dir=back];
        C -> C [label =<Np<sub>b</sub>n/y-p<sub>b</sub>>, tailport=n, headport=n dir=back];
        D -> D [label =<N/n-p<sub>a</sub>>, tailport=n, headport=n dir=back];
        B:s -> A:s [label =<x-p<sub>a</sub>> ]
        D:s -> B:s [xlabel =<n-p> labeldistance=10. constraint=false];
	}	
	//}
        edge [style=invis]
	// invisible edges & group attribute for vertical alignment
	1->A // establish 2nd rank
	// vertically align other nodes, by group
	2->B
	3->C
	4->D
}

Giving:
another_fsm2

1 Like

New issue (subscript bug): html text subscripts fail (#2235) · Issues · graphviz / graphviz · GitLab