Learn R Programming

CausalQueries (version 1.4.5)

plot_model: Plots a DAG in ggplot style using a causal model input

Description

Creates a plot of a DAG using ggplot functionality and a Sugiyama layout from igraph. Unmeasured confounds (<->) are indicated then these are represented as curved dotted lines. Users can control node sizes and colors as well as coordinates and label behavior. Other modifications can be made by adding additional ggplot layers.

Usage

plot_model(
  model = NULL,
  x_coord = NULL,
  y_coord = NULL,
  labels = NULL,
  title = "",
  textcol = "white",
  textsize = 3.88,
  shape = 16,
  nodecol = "black",
  nodesize = 12,
  parse = FALSE,
  strength = 0.3
)

Value

A ggplot object.

Arguments

model

A causal_model object generated from make_model

x_coord

A vector of x coordinates for DAG nodes. If left empty, coordinates are randomly generated

y_coord

A vector of y coordinates for DAG nodes. If left empty, coordinates are randomly generated

labels

Optional labels for nodes

title

String specifying title of graph

textcol

String specifying color of text labels

textsize

Numeric, size of text labels

shape

Indicates shape of node. Defaults to circular node.

nodecol

String indicating color of node that is accepted by ggplot's default palette

nodesize

Size of node.

strength

Degree of curvature of curved arcs

Examples

Run this code

if (FALSE) {
model <- make_model('X -> K -> Y')

# Simple plot
model |> plot_model()

# Adding additional layers
model |> plot_model() +
  ggplot2::coord_flip()

# Adding labels
model |>
  plot_model(
    labels = c("A long name for a \n node", "This", "That"),
    nodecol = "white",
    textcol = "black")

# Math labels: add parse = TRUE
make_model() |>
  plot_model(
    labels = c("alpha^2", "beta[1]"),
    parse = TRUE,
    textcol  = "black", nodecol = "white")

# Mixed math text labels: add parse = TRUE
make_model() |>
  plot_model(
      labels = c('alpha ~ "class"', 'beta ~ "class"'),
      parse = TRUE,
      textcol  = "black", nodecol = "white",)

# Adding math title after graph creation
make_model() |>
  plot_model() +
  ggplot2::labs(title = expression(paste(Gamma, " graph")))
}

# DAG with unobserved confounding and shapes and position control
make_model('Z -> X -> Y; X <-> Y') |>
  plot(x_coord = 1:3, y_coord = 1:3, shape = c(15, 16, 16))

# Sometimes clipping of nodes or labels arises and can be dealt with thus:
make_model() |>
  plot_model() +
  ggplot2::coord_cartesian(clip = "off")

Run the code above in your browser using DataLab