Graphviz node ordering - graphviz

I want to have a diagram which has the layout as follwing :
But when i wrote the codes like this:
digraph g {
a->b->c;
{rank=same;b,d,e,f,g,h}
d->g [weight = 1];
d->f [weight = 10];
}
And comes like this:
The dot guide even recommends this:
Edge weights also play a role when nodes are constrained to the same rank.
Edges with non-zero weight between these nodes are aimed across the rank
in the samedirection (left-to-right, or top-to-bottom in a rotated drawing) as far
as possible. This fact may be exploited to adjust node ordering by placing
invisible edges (style="invis") where needed.
I wander why it not work?

add the following line in your graph:
f->g[style=invis];

digraph g {
a->b->c;
{rank=same;b,d,e,f,g,h}
d->g [weight = 0];
d->f [weight = 1];
}
Works as you wish.
But, I also don't know why
d->g [weight = 1];
d->f [weight = 2];
doesn't work.
Maybe it depends on layout type, neato or dot or etc. See https://graphviz.gitlab.io/docs/attrs/weight/
Also
digraph {
a -> b -> c;
d->g
d->f
{rank = same; b;d;e;f;g;h;}
}
Works. Source

Related

How do I vertically align a node with the center of a cluster?

I am looking for a way to align individual nodes in graphviz with an entire subgraph/cluster.
I am currently teaching a small team of analysts to use rmarkdown and I want to allow them to include simple graphs and flowcharts in their documents. I therefore use DiagrammeR (and viz.js) to render charts written in dot. To increase user adoption, I want to minimize complexity and avoid HTML and use raw dot.
Our problem is that we frequently try to align nodes with subgraphs/clusters containing multiple nodes, but we cannot align the entire subgraph with the node. Alignment only works with the constituent nodes within the subgraph/cluster. (see nodes C,D,E and G,T below)
I have tried to create a record node using GT [shape = record label="{G | T}"] but that does not allow individual styling of the two nodes.
I have also tried creating an HTML record node which allows individual styling but increases complexity too much and requires users to know HTML.
Finally, I have tried to insert an invisible node inside the clusters to "simulate" the center and then use the invisible node for alignment. This unfortunately pushes G and T to far apart and doesn't achieve the intended effect.
digraph G {
graph [splines = false, ranksep = 0.2]
edge [style = "invis"]
node [shape = box, width = 7.5]
A B H
node [shape = circle, width = 1.5]
subgraph cluster_2 {
rank = same
C D E
}
subgraph cluster_1 {
G [shape = box]
T [shape = box]
}
A -> B
G -> T
B -> {C D E G}
{C D E T} -> H
}
The following gives the output:
In the picture above, the subgraph containing G and T is not aligned with the subgraph containing C,D,E.
Is there a way to vertically align the centers of the two subgraphs?
The problem is that GraphViz only align node rows and {C D E} is one row where as {G->T} is two rows so {C D E} can only be align with either G or T
You can however align {C D E} with the space between G and T though that is probably not what you want. E.g. like this:
digraph G {
graph [splines = false, ranksep = 0.2]
//edge [style = "invis"]
node [shape = box, width = 7.5]
A B H
node [shape = circle, width = 1.5]
subgraph cluster_2 {
rank = same
C D E
}
subgraph cluster_1 {
G [shape = box]
T [shape = box]
}
A -> B
G -> T[minlen=2]
B -> G
B -> {C D E} -> H [minlen=2]
T -> H
}
Resulting in:

Graphviz drawing two arrows intead of one

I have a strange result trying to make a simple graph using graphviz. Two arrows are drawn between 3 and 4 instead of one. I can't figure out why, if it is a bug or if I've done something wrong. Here is a minimal example, anything I remove makes the problem dissapear:
digraph dia {
newrank=true;
1 -> 2;
subgraph cluster_1 {
label = "1";
3 -> 5;
3 -> 6;
5 -> 6
}
subgraph cluster_2 {
label = "2";
4;
}
3 -> 4;
{rank="same"; 2; 5; 6;}
{rank="same"; 3; 4}
}
It produces the following output:
My actual graph is larger than that, and I need newrank property.
I think I have the lastest version of graphviz...
dot - graphviz version 2.40.1 (20161225.0304)
Thanks for any help !
I also noticed some strange things going on in graphviz when clusters are being used (especially in combination with rank=same. So probably a bug, yes.
In your situation simply adding a port to one of the problematic nodes fixes the problem:
3:e -> 4;

How to set head and tail position in nodes graphviz

I can not control an edge's head and tail position between 2 nodes.
I'm setting up a node like a graph below
digraph G{
node [shape = "box"]
a -> b
b -> a
}
This first part is basically the same answer that #marapet has given already:
digraph G
{
node[ shape = "box" ];
a:w -> b:w;
a:e -> b:e[ dir = back ];
}
It produces a graph with round edges:
If that's OK, then marapet's answer should be accepted.
If you insist on the shape that you provided in your post, a bit more complicated procedure needs to be applied:
digraph G
{
// we create the two nodes to be displayed
node[ shape = "box" ];
a b;
//we also create four empty nodes for routing the edges
node[ shape = point, height = 0 ];
x1 x2 x3 x4;
// we make sure that the nodes are arranged on the right levels
{ rank = same; x1 a x2 }
{ rank = same; x3 b x4 }
// we draw the edges one by one as the heads are varying
x1 -> a[ dir = none ];
a -> x2[ dir = back ];
x1 -> x3[ dir = none ];
x2 -> x4[ dir = none ];
b -> x4[ dir = none ];
x3 -> b;
}
This gives you
The head and tail position of an edge can be defined by using compass points as specified in The dot language, or by using the headport or tailport attributes:
digraph G{
node [shape = "box"]
a:w -> b:w
b -> a [headport=e, tailport=e]
}

Is it possible to have nested node shapes on Graphviz?

Is it possible to have nested shapes on Graphviz?
I would like to have a diamond inside a square, like the image bellow:
I got a solution:
subgraph cluster_1 {
margin = 1;
label = "";
associative_entity [shape = diamond, height = 1];
}
The Msquare shape might be close enough if your own solution doesn't work well for what you want.

Forcing edges on one side of a graph in DOT

In the following graph in DOT
graph {
rankdir=LR;
ranksep="1.0";
"v_1086" [label="A"];
"v_1023" [label="B"];
"v_1181" [label="C"];
"q_-888017763" [label="Q"];
"c_-961691100" [label="1"];
"c_-1161934348" [label="2"];
"c_556655049" [label="3"];
"v_1023"--"c_-961691100"[label="1.0"];
"v_1086"--"c_-1161934348"[label="1.0"];
"v_1181"--"c_556655049"[label="0.9925742574257426"];
"q_-888017763"--"v_1086" [color="green"];
"q_-888017763"--"v_1181" [color="indigo"];
"q_-888017763"--"v_1023" [color="khaki4"];
"c_-1161934348"--"c_-961691100"[label="1.0"];
"c_556655049"--"c_-1161934348"[label="0.10282888897110791"];
"c_556655049"--"c_-961691100"[label="0.006661529339994344"];
{rank = same;
"v_1086";
"v_1023";
"v_1181";
};
{rank = same;
"q_-888017763";
};
{rank = same;
};
{rank = same;
"c_-961691100";
"c_-1161934348";
"c_556655049";
};
}
How can I move the edges between the numerical nodes to the left.
The dot in the question renders as:
In that context, I don't understand the question: The numerical nodes are on the right, not the left, and I don't understand why you'd want to move the edges between them. Move them where?
Sorry this isn't an answer, but I can't embed an image in a comment.

Resources