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]
}
Related
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:
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
I'd like to draw a diagram like this.
But the only diagram I can draw is:
The code I used :
graph [rankdir = LR]
node [shape=box]
x1;x2;x3;y1;y2;y3;y4;y5;y6;y7;y8;
node [shape=oval]
ind60;dem60;dem65
{x1,x2,x3} -> ind60[arrowhead=none arrowtail=normal dir=both]
{y1,y2,y3,y4} -> dem60[arrowhead=none arrowtail=normal dir=both]
dem65 -> {y5,y6,y7,y8}
ind60->dem60 dem60->dem65 ind60->dem65
How can I draw the desired plot?
A first step in what you want to achieve, using rank=same, invisible edges, groups, and constraint=false:
digraph {
node [shape=box]
{
rank=same;
y1;y2;y3;y4;
}
dem60[shape=oval];
{y1;y2;y3;y4} -> dem60 [dir=back];
{
rank=same;
x2 [group=left];
ind60[shape=oval];
dem65[shape=oval];
y6 [group=right];
x2 -> ind60 [dir=back];
ind60 -> dem65
dem65 -> y6;
}
// Invisible edges to order vertically node groups
edge[style=invis];
x1[group=left];
x3[group=left];
x1 -> x2 -> x3;
node[group=right];
y5 -> y6 -> y7 -> y8;
node[group=""]
edge[style=solid]
ind60->dem60
dem60->dem65
edge[constraint=false];
ind60 -> x1;
ind60 -> x3;
dem65 -> y5;
dem65 -> y7;
dem65 -> y8;
}
group enforces vertical alignement of nodes (of the same group).
rank=same makes nodes stay on the same rank.
Invisible edges enforce rank order within a vertical group.
constraint=false removes constraint calculation for some edges.
dir=back reverses displayed edge direction.
In Graphviz / dot, is it possible to get the edge to connect exactly in the top center of a node? Reading the dot guide, I thought tailport and headport would help me, but adding those make no difference and sometimes get me weirder results.
This is what I'm getting:
And this is what I'm looking for:
The code I used to get the (incorrect) graph is:
digraph G {
graph [splines = ortho];
node [shape = box];
edge [dir = none];
{
rank = same
A
AB [shape = point]
B
A -> AB
AB -> B
}
{
rank = same
point1 [shape = point]
point2 [shape = point]
point3 [shape = point]
}
AB -> point1
// The following section if to make the nodes appear in
// the correct order, not sure if there's a better way
{
edge [style = invisible]
rank = same
C
D
E
F
C -> D
D -> E
}
point2 -> point1
point2 -> C
point1 -> point3
point3 -> E
point1 -> D
}
Notes
splines=ortho doesn't support tailport or headport (see: "Graphviz Issue Tracker - 0002142: ortho plots do not respect ports. also arrowheads seem to go the wrong way.")
You can use hidden nodes, but don't use them on the lateral nodes (like C or F on the example below)
Image
Code
This code works even with more than 3 child nodes and it's compatible with Graphviz 2.38. Useful for Org Chart (even if it's not perfect if you have many levels - I'm still try to reduce asymmetries).
graph {
splines=ortho;
{0, 1, 2, 3 [width=0, shape=point, style=invis];}
{rank=same; 1 -- 2 -- 3;}
0 -- 2;
node [shape=box];
{rank=same; A -- 0 -- B;}
1 -- C;
1 -- D;
3 -- E;
3 -- F;
}
Turns out the most recent (2.38) version isn't working properly in Mac OS X Yosemite, I had to downgrade to 2.36 as noted in the download page.
I see the same effect under Windows7 - did not try other platforms.
I tried fiddling with the 'port positions', See http://www.graphviz.org/docs/attr-types/portPos/
e.g. changing the last lines of your code to
point2 -> point1
point2 -> C:nw
point1 -> point3
point3 -> E:ne
point1 -> D
gives a (slightly) better positioning of the connecting lines.
For example:
digraph G {
rankdir = TB;
subgraph A {
a -> {a0, a1};
};
subgraph B {
b -> {b0, b1, b2};
};
};
I want to put B at the bottom of A, how to do it?
You'll have to tell graphviz that the subgraphs are not equal. One way to do it is simply to add invisible edges between the graphs:
digraph G {
rankdir = TB;
subgraph A {
a -> {a0, a1};
};
subgraph B {
b -> {b0, b1, b2};
};
a0 -> b [style=invis];
a1 -> b [style=invis];
};
By adding two invisible edges the subgraphs are neatly aligned.