Title: | Interactively Visualize Structural Equation Modeling Diagrams |
---|---|
Description: | It is an R package and web-based application, allowing users to perform interactive and reproducible visualizations of path diagrams for structural equation modeling (SEM) and networks using the 'ggplot2' engine. Its app (built with 'shiny') provides an interface that allows extensive customization, and creates CSV outputs, which can then be used to recreate the figures either using the web app or script-based workflow. |
Authors: | Seung Hyun Min [aut, cre] |
Maintainer: | Seung Hyun Min <[email protected]> |
License: | GPL-2 |
Version: | 0.2.4 |
Built: | 2025-02-04 01:20:24 UTC |
Source: | https://github.com/smin95/ggsem |
This function modifies the axis ranges of a ggplot object, with optional user-specified
ranges, additional buffers, and the ability to enforce a fixed aspect ratio. This is a modified
version of adjust_axis_space()
.
adjust_axis_range( plot, x_range = NULL, y_range = NULL, buffer_percent = 0, fixed_aspect_ratio = TRUE )
adjust_axis_range( plot, x_range = NULL, y_range = NULL, buffer_percent = 0, fixed_aspect_ratio = TRUE )
plot |
A ggplot object. The plot whose axis ranges are to be adjusted. |
x_range |
A numeric vector of length 2 specifying the desired x-axis range.
If |
y_range |
A numeric vector of length 2 specifying the desired y-axis range.
If |
buffer_percent |
A numeric value indicating the percentage of additional space to add to each axis range as a buffer. Default is '0' (no buffer). |
fixed_aspect_ratio |
A logical value indicating whether to maintain a fixed
aspect ratio for the plot. If |
- If 'x_range' or 'y_range' are provided, these values will override the current axis ranges. - The 'buffer_percent' parameter adds proportional space to the axis ranges, calculated as a percentage of the range's width or height. - When 'fixed_aspect_ratio' is 'TRUE', the function adjusts either the x-axis or y-axis to ensure the plot maintains a fixed aspect ratio.
A modified ggplot object with adjusted axis ranges.
# CSV files from ggsem app points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) lines_data <- data.frame( x_start = 2, y_start = -2, x_end = 10, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) p <- csv_to_ggplot(points_data = points_data, lines_data = lines_data, zoom_level = 1.2, # Value from the ggsem app horizontal_position = 0, # Value from the ggsem app element_order = c('lines', 'points')) # order priority: lines < points adjust_axis_range(p, x_range = c(-30,30), y_range= c(-30,30))
# CSV files from ggsem app points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) lines_data <- data.frame( x_start = 2, y_start = -2, x_end = 10, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) p <- csv_to_ggplot(points_data = points_data, lines_data = lines_data, zoom_level = 1.2, # Value from the ggsem app horizontal_position = 0, # Value from the ggsem app element_order = c('lines', 'points')) # order priority: lines < points adjust_axis_range(p, x_range = c(-30,30), y_range= c(-30,30))
This function allows users to remove or manage whitespace around graphical elements. It supports asymmetrical adjustments for each boundary (left, right, bottom, and top). Users can also maintain a fixed aspect ratio if required.
adjust_axis_space( plot, x_adjust_left_percent = 0, x_adjust_right_percent = 0, y_adjust_bottom_percent = 0, y_adjust_top_percent = 0, fixed_aspect_ratio = TRUE )
adjust_axis_space( plot, x_adjust_left_percent = 0, x_adjust_right_percent = 0, y_adjust_bottom_percent = 0, y_adjust_top_percent = 0, fixed_aspect_ratio = TRUE )
plot |
A ggplot2 object. The plot whose axis ranges need adjustment. |
x_adjust_left_percent |
Numeric. Percentage by which to expand the left boundary of the x-axis. Default is |
x_adjust_right_percent |
Numeric. Percentage by which to expand the right boundary of the x-axis. Default is |
y_adjust_bottom_percent |
Numeric. Percentage by which to expand the bottom boundary of the y-axis. Default is |
y_adjust_top_percent |
Numeric. Percentage by which to expand the top boundary of the y-axis. Default is |
fixed_aspect_ratio |
Logical. If |
- **Percentage Adjustments:** The percentages provided for each axis boundary are calculated based on the current axis range. For example, x_adjust_left_percent = 10
expands the left boundary by 10
- **Fixed Aspect Ratio:** When fixed_aspect_ratio = TRUE
, the function adjusts either the x-axis or y-axis to maintain a 1:1 aspect ratio. The larger adjustment determines the scaling for both axes.
A ggplot2 object with adjusted axis ranges. The adjusted plot retains its original attributes and is compatible with additional ggplot2 layers and themes.
# CSV files from ggsem app points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) lines_data <- data.frame( x_start = 2, y_start = -2, x_end = 10, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) p <- csv_to_ggplot(points_data = points_data, lines_data = lines_data, zoom_level = 1.2, # Value from the ggsem app horizontal_position = 0, # Value from the ggsem app element_order = c('lines', 'points')) # order priority: lines < points adjust_axis_space(p, x_adjust_left_percent = 10, x_adjust_right_percent = 10, y_adjust_bottom_percent = 5, y_adjust_top_percent = 5)
# CSV files from ggsem app points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) lines_data <- data.frame( x_start = 2, y_start = -2, x_end = 10, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) p <- csv_to_ggplot(points_data = points_data, lines_data = lines_data, zoom_level = 1.2, # Value from the ggsem app horizontal_position = 0, # Value from the ggsem app element_order = c('lines', 'points')) # order priority: lines < points adjust_axis_space(p, x_adjust_left_percent = 10, x_adjust_right_percent = 10, y_adjust_bottom_percent = 5, y_adjust_top_percent = 5)
This function converts the CSV files exported from the ggsem Shiny app into a customizable ggplot object. The resulting plot is compatible with ggplot2 functions, allowing users to modify it further (e.g., adding titles or annotations).
csv_to_ggplot( points_data = NULL, lines_data = NULL, annotations_data = NULL, loops_data = NULL, element_order = c("lines", "points", "self_loops", "annotations"), zoom_level = 1.2, horizontal_position = 0, vertical_position = 0, n = 100 )
csv_to_ggplot( points_data = NULL, lines_data = NULL, annotations_data = NULL, loops_data = NULL, element_order = c("lines", "points", "self_loops", "annotations"), zoom_level = 1.2, horizontal_position = 0, vertical_position = 0, n = 100 )
points_data |
A data frame containing point data exported from the ggsem Shiny app. Default is |
lines_data |
A data frame containing line data exported from the ggsem Shiny app. Default is 'NULL'. |
annotations_data |
A data frame containing text annotation data exported from the ggsem Shiny app. Default is |
loops_data |
A data frame containing self-loop arrow data exported from the ggsem Shiny app. Default is |
element_order |
A character vector specifying the order in which graphical elements are added to the plot.
For example: |
zoom_level |
A numeric value controlling the zoom level of the plot. A value >1 zooms in; <1 zooms out. Default is |
horizontal_position |
A numeric value to shift the plot horizontally. Default is |
vertical_position |
A numeric value to shift the plot vertically. Default is |
n |
Number of points used for interpolation in gradient or curved lines. Default is |
- The function uses 'coord_fixed' to ensure square plotting space and uniform scaling. - The 'element_order' parameter determines the layering of graphical elements, with later elements appearing on top. - The 'axis_ranges' attribute is attached to the plot for additional programmatic access.
A ggplot object with an axis_ranges
attribute specifying the x and y axis ranges after adjustments.
# CSV files from ggsem app points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) lines_data <- data.frame( x_start = 2, y_start = -2, x_end = 10, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) csv_to_ggplot(points_data = points_data, lines_data = lines_data, zoom_level = 1.2, # Value from the ggsem app horizontal_position = 0, # Value from the ggsem app element_order = c('lines', 'points')) # order priority: lines < points
# CSV files from ggsem app points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) lines_data <- data.frame( x_start = 2, y_start = -2, x_end = 10, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) csv_to_ggplot(points_data = points_data, lines_data = lines_data, zoom_level = 1.2, # Value from the ggsem app horizontal_position = 0, # Value from the ggsem app element_order = c('lines', 'points')) # order priority: lines < points
This function overlays text annotations onto any ggplot object. It is particularly useful for adding annotations from CSV files generated by the ggsem Shiny app but can also be used with custom annotation data.
draw_annotations(p, annotations_data, zoom_level = 1)
draw_annotations(p, annotations_data, zoom_level = 1)
p |
A ggplot object. The plot to which the annotations will be added. |
annotations_data |
A data frame containing annotation information. Typically, this comes from a CSV file generated by the ggsem Shiny app. The required columns include:
|
zoom_level |
Numeric. Adjusts the size of annotations based on the zoom level. Default is |
A ggplot object with the specified annotations added.
library(ggplot2) annotations_data <- data.frame( text = 'Square One', x = 26, y = 300, font = 'serif', size = 20, color = '#000000', angle = 0, alpha = 1, fontface = 'bold', math_expression = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) p <- ggplot() draw_annotations(p, annotations_data, zoom_level = 1.2)
library(ggplot2) annotations_data <- data.frame( text = 'Square One', x = 26, y = 300, font = 'serif', size = 20, color = '#000000', angle = 0, alpha = 1, fontface = 'bold', math_expression = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) p <- ggplot() draw_annotations(p, annotations_data, zoom_level = 1.2)
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.
draw_lines(p, lines_data, zoom_level = 1, n = n)
draw_lines(p, lines_data, zoom_level = 1, n = n)
p |
A ggplot object to which the lines will be added. |
lines_data |
A data frame containing line information. The expected columns include:
|
zoom_level |
Numeric. Adjusts the size of line widths and arrowheads relative to the plot. Default is |
n |
Integer. Number of points for interpolation in gradient or curved lines. Default is |
A ggplot object with the specified lines or arrows added.
library(ggplot2) lines_df <- data.frame( x_start = 2, y_start = -2, x_end = 6, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) p <- ggplot() draw_lines(p, lines_data = lines_df, zoom_level = 1.2, n = 200)
library(ggplot2) lines_df <- data.frame( x_start = 2, y_start = -2, x_end = 6, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) p <- ggplot() draw_lines(p, lines_data = lines_df, zoom_level = 1.2, n = 200)
This function overlays self-loop arrows to a ggplot object based on data describing their positions, sizes, orientations, and styles. Self-loop arrows can be drawn in one direction or bidirectionally with customizable parameters such as color, width, and arrow type. The data can come from a CSV file generated by the ggsem Shiny app or custom input.
draw_loops(p, loops_data, zoom_level = 1)
draw_loops(p, loops_data, zoom_level = 1)
p |
A ggplot object to which the self-loop arrows will be added. |
loops_data |
A data frame containing information about the self-loop arrows. The expected columns include:
|
zoom_level |
Numeric. Adjusts the size of line widths and arrowheads relative to the plot. Default is |
A ggplot object with the specified self-loop arrows added.
library(ggplot2) loops_data <- data.frame( x_center = -5, y_center = 5, radius = 2, color = '#000000', width = 1, alpha = 1, arrow_type = 'closed', arrow_size = 0.1, gap_size = 0.2, loop_width = 5, loop_height = 5, orientation = 0, two_way = FALSE, locked = FALSE ) p <- ggplot() draw_loops(p, loops_data, zoom_level = 1.2)
library(ggplot2) loops_data <- data.frame( x_center = -5, y_center = 5, radius = 2, color = '#000000', width = 1, alpha = 1, arrow_type = 'closed', arrow_size = 0.1, gap_size = 0.2, loop_width = 5, loop_height = 5, orientation = 0, two_way = FALSE, locked = FALSE ) p <- ggplot() draw_loops(p, loops_data, zoom_level = 1.2)
This function overlays points to a ggplot object using data from a CSV file generated by the ggsem Shiny app or any custom dataset. Points can be styled with various shapes, colors, sizes, and orientations.
draw_points(p, points_data, zoom_level = 1)
draw_points(p, points_data, zoom_level = 1)
p |
A ggplot object to which the points will be added. |
points_data |
A data frame containing information about the points to be drawn. The expected columns include:
|
zoom_level |
Numeric. Adjusts the size of the points relative to the plot. Default is |
A ggplot object with the specified points added.
library(ggplot2) points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) p <- ggplot() draw_points(p, points_data, zoom_level = 1.2) + scale_x_continuous(limits = c(0,50)) + scale_y_continuous(limits = c(0,50))
library(ggplot2) points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) p <- ggplot() draw_points(p, points_data, zoom_level = 1.2) + scale_x_continuous(limits = c(0,50)) + scale_y_continuous(limits = c(0,50))
A function to calculate the range of x- and y- axes.
get_axis_range(plot)
get_axis_range(plot)
plot |
ggplot output from csv_to_ggplot() |
A list object that has two elements, each of which has two vector values. The first element stores the minimum and maximum values of the plot's x-axis range, while the second element stores the minimum and maximum values of the plot's y-axis range.
library(ggplot2) ggplot(mtcars) + geom_point(aes(mpg, disp)) -> p1 get_axis_range(p1)
library(ggplot2) ggplot(mtcars) + geom_point(aes(mpg, disp)) -> p1 get_axis_range(p1)
Run ggsem (shiny app) locally through a browser
ggsem()
ggsem()
No return value, called for side effects
This function saves a ggplot object (created from 'csv_to_ggplot()' function) to a file with dimensions automatically determined based on the x-axis and y-axis ranges of the plot. The size of the output can be further controlled using addtional arguments.
save_figure( filename, plot, units = "in", dpi = 300, aspect_ratio = NULL, scale_factor = 0.11, ... )
save_figure( filename, plot, units = "in", dpi = 300, aspect_ratio = NULL, scale_factor = 0.11, ... )
filename |
A string. The name of the output file (e.g., "plot.png"). |
plot |
A ggplot object to save. |
units |
A string. Units for width and height. Default is |
dpi |
Numeric. Resolution of the output file in dots per inch. Default is 300. |
aspect_ratio |
Numeric or |
scale_factor |
Numeric. A scaling factor to control the overall size of the saved plot. Default is |
... |
Additional arguments passed to |
Saves the ggplot object to the specified file and does not return a value.
## Not run: # CSV files from ggsem app points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) lines_data <- data.frame( x_start = 2, y_start = -2, x_end = 10, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) p <- csv_to_ggplot(points_data = points_data, lines_data = lines_data, zoom_level = 1.2, # Value from the ggsem app horizontal_position = 0, # Value from the ggsem app element_order = c('lines', 'points')) # order priority: lines < points p1 <- adjust_axis_range(p, x_adjust_left_percent = 10, x_adjust_right_percent = 10, y_adjust_bottom_percent = 5, y_adjust_top_percent = 5) # Save with default scaling save_figure("p1.png", p1) ## End(Not run)
## Not run: # CSV files from ggsem app points_data <- data.frame( x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50, border_color = '#9646D4', border_width = 2, alpha = 1, width_height_ratio = 1.6, orientation = 45, lavaan = FALSE, lavaan = FALSE, network = FALSE, locked = FALSE ) lines_data <- data.frame( x_start = 2, y_start = -2, x_end = 10, y_end = -2, ctrl_x = NA, ctrl_y = NA, type = 'Straight Line', color = '#000000', end_color = '#cc3d3d', color_type = 'Gradient', gradient_position = 0.35, width = 1.5, alpha = 1, arrow = FALSE, arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE, network = FALSE, line_style = 'solid', locked = FALSE ) p <- csv_to_ggplot(points_data = points_data, lines_data = lines_data, zoom_level = 1.2, # Value from the ggsem app horizontal_position = 0, # Value from the ggsem app element_order = c('lines', 'points')) # order priority: lines < points p1 <- adjust_axis_range(p, x_adjust_left_percent = 10, x_adjust_right_percent = 10, y_adjust_bottom_percent = 5, y_adjust_top_percent = 5) # Save with default scaling save_figure("p1.png", p1) ## End(Not run)