Skip to content

Commit e697bb1

Browse files
committed
Update Graphviz lesson with further questions and examples on graph layouts.
1 parent 37d4853 commit e697bb1

9 files changed

+266
-0
lines changed

Graphviz/customTests.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,30 @@ test_subgraph_cluster <- function() {
120120
ok <- all(t1, t2)
121121
}, silent = TRUE)
122122
exists('ok') && isTRUE(ok)
123+
}
124+
125+
test_node_layout <- function() {
126+
# Get e
127+
e <- get('e', parent.frame())
128+
x <<- e$expr[1]
129+
# Test expression from user's script
130+
try({
131+
t1 <- grepl(pattern = "graph \\[.*layout = neato.*\\]", e$expr[1])
132+
t2 <- !grepl(pattern = "graph \\[.*layout = dot.*\\]", e$expr[1])
133+
ok <- all(t1, t2)
134+
}, silent = TRUE)
135+
exists('ok') && isTRUE(ok)
136+
}
137+
138+
test_node_layout_rankdir <- function() {
139+
# Get e
140+
e <- get('e', parent.frame())
141+
x <<- e$expr[1]
142+
# Test expression from user's script
143+
try({
144+
t1 <- grepl(pattern = "graph \\[.*rankdir = LR.*\\]", e$expr[1])
145+
t2 <- !grepl(pattern = "graph \\[.*rankdir = TB.*\\]", e$expr[1])
146+
ok <- all(t1, t2)
147+
}, silent = TRUE)
148+
exists('ok') && isTRUE(ok)
123149
}

