### Install lavaanPlot from GitHub Source: https://github.com/alishinski/lavaanplot/blob/master/README.md Install the development version of the lavaanPlot package directly from GitHub using the devtools package. ```r install.packages("devtools") devtools::install_github("alishinski/lavaanPlot") ``` -------------------------------- ### Save and Embed Plot Examples Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Save_and_embed.html Demonstrates how to use `embed_plot_pdf` to save a plot as a PDF and embed it in Rmarkdown, and `save_png` to save the plot as a PNG file. ```R model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) pl <- lavaanPlot(model = fit) # Example for pdf embed embed_plot_pdf(pl, "plot2.pdf", width = 500) # Example for saving to .png save_png(pl, "plot.png", width = 500) ``` -------------------------------- ### Generate Basic Path Diagram with lavaanPlot Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html This example demonstrates how to create a basic path diagram from a fitted lavaan model. It includes options for customizing node and edge appearances and displaying coefficients. ```R HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data=HolzingerSwineford1939) labels = list(visual = "Visual Ability", textual = "Textual Ability", speed = "Speed Ability") # significant standardized paths only lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE) ``` -------------------------------- ### Plot Latent Variable Loadings Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html Visualizes latent variable loadings in an SEM. This example defines a model, fits it to data, and then plots the loadings with specified labels and options. ```R HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data=HolzingerSwineford1939) labels = list(visual = "Visual Ability", textual = "Textual Ability", speed = "Speed Ability") # Show coefs lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE) ``` ```R # Significant paths lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, sig = .05) ``` ```R # All paths standardized lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, stand = TRUE) ``` -------------------------------- ### Install lavaanPlot from CRAN Source: https://github.com/alishinski/lavaanplot/blob/master/README.md Install the stable version of the lavaanPlot package from the Comprehensive R Archive Network (CRAN). ```r install.packages("lavaanPlot") ``` -------------------------------- ### Fit lavaan Model and Plot Source: https://github.com/alishinski/lavaanplot/blob/master/README.md Fits a lavaan structural equation model and then generates a plot of the model. This is a basic example showing the core functionality. ```r library(lavaanPlot) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) summary(fit) ``` ```r lavaanPlot(model = fit, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = F) ``` -------------------------------- ### Plotting latent variable relationships Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html This example demonstrates plotting a model with latent variables. The `lavaanPlot` function can visualize complex relationships between latent and observed variables. ```R HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data=HolzingerSwineford1939) lavaanPlot(model = fit, edge_options = list(color = "grey")) ``` -------------------------------- ### Fit a Lavaan Model Source: https://github.com/alishinski/lavaanplot/blob/master/docs/index.html Define and fit a structural equation model using the lavaan package. This example uses a simple regression model with the mtcars dataset. ```r library(lavaan) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) summary(fit) ``` -------------------------------- ### Generate and Save lavaan Plot Source: https://github.com/alishinski/lavaanplot/blob/master/docs/index.html This snippet demonstrates how to fit a structural equation model using lavaan, generate a plot of the model, and save it as a PNG image. Ensure the 'lavaan' and 'lavaanPlot' packages are installed and loaded. ```R model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) pl <- lavaanPlot(model = fit) # Example for pdf embed embed_plot_pdf(pl, "plot2.pdf") # Example for saving to .png save_png(pl, "plot.png") ``` -------------------------------- ### Plotting Lavaan Model with Raw Coefficients Source: https://github.com/alishinski/lavaanplot/blob/master/vignettes/Intro_to_lavaanPlot.html Generates a path diagram for a lavaan model, displaying raw (unstandardized) coefficients on the edges. Ensure the 'lavaan' package is installed and a fitted model object is available. ```R lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE) ``` -------------------------------- ### Display Latent Path Significance Stars with lavaanPlot Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html This example demonstrates how to display significance stars for latent paths in the path diagram. Set the `stars` option to 'latent' to achieve this. ```R lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = "latent") ``` -------------------------------- ### Control Decimal Places in Coefficient Labels with lavaanPlot Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html This example illustrates how to control the number of decimal places displayed for coefficient values in the path diagram labels using the `digits` option. ```R lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, digits = 2) ``` -------------------------------- ### Display Covariance Significance Stars with lavaanPlot Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html This example shows how to add significance stars to the path diagram for covariances. Use the `stars` option set to 'covs' to display these stars. ```R lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = "covs") ``` -------------------------------- ### Defining a Confirmatory Factor Analysis Model Source: https://github.com/alishinski/lavaanplot/blob/master/vignettes/Intro_to_lavaanPlot.html Defines a CFA model structure using lavaan syntax. This example specifies three latent variables (visual, textual, speed) and their corresponding observed indicators (x1-x9). ```R HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' ``` -------------------------------- ### buildPaths() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Extracts the paths from the lavaan model. ```APIDOC ## buildPaths() ### Description Extracts the paths from the lavaan model. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Load lavaan Package Source: https://github.com/alishinski/lavaanplot/blob/master/README.md Loads the lavaan package. Ensure this is done before fitting any lavaan models. ```r library(lavaan) ``` -------------------------------- ### Load Required Libraries Source: https://github.com/alishinski/lavaanplot/blob/master/vignettes/Intro_to_lavaanPlot.html Load the lavaan and lavaanPlot libraries for model fitting and plotting. ```r library(lavaan) library(lavaanPlot) ``` -------------------------------- ### create_grviz() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Creates the grViz dot language code for plotting. ```APIDOC ## create_grviz() ### Description Creates the grViz dot language code for plotting. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Set Graph Layout to Neato Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html Use the `layout` argument within `graph_options` to set the graph visualization engine to 'neato'. This is useful for exploring different graph structures. ```R lavaanPlot(model = fit, labels = labels, graph_options = list(layout = "neato"), node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = TRUE, digits = 1) ``` ```json {"x":{"diagram":" digraph plot { \n graph [ layout = neato ] \n node [ shape = box, fontname = Helvetica ] \n node [shape = box] \n x1; x2; x3; x4; x5; x6; x7; x8; x9 \n node [shape = oval] \n visual; textual; speed \n visual [label = \"Visual Ability\"]\ntextual [label = \"Textual Ability\"]\nspeed [label = \"Speed Ability\"] \n edge [ color = grey ] \n visual->x1 [label = \"1\"] visual->x2 [label = \"0.6\"] visual->x3 [label = \"0.7\"] textual->x4 [label = \"1\"] textual->x5 [label = \"1.1\"] textual->x6 [label = \"0.9\"] speed->x7 [label = \"1\"] speed->x8 [label = \"1.2\"] speed->x9 [label = \"1.1\"] textual -> visual [label = \"0.4\", dir = \"both\"] speed -> visual [label = \"0.3\", dir = \"both\"] speed -> textual [label = \"0.2\", dir = \"both\"]\n}","config":{"engine":"dot","options":null}},"evals":[],"jsHooks":[]} ``` -------------------------------- ### Create Basic Path Diagrams with lavaanPlot Source: https://context7.com/alishinski/lavaanplot/llms.txt Generates a basic path diagram from a fitted lavaan regression model. Use custom styling for nodes and edges, and optionally display coefficients and significance. ```r library(lavaan) library(lavaanPlot) # Define and fit a regression model model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) # Basic plot with custom styling lavaanPlot( model = fit, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = FALSE ) # Add custom variable labels labels <- list( mpg = "Miles Per Gallon", cyl = "Cylinders", disp = "Displacement", hp = "Horsepower", qsec = "Speed", wt = "Weight" ) lavaanPlot( model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, # Show coefficient values on edges stand = TRUE, # Use standardized coefficients sig = 0.05, # Only show paths significant at p < .05 stars = c("regress"), # Add significance stars to regression paths digits = 2 # Decimal places for coefficients ) ``` -------------------------------- ### Basic lavaanPlot2 Usage Source: https://context7.com/alishinski/lavaanplot/llms.txt Demonstrates the basic usage of lavaanPlot2 to visualize a fitted lavaan model. Specify which model components to include, custom labels, and various graphical options for the plot, nodes, and edges. Significance stars and coefficient labels can also be displayed. ```r lavaanPlot2( fit, include = "covs", # Include covariances (NULL, "covs", or "all") labels = labels, graph_options = list(label = "CFA Model"), node_options = list(fontname = "Helvetica"), edge_options = list(color = "grey"), stars = c("latent"), # Significance stars for latent loadings coef_labels = TRUE, # Show coefficient values stand = TRUE # Use standardized coefficients ) ``` -------------------------------- ### Set Graph Layout to Twopi Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html Specify 'twopi' as the graph visualization engine using the `layout` argument in `graph_options`. This option provides a radial layout for the graph. ```R lavaanPlot(model = fit, labels = labels, graph_options = list(layout = "twopi"), node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = TRUE, digits = 1) ``` ```json {"x":{"diagram":" digraph plot { \n graph [ layout = twopi ] \n node [ shape = box, fontname = Helvetica ] \n node [shape = box] \n x1; x2; x3; x4; x5; x6; x7; x8; x9 \n node [shape = oval] \n visual; textual; speed \n visual [label = \"Visual Ability\"]\ntextual [label = \"Textual Ability\"]\nspeed [label = \"Speed Ability\"] \n edge [ color = grey ] \n visual->x1 [label = \"1\"] visual->x2 [label = \"0.6\"] visual->x3 [label = \"0.7\"] textual->x4 [label = \"1\"] textual->x5 [label = \"1.1\"] textual->x6 [label = \"0.9\"] speed->x7 [label = \"1\"] speed->x8 [label = \"1.2\"] speed->x9 [label = \"1.1\"] textual -> visual [label = \"0.4\", dir = \"both\"] speed -> visual [label = \"0.3\", dir = \"both\"] speed -> textual [label = \"0.2\", dir = \"both\"]\n}","config":{"engine":"dot","options":null}},"evals":[],"jsHooks":[]} ``` -------------------------------- ### formatting() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Enables conditional formatting for different parts of the model. ```APIDOC ## formatting() ### Description Enables conditional formatting for different parts of the model. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Set Graph Orientation to Bottom-to-Top with lavaanPlot Source: https://github.com/alishinski/lavaanplot/blob/master/vignettes/Intro_to_lavaanPlot.html Shows how to configure the graph orientation to Bottom-to-Top (BT) by using `rankdir = "BT"` within `graph_options`. This reverses the typical top-down layout. ```R lavaanPlot(model = fit, labels = labels, graph_options = list(rankdir = "BT"), node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = TRUE, digits = 1) ``` -------------------------------- ### lavaanPlot() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Plots lavaan path model with DiagrammeR. ```APIDOC ## lavaanPlot() ### Description Plots lavaan path model with DiagrammeR. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Control Graph Layout with lavaanPlot Source: https://context7.com/alishinski/lavaanplot/llms.txt Customize the layout and orientation of path diagrams using Graphviz layout engines and direction settings. Supports 'TB', 'BT', 'LR', 'RL' for direction and 'dot', 'neato', 'circo', 'twopi' for engines. ```r library(lavaan) library(lavaanPlot) HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data = HolzingerSwineford1939) labels <- list(visual = "Visual", textual = "Textual", speed = "Speed") # Change orientation: TB (top-bottom), BT (bottom-top), LR (left-right), RL (right-left) lavaanPlot( model = fit, labels = labels, graph_options = list(rankdir = "LR"), # Left to right layout node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE ) # Use different layout engines: dot, neato, circo, twopi lavaanPlot( model = fit, labels = labels, graph_options = list(layout = "circo"), # Circular layout node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE ) ``` -------------------------------- ### create_nodes() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Creates node data frame and adds formatting. ```APIDOC ## create_nodes() ### Description Creates node data frame and adds formatting. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### lavaanPlot2() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Plots lavaan path model with DiagrammeR. ```APIDOC ## lavaanPlot2() ### Description Plots lavaan path model with DiagrammeR. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### buildCall() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Builds the Diagrammer function call. ```APIDOC ## buildCall() ### Description Builds the Diagrammer function call. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Set Graph Layout to Circo Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html Utilize the `layout` argument within `graph_options` to specify 'circo' as the graph visualization engine. This offers an alternative circular layout for the diagram. ```R lavaanPlot(model = fit, labels = labels, graph_options = list(layout = "circo"), node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = TRUE, digits = 1) ``` ```json {"x":{"diagram":" digraph plot { \n graph [ layout = circo ] \n node [ shape = box, fontname = Helvetica ] \n node [shape = box] \n x1; x2; x3; x4; x5; x6; x7; x8; x9 \n node [shape = oval] \n visual; textual; speed \n visual [label = \"Visual Ability\"]\ntextual [label = \"Textual Ability\"]\nspeed [label = \"Speed Ability\"] \n edge [ color = grey ] \n visual->x1 [label = \"1\"] visual->x2 [label = \"0.6\"] visual->x3 [label = \"0.7\"] textual->x4 [label = \"1\"] textual->x5 [label = \"1.1\"] textual->x6 [label = \"0.9\"] speed->x7 [label = \"1\"] speed->x8 [label = \"1.2\"] speed->x9 [label = \"1.1\"] textual -> visual [label = \"0.4\", dir = \"both\"] speed -> visual [label = \"0.3\", dir = \"both\"] speed -> textual [label = \"0.2\", dir = \"both\"]\n}","config":{"engine":"dot","options":null}},"evals":[],"jsHooks":[]} ``` -------------------------------- ### Fitting a Lavaan Model Source: https://github.com/alishinski/lavaanplot/blob/master/vignettes/Intro_to_lavaanPlot.html Fits a specified lavaan model to the provided data. This step is necessary before visualization. Ensure the 'lavaan' package is loaded and the data is available. ```R fit <- cfa(HS.model, data=HolzingerSwineford1939) ``` -------------------------------- ### Generate Default Graph with lavaanPlot Source: https://github.com/alishinski/lavaanplot/blob/master/vignettes/Intro_to_lavaanPlot.html This snippet shows the basic usage of lavaanPlot to generate a default graph. It includes model fitting, label definition, and plotting with specified node and edge options. ```R lavaanPlot(model = fit, labels = labels, graph_options = list(overlap = TRUE, fontsize = 10), node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = TRUE, digits = 1) ``` -------------------------------- ### Plot SEM with Standardized Coefficients Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html Plots an SEM, showing only standardized coefficients. Use the `stand = TRUE` argument for this visualization. ```R lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, stand = TRUE) ``` -------------------------------- ### create_edges() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Creates edge data frame and adds formatting. ```APIDOC ## create_edges() ### Description Creates edge data frame and adds formatting. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Generate Lavaan Path Diagram Source: https://github.com/alishinski/lavaanplot/blob/master/docs/index.html Create a path diagram for a fitted lavaan model using the lavaanPlot function. Customize node and edge appearance, and choose whether to display coefficients. ```r lavaanPlot(model = fit, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = F) ``` -------------------------------- ### getNodes() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Extracts the paths from the lavaan model. ```APIDOC ## getNodes() ### Description Extracts the paths from the lavaan model. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### save_png Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/save_png.html Saves a lavaanPlot visualization as a PNG image file. ```APIDOC ## save_png ### Description Saves a plot as a png ### Method Not applicable (R function) ### Endpoint Not applicable (R function) ### Parameters #### Arguments - **plot** (plot object) - Required - plot object created by `lavaanPlot` - **path** (string) - Required - filename to save the image - **width** (numeric) - Optional - width of image in pixels, NULL for default - **height** (numeric) - Optional - height of image, NULL for default ### Request Example ```R library(lavaan) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) pl <- lavaanPlot(model = fit) if (FALSE) { save_png(pl, "plot.png") } ``` ### Response #### Success Response No return value; saves plot as png. ``` -------------------------------- ### Plot lavaan path model with DiagrammeR Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/lavaanPlot.html Use this function to generate a path diagram for a lavaan model fit. Specify node and edge options for customization. Set `coefs = FALSE` to hide coefficients. ```R library(lavaan) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) lavaanPlot(model = fit, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = FALSE) ``` -------------------------------- ### Generate DOT Specification for Graphviz Source: https://context7.com/alishinski/lavaanplot/llms.txt Converts node and edge data frames into a Graphviz DOT language specification string. This string can be passed to `grViz()` for rendering or used for custom processing. `graph_options` can set attributes like `rankdir` and `label`. ```r library(lavaan) library(lavaanPlot) library(DiagrammeR) HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data = HolzingerSwineford1939) coefs <- extract_coefs(fit, include = "covs") ndf <- create_nodes(coefs, labels = NULL, node_options = list(fontname = "Helvetica")) edf <- create_edges(coefs, ndf, edge_options = list(color = "grey"), coef_labels = TRUE) # Generate DOT specification dot_spec <- convert_graph( ndf, edf, graph_options = list(rankdir = "TB", label = "CFA Model") ) # Print DOT code for inspection cat(dot_spec) # Render directly with grViz grViz(dot_spec) ``` -------------------------------- ### Save Plot as PNG Image Source: https://context7.com/alishinski/lavaanplot/llms.txt Saves a lavaanPlot visualization as a PNG image file. Requires `DiagrammeRsvg`, `rsvg`, and `png` packages. Custom dimensions can be specified in pixels using `width` and `height` arguments. ```r library(lavaan) library(lavaanPlot) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) # Create the plot pl <- lavaanPlot( model = fit, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE ) # Save as PNG with default dimensions save_png(pl, "path_diagram.png") # Save with custom dimensions (in pixels) save_png(pl, "path_diagram_large.png", width = 800, height = 600) ``` -------------------------------- ### sig_stars() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Generates standard significance stars. ```APIDOC ## sig_stars() ### Description Generates standard significance stars. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Build Call Function Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/buildCall.html This function constructs the Diagrammer function call used for plotting lavaan models. ```APIDOC ## buildCall ### Description Builds the Diagrammer function call. ### Method Not applicable (R function) ### Endpoint Not applicable (R function) ### Parameters #### Arguments - **model** (lavaan fit object) - Required - A model fit object of class lavaan. - **name** (string) - Required - A string of the name of the plot. - **labels** (named list) - Optional - An optional named list of variable labels fit object of class lavaan. - **graph_options** (named list) - Optional - A named list of graph options for Diagrammer syntax. - **node_options** (named list) - Optional - A named list of node options for Diagrammer syntax. - **edge_options** (named list) - Optional - A named list of edge options for Diagrammer syntax. - **...** - Additional arguments to be passed to `buildPaths` ### Request Example ```R # Example usage (assuming 'fit' is a lavaan fit object) buildCall(model = fit, name = "my_plot", labels = list(var1 = "Variable 1")) ``` ### Response #### Success Response - **string** - A string specifying the path diagram for the `model`. #### Response Example ``` "digraph G { overlap=true; fontsize=10; node [shape=box]; edge [color=black]; ... }" ``` ``` -------------------------------- ### Plot SEM Model with Lavaan Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html Use lavaanPlot to visualize a fitted SEM model. Customize node and edge appearance, and display coefficients and covariances with significance stars. Requires a fitted lavaan model object. ```R lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = TRUE, digits = 1) ``` -------------------------------- ### Graphviz DOT language for path diagram Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/lavaanPlot.html This is the Graphviz DOT language representation of the path diagram generated by lavaanPlot. It defines nodes, edges, and their attributes. ```dot digraph plot { graph [ overlap = true, fontsize = 10 ] node [ shape = box, fontname = Helvetica ] node [shape = box] cyl; disp; hp; wt; mpg; qsec node [shape = oval] edge [ color = grey ] cyl->mpg disp->mpg hp->mpg disp->qsec hp->qsec wt->qsec } ``` -------------------------------- ### Fit a lavaan model Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html Fit a structural equation model using the lavaan package. This is a prerequisite for using lavaanPlot. ```r library(lavaan) ## This is lavaan 0.6-11 ## lavaan is FREE software! Please report any bugs. library(lavaanPlot) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) summary(fit) ## lavaan 0.6-11 ended normally after 32 iterations ## ## Estimator ML ## Optimization method NLMINB ## Number of model parameters 9 ## ## Number of observations 32 ## ## Model Test User Model: ## ## Test statistic 18.266 ## Degrees of freedom 2 ## P-value (Chi-square) 0.000 ## ## Parameter Estimates: ## ## Standard errors Standard ## Information Expected ## Information saturated (h1) model Structured ## ## Regressions: ## Estimate Std.Err z-value P(>|z|) ## mpg ~ ## cyl -0.987 0.738 -1.337 0.181 ## disp -0.021 0.010 -2.178 0.029 ## hp -0.017 0.014 -1.218 0.223 ## qsec ~ ## disp -0.008 0.004 -2.122 0.034 ## hp -0.023 0.004 -5.229 0.000 ## wt 1.695 0.398 4.256 0.000 ## ## Covariances: ## Estimate Std.Err z-value P(>|z|) ## .mpg ~~ ## .qsec 0.447 0.511 0.874 0.382 ## ## Variances: ## Estimate Std.Err z-value P(>|z|) ## .mpg 8.194 2.049 4.000 0.000 ## .qsec 0.996 0.249 4.000 0.000 ``` -------------------------------- ### buildPaths Function Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/buildPaths.html Extracts the paths from a lavaan model fit object to generate a plot. ```APIDOC ## buildPaths Function ### Description Extracts the paths from the lavaan model. ### Method Not applicable (R function) ### Endpoint Not applicable (R function) ### Parameters #### Arguments - **fit** (lavaan object) - A model fit object of class lavaan. - **coefs** (logical) - whether or not to include significant path coefficient values in diagram. - **sig** (numeric) - significance level for determining what significant paths are. - **stand** (logical) - Should the coefficients being used be standardized coefficients. - **covs** (logical) - Should model covariances be included in the diagram. - **stars** (character vector) - a character vector indicating which parameters should include significance stars be included for regression paths, latent paths, or covariances. Include which of the 3 you want ("regress", "latent", "covs"), default is none. - **digits** (numeric) - A number indicating the desired number of digits for the coefficient values in the plot. ### Request Example ```R # Example usage (assuming 'fit' is a lavaan model fit object) # buildPaths(fit, coefs = TRUE, sig = 0.05, stand = TRUE, covs = TRUE, stars = c("regress", "covs"), digits = 3) ``` ### Response #### Success Response Returns a plot object representing the lavaan model paths. #### Response Example (Output is a plot, not a JSON object. Example would be a visual representation of the model.) ``` -------------------------------- ### Set Plot Orientation to Bottom-to-Top (BT) Source: https://github.com/alishinski/lavaanplot/blob/master/docs/articles/Intro_to_lavaanPlot.html Use the `rankdir` option within `graph_options` to set the plot orientation to Bottom-to-Top. This orientation is useful for visualizing hierarchical structures where the base is at the bottom and flows upwards. ```R lavaanPlot(model = fit, labels = labels, graph_options = list(rankdir = "BT"), node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = TRUE, digits = 1) ``` ```dot digraph plot { graph [ rankdir = BT ] node [ shape = box, fontname = Helvetica ] node [shape = box] x1; x2; x3; x4; x5; x6; x7; x8; x9 node [shape = oval] visual; textual; speed visual [label = "Visual Ability"] textual [label = "Textual Ability"] speed [label = "Speed Ability"] edge [ color = grey ] visual->x1 [label = "1"] visual->x2 [label = "0.6"] visual->x3 [label = "0.7"] textual->x4 [label = "1"] textual->x5 [label = "1.1"] textual->x6 [label = "0.9"] speed->x7 [label = "1"] speed->x8 [label = "1.2"] speed->x9 [label = "1.1"] textual -> visual [label = "0.4", dir = "both"] speed -> visual [label = "0.3", dir = "both"] speed -> textual [label = "0.2", dir = "both"] } ``` -------------------------------- ### lavaanPlot Function Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/lavaanPlot.html This section details the main function `lavaanPlot` used for generating path diagrams from lavaan model fits. ```APIDOC ## lavaanPlot Function ### Description Plots lavaan path model with DiagrammeR. ### Method Function Call ### Endpoint N/A (R function) ### Parameters #### Arguments - **model** (lavaan fit object) - Required - A model fit object of class lavaan. - **name** (string) - Optional - A string of the name of the plot. Defaults to "plot". - **labels** (named list) - Optional - An optional named list of variable labels. - **...** - Additional arguments to be called to `buildCall` and `buildPaths`. ### Value - A Diagrammer plot of the path diagram for `model`. ### Examples ```R library(lavaan) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) lavaanPlot(model = fit, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = FALSE) ``` ### Response Example ```json { "x": { "diagram": " digraph plot { \n graph [ overlap = true, fontsize = 10 ] \n node [ shape = box, fontname = Helvetica ] \n node [shape = box] \n cyl; disp; hp; wt; mpg; qsec \n node [shape = oval] \n \n \n edge [ color = grey ] \n cyl->mpg disp->mpg hp->mpg disp->qsec hp->qsec wt->qsec \n} ", "config": { "engine": "dot", "options": null } }, "evals": [], "jsHooks": [] } ``` ``` -------------------------------- ### Basic lavaan Plot Source: https://github.com/alishinski/lavaanplot/blob/master/vignettes/Intro_to_lavaanPlot.html Generates a basic path diagram for a lavaan model fit. Customize node and edge appearance using options. ```R lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = FALSE) ``` -------------------------------- ### Plotting with Coefficients Source: https://github.com/alishinski/lavaanplot/blob/master/vignettes/Intro_to_lavaanPlot.html Displays path coefficients on the plot edges. Ensure the model fit object is generated using `sem()` or `cfa()`. ```R model <- 'mpg ~cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) summary(fit) ## lavaan 0.6-10 ended normally after 32 iterations ## ## Estimator ML ## Optimization method NLMINB ## Number of model parameters 9 ## ## Number of observations 32 ## ## Model Test User Model: ## ## Test statistic 18.266 ## Degrees of freedom 2 ## P-value (Chi-square) 0.000 ## ## Parameter Estimates: ## ## Standard errors Standard ## Information Expected ## Information saturated (h1) model Structured ## ## Regressions: ## Estimate Std.Err z-value P(>|z|) ## mpg ~ ## cyl -0.987 0.738 -1.337 0.181 ## disp -0.021 0.010 -2.178 0.029 ## hp -0.017 0.014 -1.218 0.223 ## qsec ~ ## disp -0.008 0.004 -2.122 0.034 ## hp -0.023 0.004 -5.229 0.000 ## wt 1.695 0.398 4.256 0.000 ## ## Covariances: ## Estimate Std.Err z-value P(>|z|) ## .mpg ~~ ## .qsec 0.447 0.511 0.874 0.382 ## ## Variances: ## Estimate Std.Err z-value P(>|z|) ## .mpg 8.194 2.049 4.000 0.000 ## .qsec 0.996 0.249 4.000 0.000 lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE) ``` -------------------------------- ### convert_graph() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Uses the diagrammeR functions to turn the ndf and edf into dot. ```APIDOC ## convert_graph() ### Description Uses the diagrammeR functions to turn the ndf and edf into dot. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Create Edge Data Frame with Labels and Stars Source: https://context7.com/alishinski/lavaanplot/llms.txt Generates an edge data frame from extracted coefficients, supporting coefficient labels, significance stars, and conditional formatting. Use `coef_labels = TRUE` to show coefficient values and `stars` to specify significance levels for loadings and covariances. ```r library(lavaan) library(lavaanPlot) HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data = HolzingerSwineford1939) coefs <- extract_coefs(fit, include = "covs", stand = TRUE) ndf <- create_nodes(coefs, labels = NULL, node_options = list(fontname = "Helvetica")) # Create edges with coefficient labels and significance stars edf <- create_edges( coefs, ndf, edge_options = list(color = "grey"), coef_labels = TRUE, # Show coefficient values stand = TRUE, # Use standardized coefficients stars = c("latent", "covs"), # Stars for loadings and covariances sig = 0.05 # Significance threshold ) # Create edges with conditional formatting edge_fmt <- formatting( list(color = "black"), # Regression list(color = "blue"), # Latent loadings list(color = "red"), # Covariances type = "edge" ) edf_formatted <- create_edges(coefs, ndf, edge_options = edge_fmt, coef_labels = TRUE) ``` -------------------------------- ### Create Node Data Frame with create_nodes Source: https://context7.com/alishinski/lavaanplot/llms.txt Generates a DiagrammeR node data frame from extracted lavaan coefficients. Supports custom labels and conditional node formatting. This function is useful for preparing node data before plotting with lavaanPlot2 or other DiagrammeR-based visualizations. ```r library(lavaan) library(lavaanPlot) HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data = HolzingerSwineford1939) # Extract coefficients coefs <- extract_coefs(fit, include = "covs") # Create nodes with labels and basic options labels <- list(visual = "Visual", textual = "Textual", speed = "Speed") ndf <- create_nodes( coefs, labels = labels, node_options = list(fontname = "Helvetica", fontsize = "12") ) # Create nodes with conditional formatting node_fmt <- formatting( list(shape = "ellipse", color = "blue"), # Latent list(shape = "box", color = "black"), # Observed type = "node" ) ndf_formatted <- create_nodes(coefs, labels = labels, node_options = node_fmt) ``` -------------------------------- ### buildLabels() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Adds variable labels to the Diagrammer plot function call. ```APIDOC ## buildLabels() ### Description Adds variable labels to the Diagrammer plot function call. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Create Latent Variable Model Diagrams with lavaanPlot Source: https://context7.com/alishinski/lavaanplot/llms.txt Generates path diagrams for latent variable models (CFA). Latent variables are ovals, observed variables are boxes. Customize with labels, coefficients, covariances, and significance stars. ```r library(lavaan) library(lavaanPlot) # Define a latent variable model HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data = HolzingerSwineford1939) # Create labels for latent variables labels <- list( visual = "Visual Ability", textual = "Textual Ability", speed = "Speed Ability" ) # Plot with covariances and significance stars lavaanPlot( model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, # Show factor loadings covs = TRUE, # Include covariance paths stars = c("latent", "covs"), # Stars for loadings and covariances stand = TRUE # Standardized coefficients ) ``` -------------------------------- ### extract_coefs() Source: https://github.com/alishinski/lavaanplot/blob/master/docs/reference/index.html Creates a data frame of the parameter table from lavaan model. ```APIDOC ## extract_coefs() ### Description Creates a data frame of the parameter table from lavaan model. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters N/A ### Request Example N/A ### Response N/A ```