| make_tikz | R Documentation |
Generates LaTeX TikZ code from a Disco, Knowledge, or
caugi::caugi object, preserving node positions, labels, and visual styles.
Edges are rendered with arrows, line widths, and colors.
The output is readable LaTeX code that can be
directly compiled or modified.
make_tikz(
x,
...,
style = c("default", "classic"),
scale = 10,
full_doc = TRUE,
bend = NULL,
tier_label_pos = c("above", "below", "left", "right"),
node_labels = NULL,
tier_labels = NULL,
bend_edges = lifecycle::deprecated(),
bend_angle = lifecycle::deprecated()
)
The function calls plot() to generate a caugi::caugi_plot object, then
traverses the plot object's grob structure to extract nodes and
edges. Supported features include:
Nodes
Fill color and draw color (supports both named colors and custom RGB values)
Font size
Coordinates are scaled by the scale parameter
Edges
Line color and width
Arrow scale
Optional bending to reduce overlapping arrows
The output starts with named styles (var for nodes, link for edges,
tier and tier label for tiers) for easy restyling, and edges are
connected to nodes by name rather than coordinates.
With a positive bend (or default), edges bend to the left when they
point to the right and vice versa, so they bulge upwards; with
style = "classic" they always bend to the right. A negative angle bends
them the other way. Edges in both directions (A --> B and B --> A) are
bent to opposite sides, by that angle, or if it is 0, by 25 degrees
(10 for style = "classic").
Like plot(), edges that would pass through another node are bent around
it. A bent edge that would pass through a node is bent the other way
instead, or, if that also hits a node, by the angle closest to its bend
that clears all nodes. Bent edges that leave a node in nearly the
same direction are spread apart, by bending them the other way or more.
Node sizes are estimated from the labels, so a bend may need a manual
tweak.
To draw edges exactly as requested, turn both adjustments off with
edge_style = list(route = FALSE), or for single edges with
edge_style$by_edge. Edges given in a bend list are always drawn exactly
as given.
Adds a time axis style, and the spacing is set by the x and y options
of the tikzpicture. Tier labels go on the time axis: "below" (default)
or "above" for column tiers, "left" (default) or "right" for row
tiers.
A character string containing LaTeX TikZ code. Depending on
full_doc, this is either:
a complete LaTeX document (full_doc = TRUE), or
only the tikzpicture environment (full_doc = FALSE).
################# Convert Knowledge to Tikz ################
data(num_data)
kn <- knowledge(
num_data,
X1 %-->% X2,
X2 %!-->% X3 + Y,
Y %!-->% Z
)
# Full standalone document
tikz_kn <- make_tikz(kn, scale = 10, full_doc = TRUE)
cat(tikz_kn)
# Only the tikzpicture environment
tikz_kn_snippet <- make_tikz(kn, full_doc = FALSE)
cat(tikz_kn_snippet)
# With bent edges
tikz_bent <- make_tikz(
kn,
full_doc = FALSE,
bend = 25
)
cat(tikz_bent)
# With a color not supported by default TikZ colors; will fall back to RGB
tikz_darkblue <- make_tikz(
kn,
node_style = list(fill = "darkblue"),
full_doc = FALSE
)
cat(tikz_darkblue)
# With tiered knowledge
data(tpc_example)
kn_tiered <- knowledge(
tpc_example,
tier(
child ~ starts_with("child"),
youth ~ starts_with("youth"),
old ~ starts_with("old")
)
)
tikz_tiered_kn <- make_tikz(
kn_tiered,
full_doc = FALSE
)
cat(tikz_tiered_kn)
# Classic style with LaTeX labels
tikz_classic <- make_tikz(
kn_tiered,
style = "classic",
node_labels = c(child_x1 = "$X_1$", child_x2 = "$X_2$"),
tier_labels = c(child = "Childhood", old = "Old age"),
full_doc = FALSE
)
cat(tikz_classic)
# Edges are bent around nodes in the way: here A --> C bends around B
cg_line <- caugi::caugi(A %-->% B + C, B %-->% C)
layout_line <- data.frame(name = c("A", "B", "C"), x = c(0, 1, 2), y = 0)
tikz_routed <- make_tikz(
cg_line,
layout = layout_line,
style = "classic",
full_doc = FALSE
)
cat(tikz_routed)
# Draw every edge exactly with the requested bend instead
tikz_exact <- make_tikz(
cg_line,
layout = layout_line,
style = "classic",
edge_style = list(route = FALSE),
full_doc = FALSE
)
cat(tikz_exact)
# Set the bend of single edges (positive = bend left, negative = bend right);
# these are always drawn exactly as given
tikz_bends <- make_tikz(
cg_line,
layout = layout_line,
style = "classic",
bend = list(A = list(B = 30, C = 0)),
full_doc = FALSE
)
cat(tikz_bends)
################# Convert Disco to Tikz ################
data(num_data)
kn <- knowledge(
num_data,
X1 %-->% X2,
X2 %!-->% X3 + Y,
Y %!-->% Z
)
pc_bnlearn <- pc(engine = "bnlearn", test = "fisher_z", alpha = 0.05)
disco_kn <- disco(data = num_data, method = pc_bnlearn, knowledge = kn)
tikz_snippet <- make_tikz(disco_kn, scale = 10, full_doc = FALSE)
cat(tikz_snippet)
################# Convert caugi objects to Tikz ################
cg <- caugi::caugi(A %-->% B + C)
tikz_snippet <- make_tikz(
cg,
node_style = list(fill = "red"),
scale = 10,
full_doc = FALSE
)
cat(tikz_snippet)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.