Plotting

To use plotting, first load a Makie backend. For node placement, you can use NetworkLayout.jl, Sugiyama.jl, or provide your own layout. Below we use CairoMakie:

using CausalStructures
using CairoMakie
using NetworkLayout
using Sugiyama
General-purpose by design

There are many conventions for drawing causal graphs. For example, you might box conditioned variables, dash latent variables, draw <-> as a dashed arc, or use different colors for exposures and outcomes.

plot does not impose any of these conventions by default. Instead, you can use its styling options, Makie themes, and layouts to choose the conventions that make sense for your application[1].

Basic usage

You can pass any CausalGraph to plot, and all edge marks are supported:

unknown = UNKNOWN("A <-> B o-> C o-- A")
plot(unknown)

As an example, here is the DAG from Figure 6.5 of Peters et al. (2017):

dag = DAG(
    "C --> X, A --> X + K, X --> F + D, K --> Y, D --> Y + G, Y --> H"
)
plot(dag)

plot returns a FigureAxisPlot, so fig, ax, plt = plot(dag) gives you the Figure, Axis, and plot object. The plot is reactive, so you can update it after creating it:

fig, ax, plt = plot(dag)
plt.node_color[] = :salmon
fig

There are four main areas of styling:

If you want to set defaults for an entire project, you can use a Makie theme:

Makie.set_theme!(CausalGraphPlot = (node_color = :lightblue, linewidth = 2))

edge_color and edge_label_color also pick up the active theme's linecolor and textcolor.

Per-node and per-edge styling

Every node, edge, and label keyword accepts either a single value, which applies to all nodes or edges, or a Dict that sets it per node or per edge. Nodes and edges the Dict doesn't mention keep their default, unless you add a :default key:

plot(dag;
    node_color = Dict(:Y => :lightskyblue),
    edge_color = Dict((:K, :Y) => :crimson, :default => :gray60),
)

Only Y is filled blue; the other nodes keep the default white. The K --> Y edge is crimson, and the :default key turns every other edge gray. The sections below list the keys each Dict accepts.

Layout

The layout keyword controls where the nodes are placed. For DAGs, it defaults to :sugiyama if Sugiyama.jl is loaded, and to :stress otherwise.

plot(dag; layout = :spring)

You can use these shorthand names for the most common layouts. All except :sugiyama come from NetworkLayout.jl, while :sugiyama comes from Sugiyama.jl:

layoutAlgorithm
:springFruchterman-Reingold force-directed
:stressStress majorization
:sfdpScalable Force-Directed Placement
:spectralSpectral layout
:shellConcentric shells
:squaregridSquare grid
:sugiyamaSugiyama layout (DAGs only)

If you want to place the nodes yourself, you can pass explicit positions instead of a Symbol. You can either use a Dict of (x, y) pairs keyed by node name, or a Vector in the order returned by nodes(cg):

plot(dag; layout = Dict(
    :A => (0, 1), :C => (0, -1), :K => (1, 1), :X => (1, -1),
    :D => (2, -1), :F => (2, -2), :Y => (3, 0), :G => (3, 1), :H => (4, 0),
))
Tweaking a layout by hand

You can compute a starting layout using layout, and then manually adjust a few nodes:

positions = layout(dag, :spring)
positions[:A] = (0.0, 2.0)
plot(dag; layout = positions)
Sugiyama positions without Sugiyama routing

Sugiyama.jl implements its own routing using dummy nodes. If you prefer the automatic Bezier-curve routing used by plot, you can compute the Sugiyama positions with layout and pass those instead:

dag_layered = DAG("A --> X, A --> B, X --> Y, B --> Y, A --> Y")
plot(dag_layered; layout = layout(dag_layered, :sugiyama))

Styling nodes

A per-node Dict is keyed by node name, with :default as a fallback.

KeywordDefaultControls
node_color:whitefill color
node_strokecolor:blackborder color
node_strokewidth2.0border line width, drawn outside the node
node_linestylenothing (solid)border line style
node_shape:circlenode outline shape
node_radiusnothing (text-fit)size of each node, inside its border
node_padding10.0clearance around each label when node_radius is nothing

Combine color, border, and shape to highlight a node:

plot(dag;
    node_color = Dict(:A => :salmon, :default => :lightblue),
    node_strokecolor = Dict(:A => :crimson, :default => :navy),
    node_shape = Dict(:K => :square, :default => :circle),
)

You can set node_shape to :circle (the default), :square, :ellipse, or :rect. The latter two are mainly useful when you want to fit an oblong label. You can also use node_linestyle to style the border, for example to mark a latent variable:

plot(dag;
    node_linestyle = Dict(:X => :dash),
    node_strokecolor = Dict(:X => :gray50, :default => :black),
)

