### Create a wedge diagram with ob_wedge Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/arcwedges.html This example demonstrates using ob_wedge to draw multiple wedges with specified radii, start and end angles, and aesthetic properties like fill and color. ```R theta <- turn(c(0, 1 / 3, 1 / 2, 1)) bp + ob_wedge(radius = 3, start = theta[-length(theta)], end = theta[-1], fill = c("dodgerblue4", "orchid4", "darkgreen"), color = "white", linewidth = 1) ``` -------------------------------- ### Example of circle_from_3_points Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/circle_from_3_points.md An example demonstrating the calculation of a circle from three specific points. ```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 ``` -------------------------------- ### equation usage examples Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/equation.html Examples showing how to generate equations for a line and a circle object. ```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" ``` -------------------------------- ### Examples of subscript and superscript usage Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/subscript.md Basic examples showing how to generate subscript and superscript strings with their corresponding output. ```r subscript("X", 1) #> [1] "X1" superscript("A", 2) #> [1] "A2" ``` -------------------------------- ### Create arcs and wedges Source: https://github.com/wjschne/ggdiagram/blob/main/man/ob_arc.Rd Examples of creating arc, circular segment, and wedge objects using degree-based start and end points. ```R ob_arc(start = degree(0), end = degree(60)) ob_circular_segment(start = degree(120), end = degree(180)) ob_wedge(start = degree(240), end = degree(300)) ``` -------------------------------- ### ggdiagram example usage Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ggdiagram.html An example demonstrating how to initialize a diagram with custom font and line settings, then add objects. ```R ggdiagram(font_size = 20, font_family = "serif", linewidth = 3) + ob_circle(label = "Circle") + ob_rectangle(label = "Rectangle", x = 3, width = 3) ``` -------------------------------- ### Vector Shifting Examples Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/lead_cycle.html Examples demonstrating how lead_cycle and lag_cycle shift vector elements with recycling. ```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 ``` -------------------------------- ### Create an ob_ngon object Source: https://github.com/wjschne/ggdiagram/blob/main/man/ob_ngon.Rd Example showing how to initialize an ob_ngon object with a sequence of centers, varying number of sides, and a fixed radius. ```R ob_ngon(center = ob_point(x = 3:8, y = 0), n = 3:8, radius = .4) ``` -------------------------------- ### Setup ggdiagram environment Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/angles.md Initializes the necessary libraries and defines a font variable for subsequent plotting. ```r library(ggdiagram) library(ggplot2) library(dplyr) library(ggtext) library(ggarrow) my_font <- "Roboto Condensed" ``` -------------------------------- ### Load Required Libraries Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/bezier.md Initial setup to load the necessary packages for working with ggdiagram. ```r library(ggdiagram) library(ggplot2) library(dplyr) library(ggtext) library(ggarrow) library(geomtextpath) ``` -------------------------------- ### Setup ggdiagram environment Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/equations.html Load the necessary libraries required to use ggdiagram and its dependencies. ```r library(ggplot2) library(ggdiagram) library(tibble) library(dplyr) library(purrr) library(ggarrow) ``` -------------------------------- ### Manipulating class_color objects Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/class_color.html Examples showing how to initialize a color, retrieve its hex code, and apply transformations like transparency, lightening, and darkening. ```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 ``` -------------------------------- ### Example of latex_color usage Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/latex_color.html Demonstrates wrapping a TeX expression 'X^2' with the color 'red'. ```R latex_color("X^2", "red") #> [1] "{\\color[HTML]{FF0000} X^2}" ``` -------------------------------- ### Calculate hierarchy depth examples Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/get_depth.md Examples showing how to calculate the depth of latent and manifest variables in a simple lavaan model. ```r model <- "X =~ X1 + X2" get_depth("X", model = model) #> [1] 2 get_depth("X1", model = model) #> [1] 1 ``` -------------------------------- ### Examples of arrowhead management Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/arrowhead.html Demonstrates retrieving the default arrowhead, setting a new custom arrowhead using ggarrow, and restoring the original default. ```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 ``` -------------------------------- ### Install ggdiagram development version Source: https://github.com/wjschne/ggdiagram/blob/main/README.md Install the latest development version of the package from R-universe. ```r install.packages("ggdiagram", repos = "https://wjschne.r-universe.dev") ``` -------------------------------- ### Create an ob_rectangle Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_rectangle.md Example of creating an ob_rectangle centered at (0,0) with a width of 3 and 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 ``` -------------------------------- ### Create and connect diagram objects Source: https://github.com/wjschne/ggdiagram/blob/main/docs/index.md 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) ``` -------------------------------- ### Nudge object example Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/nudge.md Shows how to use nudge to shift a circle object and compares it with the addition operator syntax. ```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 ``` -------------------------------- ### Install ggdiagram from CRAN Source: https://github.com/wjschne/ggdiagram/blob/main/README.md Install the stable version of the package from the CRAN repository. ```r install.packages("ggdiagram") ``` -------------------------------- ### Perform basic angle arithmetic Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/angles.md Examples of adding degrees, radians, and turns. ```r degree(30) + degree(60) #> [1] "90°" degree(30) + 10 #> [1] "40°" radian(pi) + 0.5 * pi #> [1] "1.5π" turn(.1) + .2 #> [1] ".30" ``` -------------------------------- ### Create an ellipse with center and axes Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_ellipse.md Example of initializing an ob_ellipse by specifying the center point and the lengths of the semi-major and semi-minor axes. ```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 ``` -------------------------------- ### Creating and manipulating angles Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_angle.html Examples showing how to create angles using different units, perform arithmetic operations, and apply trigonometric functions. ```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 ``` -------------------------------- ### Generate equations for geometric objects Source: https://github.com/wjschne/ggdiagram/blob/main/man/equation.Rd This example demonstrates how to create line and circle objects using ob_line and ob_circle, then retrieve their equations using the equation function. ```R l1 <- ob_line(slope = 2, intercept = 4) c1 <- ob_circle(radius = 3) equation(c1) equation(l1) ``` -------------------------------- ### ob_wedge(start, end) Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_arc.html Creates a wedge shape defined by start and end angles. ```APIDOC ## ob_wedge(start, end) ### Description Generates a wedge object with specified start and end angles. ### Parameters - **start** (degree) - Required - The starting angle of the wedge. - **end** (degree) - Required - The ending angle of the wedge. ``` -------------------------------- ### Initialize base plot Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/segments.md Create a reusable base plot configuration for ggdiagram 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)) ``` -------------------------------- ### Bind points and circles Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/bind.html Examples showing how to bind multiple point objects and multiple circle objects into their respective bound classes. ```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 ``` -------------------------------- ### Create multiple ngons Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_ngon.md Example of creating multiple regular polygons with varying numbers of sides and centers. ```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 ``` -------------------------------- ### Example of ob_covariance Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_covariance.html A basic example showing how to add a covariance arrow between two points within a ggdiagram. ```R ggdiagram() + {x <- ob_circle(ob_point(c(-2, 2), 0))} + ob_covariance(x = x[1], y = x[2], label = ob_label("A")) ``` -------------------------------- ### Creating a squircle with redefault Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/redefault.html This example demonstrates creating a squircle function by setting the m1 parameter of ob_ellipse to 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 ``` -------------------------------- ### Example of perpendicular point calculation Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/perpendicular_point.md Demonstrates calculating perpendicular points for two ob_point objects x and y using both vertical and horizontal orientation operators. ```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 ``` -------------------------------- ### round_probability example Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/round_probability.html Demonstrates rounding a vector of probabilities to 2 significant digits. ```R round_probability(c(0, .0012, .012, .12, .99, .992, .9997, 1), digits = 2) #> [1] "0" ".0012" ".012" ".12" ".99" ".992" ".9997" "1" ``` -------------------------------- ### Create multiple bézier paths Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/bezier.md Supplying a list of point objects to ob_bezier generates a curve for each point object. The example uses the c function to concatenate points and visualizes them with ob_path and bind. ```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) ``` -------------------------------- ### Load ggdiagram and ggplot2 libraries Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/ggdiagram.md Initial setup required to use the ggdiagram package alongside ggplot2. ```r library(ggplot2) library(ggdiagram) ``` -------------------------------- ### Fixing arc start and end points Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/arcwedges.html Demonstrates using start_point and end_point arguments to anchor arcs to a specific point p1. ```R p1 <- ob_point(0, 0) bp + ob_arc(start = -45, end = 45, radius = 2, color = "orchid4", start_point = p1) + ob_arc(start = -45, end = 45, radius = 2, color = "forestgreen", end_point = p1) + p1 ``` -------------------------------- ### Generate circular geometries with ob_arc, ob_circular_segment, and ob_wedge Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_arc.md Examples of creating arc, circular segment, and wedge data structures using degree-based angle specifications. ```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_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_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 ``` -------------------------------- ### Building a Latent Variable Diagram with Indicators Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/ggdiagram.html This example demonstrates creating a latent variable A and three observed indicators A1-A3 using curly braces for variable assignment within a ggdiagram pipeline. ```R # Loadings loadings <- c(.86, .79, .90) error_variances <- sqrt(1 - loadings ^ 2) ggdiagram(font_size = 16) + # Latent variable {A <- ob_circle(radius = 2, label = ob_label("A", size = 96, nudge_y = -.15))} + # Observed variables (array of 3 superellipses below A) {A_3 <- ob_ellipse(m1 = 20) |> place(from = A, where = "below", sep = 3) | ob_array( k = 3, sep = .5, label = ob_label( label = paste0("A~", 1:3, "~"), size = 32, vjust = .6 ) )} + # Observed variable loadings with labels set to the same y coordinate connect( from = A, to = A_3, resect = 2, label = ob_label(label = round_probability(loadings), angle = 0))@set_label_y(position = .5) + # Error variances ob_variance( A_3, where = "south", looseness = 1.2, bend = -20, label = ob_label(label = round_probability(error_variances)) ) ``` -------------------------------- ### Connecting variables with map_ob Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/automation.md A practical example of using map_ob to connect every variable in one array to every variable in another array. ```r k <- 5 clr <- viridis::viridis(k, begin = .3, end = .7) ggdiagram() + {t1 <- ob_array(ob_circle(), k = k, where = "north", fill = clr, color = clr)} + {t2 <- ob_circle(fill = clr, color = clr)@place( from = t1, where = "right", sep = 10)} + map_ob(t1, \(tt) { connect(tt, t2, resect = 2, color = tt@color) }) ``` -------------------------------- ### ob_arc(start, end) Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_arc.html Creates an arc shape defined by start and end angles. ```APIDOC ## ob_arc(start, end) ### Description Generates an arc object with specified start and end angles. ### Parameters - **start** (degree) - Required - The starting angle of the arc. - **end** (degree) - Required - The ending angle of the arc. ``` -------------------------------- ### ob_circular_segment(start, end) Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_arc.html Creates a circular segment shape defined by start and end angles. ```APIDOC ## ob_circular_segment(start, end) ### Description Generates a circular segment object with specified start and end angles. ### Parameters - **start** (degree) - Required - The starting angle of the segment. - **end** (degree) - Required - The ending angle of the segment. ``` -------------------------------- ### ob_variance usage example Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_variance.md An example demonstrating the use of ob_variance to draw variance arrows around a series of points defined by polar coordinates. ```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) ``` -------------------------------- ### ob_arc(start, end, ...) Source: https://github.com/wjschne/ggdiagram/blob/main/man/ob_arc.Rd Creates an arc object defined by start and end angles. Additional parameters can be passed to define styling and positioning. ```APIDOC ## ob_arc ### Description Creates arcs and wedges for diagramming. ### Parameters - **start** (numeric) - The starting angle of the arc. - **end** (numeric) - The ending angle of the arc. - **resect_head** (numeric/unit) - Optional. Shortens the arrow head. - **stroke_color** (color) - Optional. Color of the point border line. - **stroke_width** (numeric) - Optional. Stroke width in arrows. - **style** (ob_style) - Optional. An ob_style object. - **x** (numeric) - Optional. x-coordinate of center point. - **y** (numeric) - Optional. y-coordinate of center point. - **id** (character) - Optional. Identifier for the object. - **...** (dynamic-dots) - Properties passed to the style object. ### Returns - **ob_arc** (object) - An arc object. ``` -------------------------------- ### bind ob_circle objects Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/bind.md Example of binding a vector of ob_circle objects. ```r 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 ``` -------------------------------- ### Create equivalent angles Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/angles.md Demonstrates how to instantiate angles using degree, turn, and radian functions. ```r degree(90) #> [1] "90°" turn(1 / 4) #> [1] ".25" radian(pi / 2) #> [1] "0.5π" ``` -------------------------------- ### bind ob_point objects Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/bind.md Example of binding a vector of ob_point objects. ```r bind(c(ob_point(1,2), ob_point(3,4))) #> #> ── #> # A tibble: 2 × 2 #> x y #> #> 1 1 2 #> 2 3 4 ``` -------------------------------- ### Perform angle multiplication Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/angles.md Examples of multiplying angle objects by scalars. ```r 2 * degree(20) #> [1] "40°" 2 * degree(180) #> [1] "0°" ``` -------------------------------- ### Example of calculating mean color Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/mean_color.html Calculates the average color between 'dodgerblue' and 'violet'. ```R color_A <- "dodgerblue" color_B <- "violet" mean_color(c(color_A, color_B)) #> [1] "#A68BF6" ``` -------------------------------- ### Basic nudge usage Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/nudge.md Demonstrates the syntax for the nudge function. ```r nudge(object, x, y, ...) ``` -------------------------------- ### Angle arithmetic operations Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_angle.md Examples of addition, subtraction, multiplication, and division with angle objects. ```r # 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" ``` -------------------------------- ### Initialize ob_style class Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_style.md Constructor function for defining styling attributes for diagram objects. ```r ob_style( id = character(0), alpha = numeric(0), angle = numeric(0), arrow_head = list(), arrow_fins = list(), arrow_mid = list(), color = character(0), family = character(0), fill = character(0), fontface = character(0), hjust = numeric(0), justify = numeric(0), label.color = character(0), label.margin = list(), label.padding = list(), label.r = numeric(0), label.size = numeric(0), arrowhead_length = numeric(0), length_head = numeric(0), length_fins = numeric(0), length_mid = numeric(0), lineend = numeric(0), lineheight = numeric(0), linejoin = numeric(0), linewidth_fins = numeric(0), linewidth_head = numeric(0), linewidth = numeric(0), linetype = numeric(0), n = numeric(0), nudge_x = numeric(0), nudge_y = numeric(0), polar_just = numeric(0), resect = numeric(0), resect_fins = numeric(0), resect_head = numeric(0), shape = numeric(0), size = numeric(0), size.unit = numeric(0), straight = logical(0), stroke = numeric(0), stroke_color = character(0), stroke_width = numeric(0), text.color = character(0), vjust = numeric(0), ... ) ``` -------------------------------- ### Programmatic variable placement Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/automation.md Use a loop and subsetting to place variables and create connections programmatically. ```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 Bézier Curve with Labels Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/bezier.md Demonstrates creating a complex path by binding offset points and adding a label that follows the 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.")) ``` -------------------------------- ### Connect two circles with labels and styles Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/circles.md Demonstrates styling circles with custom colors and labels, and adding a labeled connection path between them. ```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") ``` -------------------------------- ### Example usage of polar2just Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/polar2just.html Demonstrates calculating horizontal and vertical justification values for a 'northwest' direction. ```R a <- "northwest" polar2just(a, axis = "h") #> [1] 0.9242641 polar2just(a, axis = "v") #> [1] 0.07573593 ``` -------------------------------- ### Example of as.geom with a circle Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/as.geom.html Demonstrates the explicit use of as.geom to add a circle shape to a ggplot object. ```R library(ggplot2) c1 <- ob_circle(radius = 3) ggplot() + as.geom(c1, fill = "black") + coord_equal() ``` -------------------------------- ### Basic usage of data2shape Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/data2shape.md Demonstrates the syntax for calling data2shape with a data object and a shape function. ```r data2shape(data, shape) ``` -------------------------------- ### Define rectangle features Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/rectangles.md Specifies the coordinates for corners, center, width, and height used in subsequent 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 ``` -------------------------------- ### Create a wedge with ob_wedge Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_arc.html Generates a wedge object starting at 240 degrees and ending at 300 degrees. ```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 ``` -------------------------------- ### Create an arc with ob_arc Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_arc.html Generates an arc object starting at 0 degrees and ending at 60 degrees. ```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(p, label, ...) Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_path.md Constructor function to create an ob_path object. It accepts a set of points and various aesthetic parameters to define the path's appearance. ```APIDOC ## ob_path ### Description Creates an `ob_path` object, which represents a path defined by a sequence of points. This object can be used within `ggdiagram` to render paths with custom styling and arrow ornaments. ### Parameters - **p** (ob_point or list) - Required - An `ob_point` object or a list of `ob_point` objects defining the path coordinates. - **label** (character/angle/ob_label) - Optional - Label for the path. - **label_sloped** (logical) - Optional - Whether the label should be sloped with the curve. - **alpha** (numeric) - Optional - Alpha transparency value. - **arrow_head** (matrix) - Optional - 2-column matrix of polygon points for the arrow head. - **arrow_fins** (matrix) - Optional - 2-column matrix of polygon points for the arrow fins. - **arrowhead_length** (numeric/unit) - Optional - Size of arrow ornaments. - **color** (character) - Optional - Color of the path. - **fill** (character) - Optional - Fill color. - **linewidth** (numeric) - Optional - Width of the lines. - **id** (character) - Optional - Identifier for the object. ### Returns - **ob_path** (object) - An `ob_path` object. ``` -------------------------------- ### Load ggdiagram library Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/colors.md Initializes the ggdiagram package. ```r library(ggdiagram) ``` -------------------------------- ### Create complex path diagrams with loops and subsetting Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/automation.html Subsetting and loops allow for programmatic placement of variables and efficient connection of multiple paths in a model. ```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) ``` -------------------------------- ### Example: Plotting Cartesian and Polar Points Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_point.html Demonstrates how to add both 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") ``` -------------------------------- ### Create a Hierarchical Model Diagram Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/causalpaths.html Demonstrates creating complex models with aesthetic styling, including custom gradient fills and circular layouts. ```R my_serif_font <- "Equity Text A" str_narrow <- "Narrow Abilities" str_tests <- "Tests" str_specifics <- "Specifics" # gradient fills black_gradient <- grid::radialGradient( colours = rev(c("gray50", "gray2", "gray2")), stops = c(0, .35, 1), group = FALSE) black_gradient_rev <- grid::radialGradient( colours = c("gray50", "black"), group = FALSE) ggdiagram(font_family = my_serif_font, font_size = 16) + {g <- ob_circle( radius = 1.33, fill = NA, color = NA )} + geom_polygon( aes(x = x, y = y), data = ob_circle(radius = 9)@polygon, color = NA, fill = black_gradient) + geom_polygon( aes(x = x, y = y), data = g@polygon, color = NA, fill = black_gradient_rev) + {broad_angle <- turn(seq(0, 1, length.out = 9)[-9] + 1 / 16) broad_color <- hcl( h = seq(0, 360 - 360 / 8, 360 / 8) + 20, c = 55, l = 60) broad <- ob_circle( color = NA, fill = broad_color) |> place( from = g, where = broad_angle, sep = 1.15)} + ob_label( "*g*", color = "gray90", fill = NA, size = 60, vjust = .4, family = my_serif_font ) + connect(g, broad, color = broad_color, resect = 1, linewidth = .75, length_head = 5) + purrr::map(unbind(broad), \(b) { geom_polygon( ``` -------------------------------- ### Define an arc object Source: https://github.com/wjschne/ggdiagram/blob/main/docs/articles/arcwedges.md Creates an arc object using a center point, start and end angles, and a 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 ``` -------------------------------- ### bind method signature Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/bind.md The basic syntax for the bind function. ```r bind(x, ...) ``` -------------------------------- ### Convert polar coordinates to justification values Source: https://github.com/wjschne/ggdiagram/blob/main/man/polar2just.Rd Example showing the conversion of a named direction to horizontal and vertical justification values. ```R a <- "northwest" polar2just(a, axis = "h") polar2just(a, axis = "v") ``` -------------------------------- ### Example: Creating a Reuleaux triangle Source: https://github.com/wjschne/ggdiagram/blob/main/docs/reference/ob_reuleaux.html Creates 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 ```