Learn R Programming

ggsem (version 0.9.9)

draw_lines: Draw Lines on a ggplot Object from Line Data

Description

This function overlays lines or arrows to a ggplot object based on line data. It supports straight lines, curved lines, gradient color transitions, and one-way or two-way arrows. The data can come from a CSV file generated by the ggsem Shiny app or custom input.

Usage

draw_lines(lines_data, zoom_level = 1, n = 100)

Value

ggplot2 line layers

Arguments

lines_data

A data frame containing line information. The expected columns include:

  • x_start, y_start: Starting coordinates of the line.

  • x_end, y_end: Ending coordinates of the line.

  • ctrl_x, ctrl_y: Control points for curved lines (optional).

  • type: Type of line ("Straight Line", "Curved Line", "Straight Arrow", or "Curved Arrow").

  • color: Start color of the line (hexadecimal color code).

  • end_color: End color of the line for gradients (hexadecimal color code).

  • color_type: "Gradient" for gradient lines, or "Single" for solid-colored lines.

  • gradient_position: Position of the gradient transition along the line (numeric, 0 to 1).

  • width: Width of the line (numeric).

  • alpha: Transparency of the line (numeric, 0 to 1).

  • arrow: Logical, whether an arrowhead is used.

  • arrow_type: Type of arrow ("open", "closed", etc.).

  • arrow_size: Size of the arrowhead.

  • two_way: Logical, whether the line has two arrowheads (bidirectional).

  • line_style: Line style ("solid", "dashed", or "dotted").

zoom_level

Numeric. Adjusts the size of line widths and arrowheads relative to the plot. Default is 1.

n

Integer. Number of points for interpolation in gradient or curved lines. Default is 100.

Examples

Run this code
library(ggplot2)

lines_df <- data.frame(
x_start = 11, y_start = -2.3, x_end = 21, y_end = 3.5,
ctrl_x = NA, ctrl_y = NA, ctrl_x2 = NA, ctrl_y2 = NA,
curvature_magnitude = NA, rotate_curvature = NA,
curvature_asymmetry = NA, type = 'Straight Line',
color = '#000000', end_color = NA, color_type = 'Single',
gradient_position = NA, width = 1, alpha = 1, arrow = TRUE,
arrow_type = 'closed', arrow_size = 0.1, two_way = FALSE,
lavaan = FALSE, network = FALSE, line_style = 'solid',
locked = FALSE, group = 1
)

p <- ggplot()

p + draw_lines(lines_data = lines_df, zoom_level = 1.2, n = 200)

Run the code above in your browser using DataLab