If you only want to draw the node names, you can make the fill transparent and remove the border. Edges still stop at the (now invisible) node boundary, so you can use :rect with a smaller node_padding to bring the arrowheads closer to the text:

plot(dag;
    node_color = :transparent,
    node_strokewidth = 0,
    node_shape = :rect,
    node_padding = 4,
)

Text-fit node sizing

By default (node_radius = nothing), nodes are sized to fit their labels:

longlabels = DAG("Exposure --> Mediator --> Y_outcome")
plot(longlabels; node_shape = Dict(:Exposure => :ellipse))

If you want to control the size yourself, you can set node_radius explicitly:

plot(dag; node_radius = 0.06)

Styling edges

A per-edge Dict can use the following keys, checked in this order:

  1. a CausalEdge for one exact edge, e.g. bidirected(:X, :Y)
  2. a (src, dst) tuple for the node pair, in either order
  3. an edge-type symbol (:directed, :undirected, :bidirected, :partially_directed, :partially_undirected, :partial)
  4. :default as a fallback
KeywordDefaultControls
edge_colortheme's linecolor (:black)line / marker color
arrow_fillnothingarrowhead fill color
linewidth1.5line width
edge_linestylenothing (solid)line style
edge_gap0.0gap (pixels) at edge ends
curvaturenothinghow far the edge bows
edge_pathsnothingexplicit waypoints for an edge's route
arrow_sizenothing (0.4 × average node radius)length of arrowhead triangles
circle_sizenothing (0.28 × average node radius)radius of open-circle (o) endpoints

Let's style some edges by type:

admg = ADMG("X --> Y, X <-> Z, Z --> Y")

plot(admg;
    edge_color = Dict(:directed => :steelblue, :bidirected => :crimson),
    linewidth  = Dict(:bidirected => 2.5, :default => 1.5),
)

arrow_fill controls the arrowhead's fill color. By default (nothing), it uses the edge's resolved edge_color, so arrowheads are solid. If you want hollow, outline-only arrowheads, pass a transparent color:

plot(dag; arrow_fill = :transparent)

edge_linestyle styles the edge line itself. For example, you can use it to dash <-> edges:

plot(admg; edge_linestyle = Dict(:bidirected => :dash))

If you prefer some space between each end of an edge and the node border, you can set edge_gap:

plot(dag; edge_gap = 4)

Targeting specific edges

If you want to change something for a specific edge, you can use a tuple key:

plot(dag;
    edge_color = Dict((:A, :X) => :red, :default => :black),
)

A tuple key uses an unordered node pair. This means that if an ADMG has both X --> Y and X <-> Y, the tuple key applies to both edges. If you need to distinguish them, use a CausalEdge instead:

shared = ADMG("X --> Y, X <-> Y")

plot(shared;
    edge_color = Dict(bidirected(:X, :Y) => :crimson, :default => :steelblue),
)
Symmetric edges

For symmetric edges, the order does not matter, so bidirected(:Y, :X) is the same key as bidirected(:X, :Y).

Notice that the two edges between X and Y are automatically curved apart, so they don't overlap.

Curvature and automatic routing

curvature bows an edge into an arc instead of drawing it straight. Positive values bow the edge to the left when travelling from src to dst, while negative values bow it to the right.

plot(admg; curvature = Dict(:bidirected => -0.3))

If a straight src --> dst edge would pass too close to a non-incident node, plot automatically bends it around that node with a Bezier curve. Edges with nothing in the way are always drawn straight.

detour = DAG("A --> X + Y, X --> Y")

plot(detour; layout = [(0, 0), (1, 0), (2, 0)])

If you want to disable this automatic routing for a particular edge, you can set its curvature to 0.0:

plot(detour;
    layout = [(0, 0), (1, 0), (2, 0)],
    curvature = Dict(directed(:A, :Y) => 0.0),
)

Explicit edge paths

Edges are normally drawn as straight lines unless automatic routing is needed. If you want to control the path yourself, you can use edge_paths and provide intermediate points for the edge. For example, the edge K --> Y below is drawn through the point (0.5, -0.25):

positions = layout(dag, :spring)

plot(dag;
    layout = positions,
    edge_paths = Dict((:K, :Y) => [positions[:K], (0.5, -0.25), positions[:Y]]),
)

Labels and titles

Labels

Node label keywords use the same Dict keys as node styling.

KeywordDefaultControls
node_labelsnothingtext drawn in each node
node_label_color:blacknode label text color
node_label_fontsize14.0node label font size
node_label_font:regularnode label font

By default, each node is labelled with its own name. If you want different labels, you can set them with node_labels. Node sizing takes multi-line labels into account, so the nodes grow to fit:

plot(DAG("A0 --> L1 --> A1 --> Y, A0 --> Y + A1");
    node_labels = Dict(
        :A0 => "Treatment\nat baseline",
        :L1 => "Confounder\nat time 1",
        :A1 => "Treatment\nat time 1",
    ),
)

