| Type: | Package |
| Title: | Create Publication-Ready Causal Diagrams |
| Version: | 0.2.1 |
| Description: | Creates publication-ready causal diagrams using 'ggplot2'. Provides simple templates for common causal diagrams (e.g., mediating mechanisms and parallel pathways) with customizable labels, colors, fonts, and export-friendly defaults. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.2 |
| Imports: | ggforce, ggplot2, ggtext |
| Suggests: | shiny, shinythemes |
| URL: | https://github.com/sebastianvanbaalen/causalplot |
| BugReports: | https://github.com/sebastianvanbaalen/causalplot/issues |
| NeedsCompilation: | no |
| Packaged: | 2026-03-11 06:55:23 UTC; sebastian |
| Author: | Sebastian van Baalen
|
| Maintainer: | Sebastian van Baalen <sebastian.van-baalen@pcr.uu.se> |
| Repository: | CRAN |
| Date/Publication: | 2026-03-16 19:00:02 UTC |
Create a causal plot
Description
Draw a simple causal diagram using ggplot2, with rounded boxes and arrows.
Usage
causal_plot(
labels = NULL,
type = "1111",
fill_variables = "grey80",
fill_mechanisms = "grey90",
corner_radius = 0.12,
font = "sans",
text_size = 4,
text_color = "black",
wrap_width = 24,
arrow_length = grid::unit(3, "mm"),
arrow_linewidth = 0.6,
box_ratio = 0.8,
xlim = NULL,
ylim = NULL
)
Arguments
labels |
Character vector of labels. If NULL, defaults are used. |
type |
Character. Layout template. |
fill_variables |
Fill color for the first and last box (variables). |
fill_mechanisms |
Fill color for the in-between boxes (mechanisms). |
corner_radius |
Rounded corner radius. Either a grid::unit() or a numeric (interpreted as cm). |
font |
Font family for labels (e.g., "sans", "serif"). Default is "sans". |
text_size |
Numeric label size. |
text_color |
Label color. |
wrap_width |
Integer. Approximate wrap width for labels (in characters). |
arrow_length |
grid::unit for arrowhead size. |
arrow_linewidth |
Numeric line width for arrows. |
box_ratio |
Numeric. Passed to coord_fixed(ratio = box_ratio). Default 0.8. |
xlim |
Numeric vector of length 2, or NULL for auto-computed limits. |
ylim |
Numeric vector of length 2, or NULL for auto-computed limits. |
Details
Supported templates:
"111" : IV -> mech -> DV (3 boxes)
"1111": IV -> mech1 -> mech2 -> DV (4 boxes)
"11111": IV -> mech1 -> mech2 -> mech3 -> DV (4 boxes)
"1121": IV -> one box -> two boxes -> DV (5 boxes)
"1211": IV (center) -> two boxes -> one box -> DV (5 boxes)
"1221": IV (center) -> two parallel paths -> DV (center) (6 boxes)
"bathtub": like "1221" but only bottom path + dashed direct IV->DV (4 boxes)
"111_moderator": like "111" but with a moderator variable
"111_confounder": like "111" but with a confounder variable
"211" : two IVs -> shared mechanism -> DV (4 boxes)
"221" : two IVs -> two parallel mechanisms -> DV (5 boxes)
"2221": two IVs -> two parallel mechanism steps -> DV (7 boxes)
Labels are wrapped to a maximum of 5 lines. If a label would wrap to more than
5 lines (given wrap_width), an error is thrown.
Value
A ggplot object.
Examples
# Simple three-box causal diagram
causal_plot(type = "111")
# With custom labels
causal_plot(
labels = c("Education", "Income", "Health"),
type = "111"
)
Launch the causalplot Shiny app
Description
Opens an interactive Shiny application for building causal diagrams and typology plots using the causalplot package.
Usage
run_app(...)
Arguments
... |
Additional arguments passed to |
Value
This function does not return a value; it launches a Shiny app.
Examples
if (interactive()) {
run_app()
}
Create a 2x2 typology diagram
Description
Draw a 2x2 typology diagram with four labeled quadrants, double-headed axis arrows, and axis labels using ggplot2. The diagram consists of a single rounded outer box divided into four quadrants by dashed midlines, with double-headed arrows indicating the two dimensions.
Usage
typology(
type_labels = c("Type 1", "Type 2", "Type 3", "Type 4"),
x_axis_label = "Dimension 2",
y_axis_label = "Dimension 1",
x_axis_values = c("Low", "High"),
y_axis_values = c("Low", "High"),
fill = "grey90",
border_color = NA,
line_color = "grey50",
line_linewidth = 0.5,
corner_radius = 0.12,
font = "sans",
text_size = 4,
text_color = "black",
axis_text_size = 3.5,
wrap_width = 20,
arrow_length = grid::unit(2, "mm"),
arrow_linewidth = 0.5,
box_ratio = 0.8,
xlim = NULL,
ylim = NULL
)
Arguments
type_labels |
Character vector of length 4. Labels for each quadrant,
in order: top-left, top-right, bottom-left, bottom-right.
Default: |
x_axis_label |
Character. Label for the horizontal axis (top).
Default: |
y_axis_label |
Character. Label for the vertical axis (left side).
Default: |
x_axis_values |
Character vector of length 2. Labels for the left (low)
and right (high) ends of the x-axis. Default: |
y_axis_values |
Character vector of length 2. Labels for the top
and bottom ends of the y-axis. Default: |
fill |
Fill color for the outer box. Default: |
border_color |
Color of the outer box border. Default: |
line_color |
Color of the dashed dividing lines. Default: |
line_linewidth |
Numeric. Line width for the dashed dividing lines. Default: |
corner_radius |
Rounded corner radius. Either a grid::unit() or a numeric
(interpreted as cm). Default: |
font |
Font family for labels (e.g., "sans", "serif"). Default: |
text_size |
Numeric. Text size for type labels. Default: |
text_color |
Color for all text labels. Default: |
axis_text_size |
Numeric. Text size for axis labels and endpoint values. Default: |
wrap_width |
Integer. Approximate wrap width for type labels (in characters). Default: |
arrow_length |
grid::unit for arrowhead size. Default: |
arrow_linewidth |
Numeric line width for axis arrows. Default: |
box_ratio |
Numeric. Passed to coord_fixed(ratio = box_ratio). Default: |
xlim |
Numeric vector of length 2, or NULL for auto-computed limits. |
ylim |
Numeric vector of length 2, or NULL for auto-computed limits. |
Details
The type_labels vector maps to quadrants as follows:
-
type_labels[1]: top-left (high y-axis, low x-axis) -
type_labels[2]: top-right (high y-axis, high x-axis) -
type_labels[3]: bottom-left (low y-axis, low x-axis) -
type_labels[4]: bottom-right (low y-axis, high x-axis)
Value
A ggplot object.
Examples
# Basic typology with defaults
typology()
# Custom labels
typology(
type_labels = c("Foxes", "Eagles", "Mice", "Snakes"),
x_axis_label = "Speed",
y_axis_label = "Size",
x_axis_values = c("Slow", "Fast"),
y_axis_values = c("Small", "Large")
)