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.
Examples
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)CausalStructures.layout — Function
layout(cg::CausalGraph, method::Symbol = :stress; kwargs...)Compute 2-D node positions for cg and return them as a Dict{Symbol,NTuple{2,Float64}} keyed by node name.
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.
Examples
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
CausalGraphPlot is the plot type associated with plotting function causalgraphplot. Check the docstring for causalgraphplot for further information.
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 the full keyword reference.
Plot type
The plot type alias for the causalgraphplot function is CausalGraphPlot.
Attributes
arrow_fill = nothing — Arrowhead fill color; defaults to the edge's own color.
arrow_size = nothing — Arrowhead size; defaults to a fraction of the typical node radius.
circle_size = nothing — Bidirected/undirected-edge circle marker size; defaults to a fraction of the typical node radius.
curvature = nothing — Per-edge curvature amount for curved edge routing.
edge_color = @inherit linecolor :black — Edge line color; inherits the active theme's linecolor by default.
edge_label_color = @inherit textcolor :black — Edge label text color; inherits the active theme's textcolor by default.
edge_label_distance = nothing — Perpendicular offset (in pixels) of an edge label from its edge; defaults to the label's own font size.
edge_label_font = :regular — Edge label font.
edge_label_fontsize = 12.0 — Edge label font size.
edge_label_rotation = nothing — Edge label rotation angle; defaults to following the edge's local tangent.
edge_label_shift = 0.5 — Fractional position of an edge label along its path (0 = source, 1 = destination).
edge_labels = nothing — Per-edge label text.
edge_linestyle = nothing — Edge line style.
edge_paths = nothing — Explicit waypoints overriding an edge's drawn route.
layout = Makie.automatic — Node position layout: Makie.automatic, a layout method Symbol (see layout), or precomputed positions.
layout_kwargs = (;) — Extra keywords forwarded to the chosen layout algorithm.
linewidth = 1.5 — Edge line width.
node_color = :white — Node fill color.
node_label_color = :black — Node label text color.
node_label_font = :regular — Node label font.
node_label_fontsize = 14.0 — Node label font size.
node_labels = nothing — Per-node label text; defaults to the node's name.
node_linestyle = nothing — Node outline line style.
node_padding = 10.0 — Padding around a node's label used when sizing the node automatically.
node_radius = nothing — Fixed node radius, overriding automatic label-fit sizing.
node_shape = :circle — Per-node marker shape: :circle, :square, :ellipse, or :rect.
node_strokecolor = :black — Node outline color.
node_strokewidth = 2.0 — Node outline width.
MakieExt.causalgraphplot! — Function
causalgraphplot! is the mutating variant of plotting function causalgraphplot. Check the docstring for causalgraphplot for further information.