Plotting

Plotting is provided by an extension that loads when a Makie backend is loaded. Node placement additionally requires NetworkLayout.jl or Sugiyama.jl (or bring your own layout). See the Plotting guide for full examples.

Makie.plot — Method
Makie.plot(cg; kwargs...) -> FigureAxisPlot
Makie.plot!(ax, cg; kwargs...) -> CausalGraphPlot

Visualize a CausalGraph using Makie. Requires loading a Makie backend (e.g. CairoMakie) before calling.

Every keyword below (except the figure-level ones noted last) is one of its reactive attributes, so e.g. plt.node_color[] = :red restyles the existing plot in place.

See causalgraphplot for the full list of attributes, all of which are shared by both plot and plot!. Style keywords accept either a scalar (applied to everything) or a Dict for per-node/per-edge overrides; a per-edge Dict may be keyed by a CausalEdge, a (src, dst) tuple, an edge-type symbol, or :default. layout_kwargs is a NamedTuple of extra keywords forwarded to the chosen layout algorithm, e.g. layout_kwargs = (; seed = 1).

title, title_fontsize, title_color, title_gap, outer_margin, fig_size, stretch_to_fig_size only apply to Makie.plot.

For a project-wide default, use a Makie theme, e.g. Makie.set_theme!(CausalGraphPlot = (node_color = :lightblue,)). edge_color and edge_label_color also inherit the active theme's linecolor/textcolor; node_color/node_label_color stay fixed and should be set together if changed.

edge_paths can be used to override an edge's drawn route directly: a Dict (same keying as other per-edge overrides) from an edge to a vector of waypoints running from its source node's position to its destination node's position, in the same coordinates as layout.

See the Plotting page for the full keyword reference, styling precedence rules, and examples.

  • ax: the axis to draw into; Makie.plot! only.
  • cg::CausalGraph: the graph to plot.

A FigureAxisPlot (for Makie.plot) or a CausalGraphPlot (for Makie.plot!).

julia> using CairoMakie

julia> dag = DAG("A --> B");

julia> fig = Makie.plot(dag);

julia> typeof(fig)
Makie.FigureAxisPlot
using CairoMakie

dag = DAG("A ---> X, A ---> Y, X ---> Y")

Makie.plot(dag; node_color = :lightblue, edge_color = :gray40)
Makie.plot(dag; edge_color = Dict((:A, :X) => :red, :default => :black))
Makie.plot(dag; edge_color = Dict(directed(:A, :X) => :red, :default => :black))
Makie.plot(dag; node_shape = Dict(:A => :square, :default => :circle))
Makie.plot(dag; node_shape = :rect, node_labels = Dict(:A => "Age at\nbaseline"))
Makie.plot(dag; curvature = Dict((:A, :Y) => 0.3))
Makie.plot(dag; title = "My DAG", layout = :spring, layout_kwargs = (; seed = 1))

# Compose two graphs into one figure:
fig = Figure()
Makie.plot!(Axis(fig[1, 1]; aspect = DataAspect()), dag)
Makie.plot!(Axis(fig[1, 2]; aspect = DataAspect()), dag; node_color = :salmon)
Makie.hidedecorations!.(fig.content)
Makie.hidespines!.(fig.content)
source
CausalStructures.layout — Function
layout(cg::CausalGraph, method::Symbol = :stress; kwargs...)

Compute 2-D node positions for cg, for use in plotting.

methodAlgorithmRequires
:springFruchterman-Reingold force-directedNetworkLayout
:stressStress majorizationNetworkLayout
:sfdpScalable Force-Directed PlacementNetworkLayout
:spectralSpectral layoutNetworkLayout
:shellConcentric shellsNetworkLayout
:squaregridSquare gridNetworkLayout
:sugiyamaLayered/hierarchical (DAG only)Sugiyama

The default is :stress, except for a DAG with Sugiyama loaded, where it is :sugiyama. Extra kwargs are forwarded to the underlying algorithm.

  • cg::CausalGraph: the graph to lay out.
  • method::Symbol = _default_layout_method(cg): the layout algorithm to use, one of :spring, :stress, :sfdp, :spectral, :shell, :squaregrid, or :sugiyama.

A Dict{Symbol,NTuple{2,Float64}} mapping each node name to its (x, y) position.

julia> using NetworkLayout

julia> dag = DAG("A --> X, A --> Y, X --> Y");

julia> layout(dag, :spring; seed = 1)
Dict{Symbol, Tuple{Float64, Float64}} with 3 entries:
  :A => (-1.21358, -0.442569)
  :X => (0.283814, 1.32745)
  :Y => (1.06799, -0.854346)
using NetworkLayout

dag = DAG("A ---> X, A ---> Y, X ---> Y")

layout(dag)
layout(dag, :spring)
layout(dag, :spring; seed = 1405, iterations = 200)

positions = layout(dag, :spring)
positions[:A] = (0.0, 2.0)

using CairoMakie
plot(dag; layout = positions)
source
MakieExt.causalgraphplot — Function
causalgraphplot(cg::CausalGraph; kwargs...) -> CausalGraphPlot
causalgraphplot!(ax, cg::CausalGraph; kwargs...) -> CausalGraphPlot

Recipe-native aliases for Makie.plot/Makie.plot! on a CausalGraph. See that docstring for composing into an existing figure and the figure-level keywords (title, fig_size, ...) that only Makie.plot accepts.

  • cg::CausalGraph: the graph to plot.

A CausalGraphPlot.

Layout

KeywordDefaultControls
layoutMakie.automaticnode position layout; see layout
layout_kwargs(;)extra keywords forwarded to the layout algorithm

Node styling

KeywordDefaultControls
node_color:whitefill color
node_strokecolor:blackborder color
node_strokewidth2.0border line width
node_linestylenothingborder line style (nothing = solid)
node_shape:circle:circle, :square, :ellipse, or :rect
node_radiusnothingfixed node radius; nothing sizes each node to its label
node_padding10.0label clearance used when sizing a node automatically
arrow_sizenothingarrowhead size; nothing scales with node size
circle_sizenothingbidirected/undirected-edge circle marker size; nothing scales with node size

Edge styling

KeywordDefaultControls
edge_colorinherits theme's linecolorline/marker color
arrow_fillnothingarrowhead fill color; nothing matches edge_color
linewidth1.5line width
edge_linestylenothingline style (nothing = solid)
curvaturenothinghow far an edge bows; also disables automatic routing around other nodes
edge_pathsnothingexplicit waypoints overriding an edge's drawn route

Edge styling keywords accept either a scalar or a Dict for per-edge overrides, keyed by a CausalEdge, a (src, dst) tuple, an edge-type symbol, or :default; node styling keywords accept a scalar or a Dict keyed by node name, with :default as a fallback.

Labels

KeywordDefaultControls
node_labelsnothingper-node label text; defaults to the node's name
node_label_color:blacknode label text color
node_label_fontsize14.0node label font size
node_label_font:regularnode label font
edge_labelsnothingper-edge label text
edge_label_colorinherits theme's textcoloredge 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 from the edge; nothing scales with edge_label_fontsize
edge_label_rotationnothinglabel angle; nothing follows the edge's own angle

See the Plotting page for styling precedence rules, examples, and runnable code (under Makie.plot).

source