## Plotting networks and marginal distributions with the Rgraphviz package

Commercial and free software suits implementing Bayesian network modelling typically display a Bayesian network as:

- a nicely laid-out graph, with nodes positioned according to the topological ordering of the network (root nodes on top, leaves at the bottom);
- marginal distributions summarized with key parameters in each node.

The aim of this kid of plot is to summarize the structure and the parameters of the Bayesian network in a single plot that can also be used to compare the effects of inference (incorporating evidence, performing interventions, counterfacturals, etc.). For discrete Bayesian networks, this means showing the marginal probabilities of all the values taken by each node.

We can producte this kind of plot in **bnlearn** using the `graphviz.chart()`

function
(documented here). In its simplest form, it takes an object of
class `bn.fit`

and produces a greyscale barplot.

> library(bnlearn) > dag = model2network("[A][C][F][B|A][D|A:C][E|B:F]") > fitted = bn.fit(dag, learning.test) > graphviz.chart(fitted)

`graphviz.chart()`

has several groups of options to customize how the plot looks:

- the
`layout`

of the graph, as in`graphviz.plot()`

(see some examples); - the
`type`

of plot to use for the marginal distributions:`"barchart"`

,`"dotplot"`

or`"barprob"`

; - whether to display the labels of the values of each variable (
`draw.levels`

), a grid of reference values (`grid`

), and the aspect ratio of the node (`scale`

); - various colours like
`col`

(node frame colour),`bg`

(node background colour),`text.col`

(colour of the labels, including the node label),`bar.col`

(colour of the bars in a barchart, of the lines in a dotplot), and`strip.bg`

(the background colour of the strip containing the node label).

By default, `graphviz.chart()`

produces barcharts like that shown above. We can overlay the probabilities
represented by the bars on the barcharts by changing the `type`

of the plot to ```
type =
"barprob"
```

.

> graphviz.chart(fitted, type = "barprob")

Changing the `type`

from `type = "barchart"`

to `type = "dotplot"`

replaces the bars
with lines and points resembling the plots produced by `dotplot`

in the **lattice** package.

> graphviz.chart(fitted, type = "dotplot")

And we can also add a grid by setting `grid = TRUE`

, in which case grid lines are placed at
`c(0, 0.25, 0.50, 0.75)`

; or by assigning their locations manually.

> graphviz.chart(fitted, type = "dotplot", grid = c(0, 0.1, 0.2, 0.4, 0.8))

Finally, we can add some colour to the plot with the arguments mentioned in the last bullet point above. Note that colour setting apply to all nodes: it is currently not possible to use a different set of colours for a particular node.

> graphviz.chart(fitted, type = "barprob", grid = TRUE, bar.col = "darkgreen", + strip.bg = "lightskyblue")

`Tue Aug 24 09:51:52 2021`

with **bnlearn**

`4.7-20210803`

and `R version 4.1.1 (2021-08-10)`

.