### Setup: Load required libraries Source: https://wjschne.github.io/ggdiagram/articles/equations.html Loads the necessary packages for the examples in this article: ggplot2, ggdiagram, tibble, dplyr, purrr, and ggarrow. ```R library(ggplot2) library(ggdiagram) library(tibble) library(dplyr) library(purrr) library(ggarrow) ``` -------------------------------- ### Setup: theme and base plot (bp) Source: https://wjschne.github.io/ggdiagram/articles/polygons.html Defines the color palette and theme used throughout the examples, and creates the base ggdiagram plot object 'bp' with custom axes. ```R # my_colors <- viridis::viridis(2, begin = .25, end = .5) my_colors <- c("#3B528B", "#21908C") theme_set( theme_minimal( base_size = my_font_size, base_family = my_font) + theme(axis.title.y = element_text(angle = 0, vjust = 0.5))) bp <- ggdiagram( font_family = my_font, font_size = my_font_size, point_size = my_point_size, linewidth = .5, theme_function = theme_minimal, axis.title.x = element_text(face = "italic"), axis.title.y = element_text( face = "italic", angle = 0, hjust = .5, vjust = .5)) + scale_x_continuous(labels = signs_centered, limits = c(-4, 4)) + scale_y_continuous(labels = signs::signs, limits = c(-4, 4)) ``` -------------------------------- ### bind examples Source: https://wjschne.github.io/ggdiagram/reference/bind.html Examples of binding point and circle objects. ```R bind(c(ob_point(1,2), ob_point(3,4))) #> #> ── #> # A tibble: 2 × 2 #> x y #> #> 1 1 2 #> 2 3 4 bind(c(ob_circle(ob_point(0,0), radius = 1), ob_circle(ob_point(1,1), radius = 2))) #> #> ── #> # A tibble: 2 × 3 #> x y r #> #> 1 0 0 1 #> 2 1 1 2 ``` -------------------------------- ### Example: set and restore default arrowhead Source: https://wjschne.github.io/ggdiagram/reference/arrowhead.html Demonstrates getting the default arrowhead, setting a new one with ggarrow::arrow_head_wings(offset = 25), and restoring the default. Output shows the matrix structure. ```R arrowhead() #> #> ── #> A matrix with 2 columns and 200 rows. First 6 rows: #> x y #> [1,] -0.39 0.00 #> [2,] -0.39 0.02 #> [3,] -0.39 0.04 #> [4,] -0.40 0.06 #> [5,] -0.40 0.08 #> [6,] -0.40 0.10 # Set new default set_default_arrowhead(ggarrow::arrow_head_wings(offset = 25)) arrowhead() #> #> ── #> A matrix with 2 columns and 4 rows. #> x y #> [1,] 1.00 0.00 #> [2,] -0.48 0.69 #> [3,] 0.00 0.00 #> [4,] -0.48 -0.69 # restore default set_default_arrowhead() arrowhead() #> #> ── #> A matrix with 2 columns and 200 rows. First 6 rows: #> x y #> [1,] -0.39 0.00 #> [2,] -0.39 0.02 #> [3,] -0.39 0.04 #> [4,] -0.40 0.06 #> [5,] -0.40 0.08 #> [6,] -0.40 0.10 ``` -------------------------------- ### latex_color Example Source: https://wjschne.github.io/ggdiagram/reference/latex_color.html An example showing how to color a mathematical expression red. ```R latex_color("X^2", "red") #> [1] "{\\color[HTML]{FF0000} X^2}" ``` -------------------------------- ### Setup ggdiagram environment Source: https://wjschne.github.io/ggdiagram/articles/bezier.html Load the necessary libraries for creating diagrams and plots. ```r library(ggdiagram) library(ggplot2) library(dplyr) library(ggtext) library(ggarrow) library(geomtextpath) ``` -------------------------------- ### Examples of subscript() and superscript() Source: https://wjschne.github.io/ggdiagram/reference/subscript.html Demonstrates the output of subscript() and superscript() with simple inputs, returning markdown-formatted strings. ```r subscript("X", 1) #> [1] "X1" superscript("A", 2) #> [1] "A2" ``` -------------------------------- ### Set up base plot with custom theme and colors Source: https://wjschne.github.io/ggdiagram/articles/lines.html Defines font, point size, colors, and a base plot (bp) with a minimal theme and custom axis labels. This setup is reused across all line examples. ```R my_font <- "Roboto Condensed" my_font_size <- 20 my_point_size <- 2 # my_colors <- viridis::viridis(2, begin = .25, end = .5) my_colors <- c("#3B528B", "#21908C") theme_set( theme_minimal( base_size = my_font_size, base_family = my_font) + theme(axis.title.y = element_text(angle = 0, vjust = 0.5))) bp <- ggdiagram( font_family = my_font, font_size = my_font_size, point_size = my_point_size, linewidth = .5, theme_function = theme_minimal, axis.title.x = element_text(face = "italic"), axis.title.y = element_text( face = "italic", angle = 0, hjust = .5, vjust = .5)) + scale_x_continuous(labels = signs_centered, limits = c(-4, 4)) + scale_y_continuous(labels = signs::signs, limits = c(-4, 4)) ``` -------------------------------- ### Example of circle_from_3_points Source: https://wjschne.github.io/ggdiagram/reference/circle_from_3_points.html An example demonstrating how to pass three ob_point objects to the function to obtain an ob_circle. ```R circle_from_3_points(ob_point(1,1), ob_point(2,4), ob_point(5,3)) #> #> ── #> # A tibble: 1 × 3 #> x y r #> #> 1 3 2 2.24 ``` -------------------------------- ### Create multiple ngons Source: https://wjschne.github.io/ggdiagram/reference/ob_ngon.html Example showing how to create a sequence of ngons with varying numbers of sides and positions. ```R ob_ngon(center = ob_point(x = 3:8, y = 0), n = 3:8, radius = .4) #> #> ── #> # A tibble: 6 × 5 #> x y radius n angle #> #> 1 3 0 0.4 3 0 #> 2 4 0 0.4 4 0 #> 3 5 0 0.4 5 0 #> 4 6 0 0.4 6 0 #> 5 7 0 0.4 7 0 #> 6 8 0 0.4 8 0 ``` -------------------------------- ### Calculate Hierarchy Depth Examples Source: https://wjschne.github.io/ggdiagram/reference/get_depth.html Examples demonstrating how to calculate the depth of variables in a simple lavaan model string. ```R model <- "X =~ X1 + X2" get_depth("X", model = model) #> [1] 2 get_depth("X1", model = model) #> [1] 1 ``` -------------------------------- ### Create an ob_rectangle instance Source: https://wjschne.github.io/ggdiagram/reference/ob_rectangle.html Example of initializing an ob_rectangle centered at the origin with a width of 3 and a height of 2. ```R ob_rectangle(center = ob_point(0,0), width = 3, height = 2) #> #> ── #> # A tibble: 1 × 7 #> x y width height angle color fill #> #> 1 0 0 3 2 0 black NA ``` -------------------------------- ### Example of creating a squircle with redefault Source: https://wjschne.github.io/ggdiagram/reference/redefault.html This example demonstrates creating a new function 'squircle' from 'ob_ellipse' with a default 'm1' value of 4, then calling it with 'a = 3'. ```R squircle <- redefault(ob_ellipse, m1 = 4) squircle(a = 3) #> #> ── #> # A tibble: 1 × 7 #> x y a b angle m1 m2 #> #> 1 0 0 3 3 0 4 4 ``` -------------------------------- ### Create multiple paths with ob_path Source: https://wjschne.github.io/ggdiagram/reference/ob_path.html Example from the documentation showing how to create an ob_path with two paths using a list of ob_point objects and different colors. ```r ggdiagram() + ob_path(list(ob_point(c(0, 0, 4), c(0, 1, 4)), ob_point(c(1, 2, 5, 6), c(0, 1, 2, 0))), color = c("red", "blue")) ``` -------------------------------- ### equation() examples with ob_line and ob_circle Source: https://wjschne.github.io/ggdiagram/reference/equation.html Example usage of equation() on a circle and a line. Creates a line with slope 2 and intercept 4, and a circle with radius 3. The output for the circle is "*x*2 + *y*2 = 32" and for the line is "*y* = 2*x* + 4". ```r l1 <- ob_line(slope = 2, intercept = 4) c1 <- ob_circle(radius = 3) equation(c1) #> [1] "*x*2 + *y*2 = 32" equation(l1) #> [1] "*y* = 2*x* + 4" ``` -------------------------------- ### nudge object example Source: https://wjschne.github.io/ggdiagram/reference/nudge.html Demonstrates shifting a circle using nudge and compares it with an alternative point addition method. ```R ob_circle() |> nudge(x = 2) #> #> ── #> # A tibble: 1 × 3 #> x y r #> #> 1 2 0 1 # Alternative to nudge: ob_circle() + ob_point(2, 0) #> #> ── #> # A tibble: 1 × 3 #> x y r #> #> 1 2 0 1 ``` -------------------------------- ### Create and connect diagram objects Source: https://wjschne.github.io/ggdiagram/index.html This example demonstrates creating a circle and a rectangle, placing them with a specified separation, and connecting them with an arrow. ```R library(ggdiagram) c1 <- ob_circle(radius = 1 / sqrt(pi)) r2 <- ob_rectangle() |> place(from = c1, where = "right", sep = .5) ggdiagram() + c1 + r2 + connect(c1, r2, resect = 1) ``` -------------------------------- ### Creating and operating on angles Source: https://wjschne.github.io/ggdiagram/reference/ob_angle.html Examples showing how to create angles using different units and perform arithmetic or trigonometric operations on them. ```R # Three Different ways to make a right angle ## 90 degrees degree(90) #> [1] "90°" ## half pi radians radian(.5 * pi) #> [1] "0.5π" ## A quarter turn turn(.25) #> [1] ".25" # Operations degree(30) + degree(20) #> [1] "50°" degree(350) + degree(20) #> [1] "10°" degree(30) - degree(30) #> [1] "0°" degree(30) - degree(50) #> [1] "−20°" degree(30) * 2 #> [1] "60°" degree(30) / 3 #> [1] "10°" radian(1) + 1 # added or subtracted numbers are radians #> [1] "0.64π" degree(10) + 10 # added or subtracted numbers are degrees #> [1] "20°" turn(.25) + .25 # added or subtracted numbers are turns #> [1] ".50" # Trigonometric functions work as normal sin(degree(30)) #> [1] 0.5 cos(degree(30)) #> [1] 0.8660254 tan(degree(30)) #> [1] 0.5773503 ``` -------------------------------- ### Load required packages for ggdiagram examples Source: https://wjschne.github.io/ggdiagram/articles/lines.html Loads ggdiagram and supporting packages (ggplot2, dplyr, ggtext, ggarrow) needed for the examples in this article. ```R library(ggdiagram) library(ggplot2) library(dplyr) library(ggtext) library(ggarrow) ``` -------------------------------- ### Install ggdiagram development version Source: https://wjschne.github.io/ggdiagram/index.html Use this command to install the development version of the package from R-universe. ```R install.packages("ggdiagram", repos = "https://wjschne.r-universe.dev") ``` -------------------------------- ### Install ggdiagram from CRAN Source: https://wjschne.github.io/ggdiagram/index.html Use this command to install the stable version of the package from CRAN. ```R install.packages("ggdiagram") ``` -------------------------------- ### class_color usage example Source: https://wjschne.github.io/ggdiagram/reference/class_color.html Demonstrates creating a `class_color` object from "blue", displaying its hex code, and using the `@transparentize`, `@lighten`, and `@darken` methods. The output shows the resulting hex codes and the printed object structure. ```r mycolor <- class_color("blue") mycolor #> #> ── ──────────────────────────────────────────────────── #> @ color: chr "#0000FFFF" #> Other props: transparentize, lighten, darken, saturation, hue, #> brightness, alpha, red, green, blue, mean, tex, id # Display html hexcode c(mycolor) #> [1] "#0000FFFF" # Set transparency mycolor@transparentize(.5) #> #> ── ──────────────────────────────────────────────────── #> @ color: chr "#0000FF80" #> Other props: transparentize, lighten, darken, saturation, hue, #> brightness, alpha, red, green, blue, mean, tex, id # Lighten color mycolor@lighten(.5) #> #> ── ──────────────────────────────────────────────────── #> @ color: chr "#8282FFFF" #> Other props: transparentize, lighten, darken, saturation, hue, #> brightness, alpha, red, green, blue, mean, tex, id # Darken color mycolor@darken(.5) #> #> ── ──────────────────────────────────────────────────── #> @ color: chr "#000082FF" #> Other props: transparentize, lighten, darken, saturation, hue, #> brightness, alpha, red, green, blue, mean, tex, id ``` -------------------------------- ### Examples of perpendicular point operators Source: https://wjschne.github.io/ggdiagram/reference/perpendicular_point.html Demonstrates finding a point perpendicular to two points using vertical (%|-% ) and horizontal (%-|% ) orientation. ```R x <- ob_point(0,0) y <- ob_point(1,1) # Find point perpendicular to x and y going vertically first x %|-% y #> #> ── #> # A tibble: 1 × 2 #> x y #> #> 1 0 1 # Find point perpendicular to x and y going horizontally first x %-|% y #> #> ── #> # A tibble: 1 × 2 #> x y #> #> 1 1 0 ``` -------------------------------- ### Define and render a single Bézier curve Source: https://wjschne.github.io/ggdiagram/articles/bezier.html Defines start, end, and offset points, binds them into a path, and renders both the control path and the resulting Bézier curve. ```R # start and end of control points p_start <- ob_point(-4,-2) p_end <- ob_point(4, 2) # Offset ob_point from the endpoints p_offset <- ob_point(5,6) # Make list of points and bind them into a single ob_point p <- c(p_start, p_start + p_offset, p_end - p_offset, p_end) |> bind() bp + ob_path(p, linetype = "dashed", color = "gray", linewidth = .25) + p + ob_bezier(p, label = ob_label("Labels can follow the path of the bézier curve.")) ``` -------------------------------- ### Example: Plotting points with ob_point and ob_polar Source: https://wjschne.github.io/ggdiagram/reference/ob_point.html Demonstrates how to add Cartesian points and polar points to a ggdiagram plot. ```R ggdiagram(theme = ggplot2::theme_minimal) + ob_point(x = -1:1, y = -1:1, color = "red") + ob_polar(theta = degree(seq(0, 330, 30)), r = 2, color = "blue") ``` -------------------------------- ### distance() examples Source: https://wjschne.github.io/ggdiagram/reference/distance.html Demonstrates calculating distances between various object types: two points, endpoints of a segment, a point and a line, and the edges of two circles. Outputs are shown as comments. ```r # Distance between two objects p1 <- ob_point(0, 0) p2 <- ob_point(3, 4) distance(p1, p2) #> [1] 5 # Distance between the endpoints of a segment s1 <- ob_segment(p1, p2) distance(s1) #> [1] 5 # Distance between a point and a line l1 <- ob_line(slope = 0, intercept = 1) distance(p1, l1) #> [1] 1 # Shortest distance between the edges of 2 circles c1 <- ob_circle(p1, radius = 1) c2 <- ob_circle(p2, radius = 2) distance(c1, c2) #> [1] 2 ``` -------------------------------- ### Load ggdiagram package Source: https://wjschne.github.io/ggdiagram/articles/colors.html Loads the ggdiagram library for use in subsequent examples. ```r library(ggdiagram) ``` -------------------------------- ### Load ggdiagram and ggplot2 libraries Source: https://wjschne.github.io/ggdiagram/articles/ggdiagram.html Initial setup required to use the ggdiagram package alongside ggplot2. ```R library(ggplot2) library(ggdiagram) ``` -------------------------------- ### mean_color() example Source: https://wjschne.github.io/ggdiagram/reference/mean_color.html Averages two colors 'dodgerblue' and 'violet' using mean_color(), outputting the resulting hex color '#A68BF6'. ```R color_A <- "dodgerblue" color_B <- "violet" mean_color(c(color_A, color_B)) #> [1] "#A68BF6" ``` -------------------------------- ### Create multiple paths with ob_path Source: https://wjschne.github.io/ggdiagram/articles/paths.html Creates three paths at once by passing a list of point objects to `ob_path`. Each path gets a different color and label. ```r bp + ob_path(c(p_curve, p_curve * .5 + ob_point(2,-2), p_curve * .5 + ob_point(-2,2)), color = c("orchid4", "dodgerblue3", "firebrick4"), label = c(".52", ".42", ".61")) ``` -------------------------------- ### Example: ob_covariance with ob_circle and ob_label Source: https://wjschne.github.io/ggdiagram/reference/ob_covariance.html Creates a ggdiagram with a circle and a covariance arrow between two points, labeled 'A'. ```R ggdiagram() + {x <- ob_circle(ob_point(c(-2, 2), 0))} + ob_covariance(x = x[1], y = x[2], label = ob_label("A")) ``` -------------------------------- ### Create an ellipse with center and semi-major axes Source: https://wjschne.github.io/ggdiagram/reference/ob_ellipse.html Example showing how to create an ellipse with a specified center point and semi-major axes. The output is an ob_ellipse object with a tibble containing the center coordinates, semi-axis lengths, angle, and superellipse exponents. ```r # specify center point and semi-major axes ob_ellipse(center = ob_point(0,0), a = 2, b = 3) #> #> ── #> # A tibble: 1 × 7 #> x y a b angle m1 m2 #> #> 1 0 0 2 3 0 2 2 ``` -------------------------------- ### Example: as.geom with ob_circle Source: https://wjschne.github.io/ggdiagram/reference/as.geom.html Creates a circle shape and adds it to a ggplot using as.geom with fill = "black" and coord_equal(). ```r library(ggplot2) c1 <- ob_circle(radius = 3) ggplot() + as.geom(c1, fill = "black") + coord_equal() ``` -------------------------------- ### Examples of lead_cycle and lag_cycle Source: https://wjschne.github.io/ggdiagram/reference/lead_cycle.html Demonstrates cyclic shifting on the vector 1:5. lead_cycle moves elements forward (first element becomes last), lag_cycle moves them backward (last element becomes first). The n argument controls the shift amount. ```r lead_cycle(1:5) #> [1] 2 3 4 5 1 lead_cycle(1:5, 2) #> [1] 3 4 5 1 2 lag_cycle(1:5) #> [1] 5 1 2 3 4 lag_cycle(1:5, 2) #> [1] 4 5 1 2 3 ``` -------------------------------- ### Example: ob_variance with circle and labels Source: https://wjschne.github.io/ggdiagram/reference/ob_variance.html Creates a ggdiagram with a circle of points, connects every third point, and adds variance arrows with labels at each point. Uses theta from 0 to 315 degrees in 45-degree increments, looseness 1.25, and resect 2. ```R theta <- degree(seq(0, 360 - 45, 45)) ggdiagram() + {x <- ob_circle(ob_polar(theta, r = 3))} + connect(x, lag_cycle(x, 3), resect = 2) + ob_variance(x, label = ob_label(LETTERS[seq_along(c(theta))]), where = theta, looseness = 1.25) ``` -------------------------------- ### Construct Complex Structural Model Diagram Source: https://wjschne.github.io/ggdiagram/articles/causalpaths.html This example demonstrates building a complex model with latent variables, observed variable arrays, and custom geometric representations of variance. ```R ggdiagram(font_family = my_font, font_size = 13) + {g <- latent(label = lb_latent("*g*"), fill = "gray15", radius = sqrt(1 / pi))} + {Gx <- place(g, g,where = "below", sep = 1.2) |> ob_array(k = 3, sep = 2.4, label = lb_latent(broad, vjust = .6, color = "gray15", size = 28), fill = "gray15")} + {d_Gx <- ob_circle( color = NA, fill = my_fills, radius = sqrt(broad_variance / pi)) |> place(Gx, where = "left", sep = .45)} + direct(d_Gx, Gx, color = my_fills, resect = 1) + {pGx <- direct(g, Gx, color = Gx@fill, resect = 1)} + {lb_direct( label = round_probability(g2broad, phantom_text = "."), center = pGx@line@point_at_y(pGx[2]@midpoint( position = .46)@y), color = Gx@fill)} + ob_wedge(center = Gx@center, radius = sqrt(1 / pi), start = turn(.5) + turn(broad_variance) / 2, end = turn(.5) - turn(broad_variance) / 2, fill = my_fills) + lb_latent(broad, center = Gx@center, vjust = .6) + {map(1:3, \(i) { b <- Gx[i] p2 <- place(ob_point(), from = b, where = "south", sep = 1.2) p1 <- place(ob_point(), from = p2, where = "left", sep = 1.1) p3 <- place(ob_point(), from = p2, where = "right", sep = 1.1) p <- bind(c(p1,p2,p3)) b_text <- b@label@label lb_o <- ob_label(paste0(b_text, "~", 1:3, "~"), center = p - ob_point(0,.55), color = "white", size = 20, fill = NA) b_p <- direct(b, p, color = my_fills[i], resect = 1) l_b_p <- lb_direct( round_probability(broad2indicator[[b_text]], phantom_text = "."), center = b_p@line@point_at_y(b_p[2]@midpoint(.46)@y), color = my_fills[i]) r_g <- ob_rectangle( north = p, fill = g@fill, color = NA, width = 1, height = (g2broad[b_text] * broad2indicator[[b_text]]) ^ 2) r_broad <- ob_rectangle( north = r_g@south, fill = my_fills[i], width = 1, color = NA, height = (sqrt(broad_variance[b_text]) * broad2indicator[[b_text]]) ^ 2) error_variance <- 1 - broad2indicator[[b_text]] ^ 2 error_color <- class_color(my_fills[i])@lighten(.5)@color r_unique <- ob_rectangle( north = r_broad@south, fill = error_color, width = 1, color = NA, height = error_variance) r <- sqrt(error_variance / pi) error <- ob_circle(r_unique@south - ob_point(0, r + .45), radius = r, color = NA, fill = error_color) error2observed <- direct(error, r_unique, resect = 1, color = error_color) list(r_g, r_broad, r_unique, b_p, l_b_p, error, error2observed, lb_o) |> lapply(as.geom) })} ``` -------------------------------- ### Initialize Plotting Environment Source: https://wjschne.github.io/ggdiagram/articles/segments.html Sets up the color palette and ggplot2 theme for the diagrams. ```R my_colors <- c("#3B528B", "#21908C") theme_set( theme_minimal(base_size = my_font_size, base_family = my_font) + theme(axis.title.y = element_text(angle = 0, vjust = 0.5)) ) bp <- ggdiagram( font_family = my_font, font_size = my_font_size, point_size = my_point_size, linewidth = .5, theme_function = theme_minimal, axis.title.x = element_text(face = "italic"), axis.title.y = element_text( face = "italic", angle = 0, hjust = .5, vjust = .5 ) ) + scale_x_continuous(labels = signs_centered, limits = c(-4, 4)) + scale_y_continuous(labels = signs::signs, limits = c(-4, 4)) ``` -------------------------------- ### ggdiagram example with circle and rectangle Source: https://wjschne.github.io/ggdiagram/reference/ggdiagram.html Example usage of ggdiagram with custom font size, font family, and line width, adding a circle and a rectangle. The example shows how to combine ggdiagram with ob_circle and ob_rectangle. ```R ggdiagram(font_size = 20, font_family = "serif", linewidth = 3) + ob_circle(label = "Circle") + ob_rectangle(label = "Rectangle", x = 3, width = 3) ``` -------------------------------- ### ob_arc with start and end degrees Source: https://wjschne.github.io/ggdiagram/reference/ob_arc.html Creates an arc object with start angle 0 and end angle 60 degrees. The output is a tibble with columns x, y, radius, start, end, n, and type. ```r ob_arc(start = degree(0), end = degree(60)) #> #> ── #> # A tibble: 1 × 7 #> x y radius start end n type #> #> 1 0 0 1 0 60 360 arc ``` -------------------------------- ### ob_path constructor usage Source: https://wjschne.github.io/ggdiagram/reference/ob_path.html Shows the full signature of the ob_path() constructor with all arguments and defaults. ```r ob_path( p = S7::class_missing, label = character(0), label_sloped = TRUE, alpha = numeric(0), arrow_head = S7::class_missing, arrow_fins = S7::class_missing, arrowhead_length = numeric(0), length_head = numeric(0), length_fins = numeric(0), color = character(0), fill = character(0), lineend = numeric(0), linejoin = numeric(0), linewidth = numeric(0), linewidth_fins = numeric(0), linewidth_head = numeric(0), linetype = numeric(0), resect = numeric(0), resect_fins = numeric(0), resect_head = numeric(0), stroke_color = character(0), stroke_width = numeric(0), style = S7::class_missing, id = character(0), ... ) ``` -------------------------------- ### ob_wedge with start and end degrees Source: https://wjschne.github.io/ggdiagram/reference/ob_arc.html Creates a wedge object with start angle 240 and end angle 300 degrees. The output tibble includes color and fill columns, with fill defaulting to black. ```r ob_wedge(start = degree(240), end = degree(300)) #> #> ── #> # A tibble: 1 × 9 #> x y radius start end color fill n type #> #> 1 0 0 1 240 300 NA black 360 wedge ``` -------------------------------- ### ob_circular_segment with start and end degrees Source: https://wjschne.github.io/ggdiagram/reference/ob_arc.html Creates a circular segment object with start angle 120 and end angle 180 degrees. The output tibble includes additional columns color and fill compared to ob_arc. ```r ob_circular_segment(start = degree(120), end = degree(180)) #> #> ── #> # A tibble: 1 × 9 #> x y radius start end color fill n type #> #> 1 0 0 1 120 180 NA black 360 segment ``` -------------------------------- ### ob_arc @tibble Source: https://wjschne.github.io/ggdiagram/reference/ob_arc.html Gets a tibble or data.frame containing parameters and styles used by ggarrow::geom_arrow. ```APIDOC ## ob_arc @tibble ### Description Gets a tibble::tibble or data.frame containing parameters and styles used by ggarrow::geom_arrow. ### Method @tibble ### Returns - **tibble** (tibble::tibble or data.frame) - The tibble or data.frame with parameters and styles. ``` -------------------------------- ### Get Segment Endpoints Source: https://wjschne.github.io/ggdiagram/articles/segments.html Retrieves segment endpoints by specifying midpoints at positions 0 and 1. ```R bp + s1 + s1@midpoint(c(0, 1)) ``` -------------------------------- ### Style circles and connectors with labels Source: https://wjschne.github.io/ggdiagram/articles/circles.html Demonstrates applying custom colors, labels, and connector styling to circles and paths. ```R bp + {cthis <- ob_circle( ob_point(-2, 2), radius = 1, fill = my_colors[1], color = NA, label = ob_label( "This", color = "white", fill = NA, size = 35 ) )} + {cthat <- ob_circle( ob_point(1.5, -1.5), radius = 1.5, fill = my_colors[2], color = NA, label = ob_label( "That", color = "white", fill = NA, size = 55 ) )} + connect(cthis, cthat, resect = 2, label = ob_label("Causes", size = 20, vjust = 0), color = "black") ``` -------------------------------- ### Create and Connect Diagram Variables Source: https://wjschne.github.io/ggdiagram/articles/automation.html Demonstrates creating multiple rectangle objects and connecting them using a loop and the connect function. ```R # Number of variables k <- 8L # Make k variables x <- ob_rectangle(ob_point(rep(0, k), rep(0, k)), label = ob_label( label = paste0("*X*~", 1:k, "~"), vjust = .6, size = 20, family = "Roboto Condensed" )) # Place even variables to the right and # odd variables below the previous variables for (i in 2:k) { x[i] <- place(x[i], x[i - 1], ifelse((i %% 2) == 0, "right", "below")) } # Plot ggdiagram() + x + # paths between variables ahead by 1 and by 2 connect(x[c(seq(1,k - 1), seq(1, k - 2))], x[c(seq(2,k), seq(3, k)), resect = 2) ``` -------------------------------- ### Create equivalent angles Source: https://wjschne.github.io/ggdiagram/articles/angles.html Demonstrates creating angle objects using degree, turn, and radian functions. ```R degree(90) #> [1] "90°" turn(1 / 4) #> [1] ".25" radian(pi / 2) #> [1] "0.5π" ``` -------------------------------- ### Calculate bounding box for ob_point Source: https://wjschne.github.io/ggdiagram/articles/points.html Demonstrates how to create an ob_point object with specific coordinates and retrieve its bounding box. ```R bp + {pts <- ob_point(x = 1:4, y = c(3, 3, 4, 2))} + pts@bounding_box ``` -------------------------------- ### Render multiple Bézier paths Source: https://wjschne.github.io/ggdiagram/articles/bezier.html Creates a list of point objects and renders a separate Bézier curve for each object in the list. ```R control_point_list <- c( ob_point( x = c(-4, -3, 4), y = c(.1, 4, .1), color = my_colors[1]), ob_point( x = c(-4, 3, 4), y = c(-.1, -4, -.1), color = my_colors[2] ) ) bp + ob_bezier(control_point_list) + ob_path(control_point_list, linetype = "dashed", linewidth = .25) + bind(control_point_list) ``` -------------------------------- ### Define an arc with ob_arc Source: https://wjschne.github.io/ggdiagram/articles/arcwedges.html Creates an arc object by specifying the center point, start and end angles, and radius. ```R ob_arc(center = ob_point(1,2), start = 25, end = 75, radius = 3) #> #> ── #> # A tibble: 1 × 7 #> x y radius start end n type #> #> 1 1 2 3 25 75 360 arc ``` -------------------------------- ### Define Default Styles and Parameters for Path Diagrams Source: https://wjschne.github.io/ggdiagram/articles/causalpaths.html This snippet sets up color palettes, path configurations, and default object styles using redefault for consistent diagram styling. ```R my_fills <- viridis::viridis(n = 3, begin = .3, end = .6) |> class_color() |> set_props(saturation = .6, brightness = .5) |> c() my_path_color <- "gray40" my_resect <- 1 broad <- c("Gv", "Gf", "Gc") g2broad <- c(Gv = .84, Gf = .95, Gc = .80) broad2indicator <- list(Gv = c(.78, .84, .91), Gf = c(.88, .81, .74), Gc = c(.74, .91, .93)) broad_variance <- 1 - g2broad ^ 2 latent <- redefault(ob_circle, color = NA) lb_latent <- redefault(ob_label, size = 30, fill = NA, color = "white") observed <- redefault(ob_ellipse, a = .5, b = .5, m1 = 10, # fill = my_fill, color = NA) lb_observed <- redefault( ob_label, size = 15, fill = NA, color = "white", nudge_y = -.04) ``` -------------------------------- ### Create base plot with theme and colors Source: https://wjschne.github.io/ggdiagram/articles/rectangles.html Sets up a base plot (bp) with a custom font, theme, and color palette. Defines my_colors as a list of three colors using class_color. ```R my_font <- "Roboto Condensed" my_font_size <- 20 my_point_size <- 2 # my_colors <- viridis::viridis(2, begin = .25, end = .5) my_colors <- c("#3B528B", "#21908C") theme_set( theme_minimal( base_size = my_font_size, base_family = my_font) + theme(axis.title.y = element_text(angle = 0, vjust = 0.5))) bp <- ggdiagram( font_family = my_font, font_size = my_font_size, point_size = my_point_size, linewidth = .5, theme_function = theme_minimal, axis.title.x = element_text(face = "italic"), axis.title.y = element_text( face = "italic", angle = 0, hjust = .5, vjust = .5)) + scale_x_continuous(labels = signs_centered, limits = c(-4, 4)) + scale_y_continuous(labels = signs::signs, limits = c(-4, 4)) my_colors <- list( primary = class_color("royalblue4"), secondary = class_color("firebrick4"), tertiary = class_color("orchid4")) ``` -------------------------------- ### Create a Reuleaux triangle Source: https://wjschne.github.io/ggdiagram/reference/ob_reuleaux.html Example of creating a Reuleaux triangle with 3 sides, filled with royalblue color and no border color. ```R ob_reuleaux(n = 3, fill = "royalblue", color = NA) #> #> ── #> # A tibble: 1 × 7 #> x y radius n angle color fill #> #> 1 0 0 1 3 90 NA royalblue ``` -------------------------------- ### Build Path Diagrams with lavaan Source: https://wjschne.github.io/ggdiagram/articles/automation.html Demonstrates the process of preparing structural equation model data for visualization with ggdiagram. ```R library(dplyr) library(tidyr) library(lavaan) # Observed variable name conversion onames <- c(VisualPerception = "x1", Cubes = "x2", Lozenges = "x3", CapitalsSpeed = "x9", AdditionSpeed = "x7", CountingSpeed = "x8", ParagraphComp = "x4", SentenceComp = "x5", WordMeaning = "x6") # Data d <- lavaan::HolzingerSwineford1939 |> dplyr::select(x1:x9) |> dplyr::rename(all_of(onames)) # Structural Model m <- ' Gv =~ VisualPerception + Cubes + Lozenges + CapitalsSpeed Gs =~ CapitalsSpeed + CountingSpeed + AdditionSpeed Gc =~ ParagraphComp + SentenceComp + WordMeaning g =~ Gv + Gc + Gs ' # Fit model with converted names fit <- sem(m, data = d) # Parameter table, find primary indicators pt <- standardizedsolution(fit) %>% mutate(primary_indicator = dplyr::dense_rank( desc(abs(est.std))) == 1 & op == "=~", .by = c(rhs, op)) # Oberved and latent names o_names <- lavaan::lavNames(fit, "ov") l_names <- lavaan::lavNames(fit, "lv") # Make colors latent_colors <- l_names %>% length() %>% viridis::viridis(alpha = .5, begin = .2, end = .75) %>% `names<-`(rev(l_names)) ``` -------------------------------- ### Usage of subscript() and superscript() Source: https://wjschne.github.io/ggdiagram/reference/subscript.html Shows the function signatures for subscript() and superscript(), including their arguments and the output parameter that can be 'markdown' (default) or 'latex'. ```r subscript(x, subscript = seq(length(x)), output = c("markdown", "latex")) superscript(x, superscript = seq(length(x)), output = c("markdown", "latex")) ``` -------------------------------- ### Define rectangle features Source: https://wjschne.github.io/ggdiagram/articles/rectangles.html Creates points for the four corners, center, width, and height of a rectangle. These are used in later examples. ```R # northeast corner ne <- ob_point(4, 2) # northwest corner nw <- ob_point(0, 2) # southwest corner sw <- ob_point(0, 0) # southeast corner se <- ob_point(4, 0) # center point cent <- ob_point(2, 1) # width w <- 4 # height h <- 2 ``` -------------------------------- ### Make a color transparent with @transparentize() Source: https://wjschne.github.io/ggdiagram/articles/colors.html Creates two circles with transparentized fills, one at the origin and one offset, to demonstrate the effect. ```r ggdiagram() + ob_circle(fill = my_color@transparentize(.5)) + ob_circle(fill = my_color@transparentize(.5), center = ob_point(1,0)) ``` -------------------------------- ### Create a basic path with ob_path Source: https://wjschne.github.io/ggdiagram/articles/paths.html Creates a path connecting three points using `ob_path` and adds it to the base plot. The points are created with `ob_point` and colored dodgerblue. ```r p <- ob_point(c(-4,-3,4), c(0,-2, 0), color = "dodgerblue") bp + ob_path(p) + p ```