Graphviz/graphviz_node_layout.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
node_layout <- grViz("
2+
digraph node_layout {
3+
4+
graph [layout = dot]
5+
6+
node [shape = circle,
7+
style = filled,
8+
color = grey]
9+
10+
node [fillcolor = red]
11+
a
12+
13+
node [fillcolor = green]
14+
b c d
15+
16+
node [fillcolor = orange]
17+
18+
edge [color = grey]
19+
a -> {b c d}
20+
b -> {e f g h}
21+
c -> {i j k l}
22+
d -> {m n o p}
23+
}")
24+
25+
print(node_layout)

Graphviz/graphviz_node_layout_circo.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
node_layout_circo <- grViz("
2+
digraph node_layout_circo {
3+
4+
graph [layout = circo]
5+
6+
node [shape = circle,
7+
style = filled,
8+
color = grey]
9+
10+
node [fillcolor = red]
11+
a
12+
13+
node [fillcolor = green]
14+
b c d
15+
16+
node [fillcolor = orange]
17+
18+
edge [color = grey]
19+
a -> {b c d}
20+
b -> {e f g h}
21+
c -> {i j k l}
22+
d -> {m n o p}
23+
}")
24+
25+
print(node_layout_circo)

Graphviz/graphviz_node_layout_twopi.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
node_layout_twopi <- grViz("
2+
digraph node_layout_twopi {
3+
4+
graph [layout = twopi]
5+
6+
node [shape = circle,
7+
style = filled,
8+
color = grey]
9+
10+
node [fillcolor = red]
11+
a
12+
13+
node [fillcolor = green]
14+
b c d
15+
16+
node [fillcolor = orange]
17+
18+
edge [color = grey]
19+
a -> {b c d}
20+
b -> {e f g h}
21+
c -> {i j k l}
22+
d -> {m n o p}
23+
}")
24+
25+
print(node_layout_twopi)

Graphviz/lesson.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,71 @@
220220
instead of a, b and c.
221221
Script: subgraph_cluster.R
222222

223+
- Class: text
224+
Output: Graphviz layouts can also be modified quite radically by making use of
225+
different Graphviz layouts. Graphviz provides four different layouts - 'dot',
226+
'neato', 'twopi' and 'circo'.
227+
228+
- Class: figure
229+
Output: You'll now see a completely new graph diagram displayed in the viewer.
230+
This graph uses the (default) Graphviz dot layout. The dot layout flows a
231+
directed graph in the direction of rank (i.e., downstream nodes of the same
232+
rank are aligned). By default, the direction is from top to bottom, but this
233+
can be changed with the 'rankdir' setting in the graph statement.
234+
Figure: graphviz_node_layout.R
235+
FigureType: new
236+
237+
- Class: script
238+
Output: Take a look at the provided script and note in the graph statement
239+
where the rank direction ('rankdir') is specified. This statement isn't
240+
really necessary in this case since top-to-bottom is the default rank
241+
direction, but has been included for the sake of illustration. Update the
242+
script to change the rank direction to be left-to-right ('LR').
243+
AnswerTests: test_node_layout_rankdir()
244+
Hint: Simply replace the rankdir 'TB' with 'LR' near the top of the graphViz
245+
specification where the graph statement appears.
246+
Script: node_layout_rankdir.R
247+
248+
- Class: text
249+
Output: See how the whole graph layout has now been rotated? Let's try one of
250+
the other Graphviz layouts now.
251+
252+
- Class: script
253+
Output: Update the provided script to change the graph layout from 'dot'
254+
to 'neato'.
255+
AnswerTests: test_node_layout()
256+
Hint: Simply replace 'dot' with 'neato' near the top of the graphViz
257+
specification where the graph layout is defined.
258+
Script: node_layout.R
259+
260+
- Class: text
261+
Output: Observe the new layout of the graph diagram. In this case, the result
262+
is a somewhat radial layout, but in general that is not always the case. The
263+
neato layout provides a class of layout sometimes referred to as a spring
264+
model layout. Essentially this layout attempts to minimize a global energy
265+
function, which is equivalent to statistical multi-dimensional scaling. This
266+
is usually a suitable layout if the graph is not too large (less than 100
267+
nodes) and you don't really know anything else in particular about it.
268+
269+
- Class: text
270+
Output: Let's now also have a look at the remaining two Graphviz layouts,
271+
again utilizing the same underlying graph in order to facilitate comparison
272+
with the two layout types we have seen already.
273+
274+
- Class: figure
275+
Output: This graph uses the 'twopi' layout. The twopi layout provides radial
276+
layouts. Nodes are placed on concentric circles depending their distance
277+
from a given root node.
278+
Figure: graphviz_node_layout_twopi.R
279+
FigureType: new
280+
281+
- Class: figure
282+
Output: And this graph uses the 'circo' layout, which is for circular layout
283+
results. This layout is usually suitable for certain diagrams with multiple
284+
cyclic structures, such as telecommunications networks.
285+
Figure: graphviz_node_layout_circo.R
286+
FigureType: new
287+
223288
- Class: text
224289
Output: Now that we've explored the main elements of Graphviz diagrams, in the
225290
next lesson we'll walk explore what's possible with Mermaid diagrams.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
node_layout <- grViz("
2+
digraph node_layout {
3+
4+
graph [layout = neato]
5+
6+
node [shape = circle,
7+
style = filled,
8+
color = grey]
9+
10+
node [fillcolor = red]
11+
a
12+
13+
node [fillcolor = green]
14+
b c d
15+
16+
node [fillcolor = orange]
17+
18+
edge [color = grey]
19+
a -> {b c d}
20+
b -> {e f g h}
21+
c -> {i j k l}
22+
d -> {m n o p}
23+
}")
24+
25+
print(node_layout)

Graphviz/scripts/node_layout.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
node_layout <- grViz("
2+
digraph node_layout {
3+
4+
graph [layout = dot]
5+
6+
node [shape = circle,
7+
style = filled,
8+
color = grey]
9+
10+
node [fillcolor = red]
11+
a
12+
13+
node [fillcolor = green]
14+
b c d
15+
16+
node [fillcolor = orange]
17+
18+
edge [color = grey]
19+
a -> {b c d}
20+
b -> {e f g h}
21+
c -> {i j k l}
22+
d -> {m n o p}
23+
}")
24+
25+
print(node_layout)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
node_layout_rankdir <- grViz("
2+
digraph node_layout_rankdir {
3+
4+
graph [layout = dot, rankdir = LR]
5+
6+
node [shape = circle,
7+
style = filled,
8+
color = grey]
9+
10+
node [fillcolor = red]
11+
a
12+
13+
node [fillcolor = green]
14+
b c d
15+
16+
node [fillcolor = orange]
17+
18+
edge [color = grey]
19+
a -> {b c d}
20+
b -> {e f g h}
21+
c -> {i j k l}
22+
d -> {m n o p}
23+
}")
24+
25+
print(node_layout_rankdir)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
node_layout_rankdir <- grViz("
2+
digraph node_layout_rankdir {
3+
4+
graph [layout = dot, rankdir = TB]
5+
6+
node [shape = circle,
7+
style = filled,
8+
color = grey]
9+
10+
node [fillcolor = red]
11+
a
12+
13+
node [fillcolor = green]
14+
b c d
15+
16+
node [fillcolor = orange]
17+
18+
edge [color = grey]
19+
a -> {b c d}
20+
b -> {e f g h}
21+
c -> {i j k l}
22+
d -> {m n o p}
23+
}")
24+
25+
print(node_layout_rankdir)

0 commit comments

Comments
 (0)