Skip to main content

Style the nodes, edges and labels of a plot

Source: R/plot-compose.R

Description

Style a plot built with cgraph_plot() one step at a time. Each function returns the plot, so the steps chain with |>, and the plot is drawn when it is printed. Passing a graph instead of a plot starts one with cgraph_plot()’s defaults.

Usage

style_nodes(p, ..., nodes = NULL)

style_labels(p, ..., nodes = NULL)

style_title(p, ...)

label_nodes(p, ...)

style_edges(p, ..., from = NULL, to = NULL, type = NULL)

Arguments

p A plot from cgraph_plot(), or a graph created with cgraph().
... Named style fields (see the sections below), or for label_nodes() the text to draw in each node, named by node, e.g. X = "Exposure"; \n starts a new line. Other nodes show their name.
nodes Character vector of the nodes to style. NULL (default) styles every node.
from, to Character vectors of nodes. An edge is styled if it joins a node in from to a node in to, in either direction unless type fixes it. NULL (default) allows any node.
type Character vector of edge notations to style, read from a node in from to a node in to: "-->", "<--", "---", "<->", "o->", "<-o", "o--", "--o", or "o-o". NULL (default) allows every type. For example, from = "X", type = "-->" styles the edges out of X, and type = "<->" every bidirected edge.

Details

Each call styles only the nodes or edges it selects. A later call wins over an earlier one for the fields both set, and anything left unset uses the defaults of plot_options(). The selectors take node sets, so the results of query functions such as parents() or adjustment_set() can be passed straight in.

Value

The plot, a cgraph_plot object.

Node fields (style_nodes())

  • fill, col: fill and border color.

  • lwd, linestyle: border width and line type (any lty).

  • shape: "circle", "square", "ellipse", or "rect".

  • radius: node size in mm. Unset, nodes fit their labels, with padding mm around the text, times size.

Edge fields (style_edges())

  • col, lwd, lty: line color, width and type.

  • fill: arrowhead fill, by default the edge’s col; NA for hollow.

  • arrow_size, circle_size: arrowhead length and endpoint circle radius, in mm.

  • gap: space in mm between the edge ends and the nodes.

  • curvature: how far the edge bows; positive to the left travelling from its first node to its second, as listed by edges(). 0 turns off the automatic routing around nodes in the way.

  • path: waypoints the edge passes through, in layout coordinates, from the node in from to the node in to: a c(x, y) point, a two-column matrix, a data.frame with x and y columns, or a list of points.

  • label: text drawn along the edge, styled with label_col, label_fontsize, label_fontface and label_fontfamily, and placed with label_shift (0 at the edge’s first node to 1 at its second), label_distance (mm from the edge) and label_rotation (degrees; unset, it follows the edge).

Text fields (style_labels(), style_title())

col, fontsize, fontface, fontfamily, and cex.

See Also

cgraph_plot() for the layout and title, plot_options() for the defaults.

Other plotting: cgraph_grob(), cgraph_layout(), cgraph_plot(), cgraph_plot_grid(), plot_default_options(), plot_options()

Examples

dag <- cgraph("A --> X + Y, X --> M --> Y")

cgraph_plot(dag, main = "Mediation") |>
  style_nodes(fill = "lightblue", nodes = parents(dag, "Y")) |>
  style_nodes(shape = "square", nodes = "A") |>
  style_edges(col = "gray60") |>
  style_edges(col = "#DC143C", lwd = 2, from = "X", type = "-->") |>
  style_labels(fontface = "bold", nodes = c("X", "Y")) |>
  label_nodes(X = "Exposure", Y = "Outcome") |>
  style_title(col = "navy")


# Dash every bidirected edge of an ADMG
admg <- cgraph("X --> Y, X <-> Y, Y <-> Z", class = "ADMG")
admg |> style_edges(lty = "dashed", type = "<->")