You can use node_label_color and node_label_fontsize to style the label text:

plot(dag;
    node_label_color = Dict(:A => :crimson, :default => :black),
    node_label_fontsize = 18,
)

Edge labels

Edge label keywords use the same Dict keys as edge styling.

KeywordDefaultControls
edge_labelsnothingtext drawn along each edge
edge_label_colortheme's textcolor (:black)edge label text color
edge_label_fontsize12.0edge label font size
edge_label_font:regularedge label font
edge_label_shift0.5position along the edge, 0 (source) to 1 (destination)
edge_label_distancenothingperpendicular gap (pixels) from the edge; nothing scales with edge_label_fontsize
edge_label_rotationnothingtext angle in radians; nothing follows the edge's own angle

Let's add a label to the edge K --> Y:

plot(dag; edge_labels = Dict(directed(:K, :Y) => "hello"))

By default, the label follows the edge's angle. You can use edge_label_shift and edge_label_distance to move it along or away from the edge:

plot(dag;
    edge_labels = Dict(directed(:K, :Y) => "hi"),
    edge_label_shift = 0.75,
    edge_label_distance = 12,
)

If an edge is steep or curved, following its angle can make the label hard to read. You can use edge_label_rotation to give it a fixed angle instead:

plot(dag;
    edge_labels = Dict(directed(:A, :X) => "steep", directed(:K, :Y) => "follows"),
    edge_label_rotation = Dict(directed(:A, :X) => 0.0),
    edge_label_distance = Dict(directed(:A, :X) => 20),
    edge_label_color = Dict(directed(:A, :X) => :crimson),
)

Titles

KeywordDefaultControls
titlenothingplot title; nothing means no title
title_fontsizenothingtitle font size; nothing uses the Makie theme's default
title_colornothingtitle color; nothing uses the Makie theme's default
title_gap4.0gap (points) between the title and the graph

If you want to add a title, set title:

plot(dag; title = "My DAG", title_fontsize = 20, title_color = :navy)

Figure size and margins

KeywordDefaultControls
outer_margin16padding (pixels) around the whole figure
fig_size(600, 450)figure size in pixels (width, height)
stretch_to_fig_sizefalsestretch the layout to fill an uneven fig_size

Let's make the figure a bit larger:

plot(dag; fig_size = (800, 600))
Large graphs need a bigger `fig_size`

The default (600, 450) works well for small examples. As your graph gets larger, labels and edges can become cramped or overlap. Increase fig_size (and, if needed, node_radius or node_label_fontsize) to keep larger graphs readable.

Uneven `fig_size` and `stretch_to_fig_size`

By default, node positions keep the layout's own aspect ratio. Depending on your fig_size, this can leave a lot of empty space around the graph. If you want the layout to fill the figure instead, set stretch_to_fig_size = true.

Composing into an existing figure

So far we've only used plot, which builds its own Figure and Axis for you. If you already have an Axis (for example, as one panel of a larger figure), you can use plot!(ax, cg; kwargs...) to draw the graph there instead. It accepts the same keywords as plot, except for outer_margin, title_gap, fig_size, and stretch_to_fig_size, since those control the figure that plot creates.

Let's use this to draw the DAG and the ADMG side by side in one figure:

fig = Figure(size = (900, 400))
plot!(Axis(fig[1, 1]; aspect = DataAspect()), dag)
plot!(Axis(fig[1, 2]; aspect = DataAspect()), admg; node_color = :salmon)
Makie.hidedecorations!.(fig.content)
Makie.hidespines!.(fig.content)
fig

Combining options

Here we combine some of the styling options above to plot a PAG:

pag = PAG(
    "C o-> X, D --> G + Y, X --> D + F, Y --> H, K o-> X, K --> Y"
)

plot(
    pag;
    layout           = :spring,
    node_color       = Dict(:X => :skyblue, :Y => :gold, :default => :whitesmoke),
    node_strokecolor = Dict(:X => :royalblue, :Y => :darkorange, :default => :slategray),
    node_shape       = Dict(:K => :square, :default => :circle),
    node_linestyle   = Dict(:K => :dash, :default => nothing),
    edge_color       = Dict(:partially_directed => :royalblue, :default => :darkslategray),
    edge_linestyle   = Dict(:partially_directed => :dash),
    curvature        = Dict((:K, :Y) => 0.3),
    edge_labels      = Dict((:K, :X) => "cool"),
    node_label_color = Dict(:X => :navy, :Y => :saddlebrown, :default => :black),
    title            = "A cool PAG",
    title_fontsize   = 18,
    title_color      = :navy,
    fig_size         = (700, 500),
)
  • 1Makie themes for common conventions are also welcome as PRs!