| Title: | Create Publication-Ready Causal Diagrams |
|---|---|
| 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. |
| Authors: | Sebastian van Baalen [aut, cre, cph] (ORCID: <https://orcid.org/0000-0003-3098-5587>) |
| Maintainer: | Sebastian van Baalen <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.2 |
| Built: | 2026-05-19 09:41:23 UTC |
| Source: | https://github.com/sebastianvanbaalen/causalplot |
Draw a simple causal diagram using ggplot2, with rounded boxes and arrows.
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 )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 )
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. |
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.
A ggplot object.
# Simple three-box causal diagram causal_plot(type = "111") # With custom labels causal_plot( labels = c("Education", "Income", "Health"), type = "111" )# Simple three-box causal diagram causal_plot(type = "111") # With custom labels causal_plot( labels = c("Education", "Income", "Health"), type = "111" )
Opens an interactive Shiny application for building causal diagrams and typology plots using the causalplot package.
run_app(...)run_app(...)
... |
Additional arguments passed to |
This function does not return a value; it launches a Shiny app.
if (interactive()) { run_app() }if (interactive()) { run_app() }
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.
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 )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 )
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. |
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)
A ggplot object.
# 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") )# 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") )