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...) -> CausalGraphPlotVisualize 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.
layout::Union{Symbol,AbstractVector,AbstractDict,Makie.Automatic} = Makie.automatic: node position layout; seelayout.layout_kwargs::NamedTuple = (;): extra keywords forwarded to the layout algorithm.edge_paths::Union{AbstractDict,Nothing} = nothing: explicit waypoints overriding an edge's drawn route.title::Union{AbstractString,Nothing} = nothing: figure title;Makie.plotonly.title_fontsize::Union{Real,Nothing} = nothing: figure title font size;Makie.plotonly.title_color = nothing: figure title color;Makie.plotonly.title_gap::Real = 4.0: gap between the title and the plot;Makie.plotonly.outer_margin::Real = 16: outer margin around the figure, in pixels;Makie.plotonly.fig_size::NTuple{2,Real} = (600.0, 450.0): figure size in pixels;Makie.plotonly.stretch_to_fig_size::Bool = false: stretch the plot to fillfig_size;Makie.plotonly.kwargs...: any other reactive attribute; seecausalgraphplotfor the full list.
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.FigureAxisPlotusing 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)CausalStructures.layout — Function
layout(cg::CausalGraph, method::Symbol = :stress; kwargs...)Compute 2-D node positions for cg, for use in plotting.
method | Algorithm | Requires |
|---|---|---|
:spring | Fruchterman-Reingold force-directed | NetworkLayout |
:stress | Stress majorization | NetworkLayout |
:sfdp | Scalable Force-Directed Placement | NetworkLayout |
:spectral | Spectral layout | NetworkLayout |
:shell | Concentric shells | NetworkLayout |
:squaregrid | Square grid | NetworkLayout |
:sugiyama | Layered/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.
kwargs...: forwarded to the underlying algorithm (e.g.seed,iterations).
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)MakieExt.CausalGraphPlot — Type
CausalGraphPlotThe plot type for causalgraphplot/causalgraphplot!, and the type returned by Makie.plot/Makie.plot! on a CausalGraph.
MakieExt.causalgraphplot — Function
causalgraphplot(cg::CausalGraph; kwargs...) -> CausalGraphPlot
causalgraphplot!(ax, cg::CausalGraph; kwargs...) -> CausalGraphPlotRecipe-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.
kwargs...: any of the attributes listed below.
A CausalGraphPlot.
Layout
| Keyword | Default | Controls |
|---|---|---|
layout | Makie.automatic | node position layout; see layout |
layout_kwargs | (;) | extra keywords forwarded to the layout algorithm |
Node styling
| Keyword | Default | Controls |
|---|---|---|
node_color | :white | fill color |
node_strokecolor | :black | border color |
node_strokewidth | 2.0 | border line width |
node_linestyle | nothing | border line style (nothing = solid) |
node_shape | :circle | :circle, :square, :ellipse, or :rect |
node_radius | nothing | fixed node radius; nothing sizes each node to its label |
node_padding | 10.0 | label clearance used when sizing a node automatically |
arrow_size | nothing | arrowhead size; nothing scales with node size |
circle_size | nothing | bidirected/undirected-edge circle marker size; nothing scales with node size |
Edge styling
| Keyword | Default | Controls |
|---|---|---|
edge_color | inherits theme's linecolor | line/marker color |
arrow_fill | nothing | arrowhead fill color; nothing matches edge_color |
linewidth | 1.5 | line width |
edge_linestyle | nothing | line style (nothing = solid) |
curvature | nothing | how far an edge bows; also disables automatic routing around other nodes |
edge_paths | nothing | explicit 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
| Keyword | Default | Controls |
|---|---|---|
node_labels | nothing | per-node label text; defaults to the node's name |
node_label_color | :black | node label text color |
node_label_fontsize | 14.0 | node label font size |
node_label_font | :regular | node label font |
edge_labels | nothing | per-edge label text |
edge_label_color | inherits theme's textcolor | edge label text color |
edge_label_fontsize | 12.0 | edge label font size |
edge_label_font | :regular | edge label font |
edge_label_shift | 0.5 | position along the edge, 0 (source) to 1 (destination) |
edge_label_distance | nothing | perpendicular gap from the edge; nothing scales with edge_label_fontsize |
edge_label_rotation | nothing | label angle; nothing follows the edge's own angle |
See the Plotting page for styling precedence rules, examples, and runnable code (under Makie.plot).
MakieExt.causalgraphplot! — Function
causalgraphplot!(ax, cg::CausalGraph; kwargs...) -> CausalGraphPlotMutating variant of causalgraphplot; draws into an existing ax.
ax: the axis to draw into.cg::CausalGraph: the graph to plot.
kwargs...: any of the attributes documented incausalgraphplot.
A CausalGraphPlot.
See Makie.plot for runnable examples.