### Example: Create and Embed Plot Source: https://lavaanplot.alexlishinski.com/reference/embed_plot_pdf This example demonstrates how to fit a lavaan model, generate a plot using lavaanPlot, and then embed that plot into a PDF. The code is wrapped in `if (FALSE)` to prevent execution during standard R checks. ```R library(lavaan) #> This is lavaan 0.6-19 #> lavaan is FREE software! Please report any bugs. model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) pl <- lavaanPlot(model = fit) ``` -------------------------------- ### Install lavaanPlot Development Version from GitHub Source: https://lavaanplot.alexlishinski.com/ Install the latest development version of lavaanPlot from GitHub using the devtools package. ```r install.packages("devtools") devtools::install_github("alishinski/lavaanPlot") ``` -------------------------------- ### Install lavaanPlot from CRAN Source: https://lavaanplot.alexlishinski.com/ Use this command to install the stable version of the lavaanPlot package from CRAN. ```r install.packages("lavaanPlot") ``` -------------------------------- ### lavaanPlot for CFA Models Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Visualizes confirmatory factor analysis (CFA) models. This example shows how to plot latent variable loadings. ```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) ``` -------------------------------- ### Basic lavaanPlot2 Graph Without Conditional Formatting Source: https://lavaanplot.alexlishinski.com/articles/conditional_formatting This example shows the default plotting behavior of `lavaanPlot2` without any specific conditional formatting applied. It includes basic labels and graph options. ```R labels2 = c(visual = "Visual Ability", textual = "Textual Ability", speed = "Speed Ability") lavaanPlot2(fit2, include = "covs", labels = labels2, graph_options = list(label = "my first graph with signficance stars"), node_options = list( fontname = "Helvetica"), edge_options = list(color = "grey"), stars = c("latent"), coef_labels = TRUE) ``` -------------------------------- ### Conditional Edge Formatting with `formatting` Source: https://lavaanplot.alexlishinski.com/articles/conditional_formatting This example applies conditional formatting to edges. Regression edges are orange, latent variable edges are red with a thicker penwidth, and covariance edges are blue. Node formatting from the previous example is also included. ```R e_opts <- formatting(list(color = "orange"),list(color = "red", penwidth = 6), list(color = "blue"), type = "edge") lavaanPlot2(fit2, include = "covs", labels = labels2, graph_options = list(label = "my first graph with signficance stars"), node_options = n_opts, edge_options = e_opts, stars = c("latent"), coef_labels = TRUE) ``` -------------------------------- ### Fit a Structural Equation Model with lavaan Source: https://lavaanplot.alexlishinski.com/ Define and fit a structural equation model using the sem function from the lavaan package. This example uses the mtcars dataset. ```r model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) summary(fit) ``` -------------------------------- ### Generate lavaan Plot with Custom Options Source: https://lavaanplot.alexlishinski.com/ Generate a plot of the lavaan model fit object using lavaanPlot. This example customizes node shape, font, edge color, and suppresses coefficient display. ```r lavaanPlot(model = fit, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = F) ``` -------------------------------- ### buildPaths() Source: https://lavaanplot.alexlishinski.com/reference Extracts the paths from the lavaan model. ```APIDOC ## buildPaths() ### Description Extracts the paths from the lavaan model. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### create_grviz() Source: https://lavaanplot.alexlishinski.com/reference Creates the grViz dot language code for plotting. ```APIDOC ## create_grviz() ### Description Creates the grViz dot language code for plotting. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### Load lavaan Package Source: https://lavaanplot.alexlishinski.com/ Load the lavaan package, which is required for fitting structural equation models. ```r library(lavaan) ``` -------------------------------- ### formatting() Source: https://lavaanplot.alexlishinski.com/reference Enables conditional formatting for different parts of the model. ```APIDOC ## formatting() ### Description Enables conditional formatting for different parts of the model. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### Load lavaanPlot Package Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Load the lavaanPlot package for visualizing lavaan model results. ```r library(lavaanPlot) ``` -------------------------------- ### create_grviz Source: https://lavaanplot.alexlishinski.com/reference/create_grviz Creates the grViz dot language code for plotting. ```APIDOC ## create_grviz ### Description Creates the grViz dot language code for plotting. ### Usage ```R create_grviz( model, labels = labels, include = include, graph_options = graph_options, node_options = node_options, edge_options = edge_options, stand = stand, ... ) ``` ### Arguments * **model** (lavaan) - A model fit object of class lavaan. * **labels** (list) - An optional named list of variable labels. * **include** (character or list) - Which parameters to include in the plot. Default is all regression and latent relationships. "covs" will also include covariances, while "all will also include error variances. * **graph_options** (list) - A named list of graphviz graph attributes. * **node_options** (list) - A named list of graphviz node attributes. * **edge_options** (list) - A named list of graphviz edge attributes. * **stand** (logical) - Should the coefficients being used be standardized coefficients. * **...** - Additional arguments to be passed to `create_edges`. ### Value A string specifying the path diagram for `model`. ``` -------------------------------- ### buildPaths Function Source: https://lavaanplot.alexlishinski.com/reference/buildpaths Extracts the paths from the lavaan model. ```APIDOC ## buildPaths ### Description Extracts the paths from the lavaan model. ### Usage ```R buildPaths( fit, coefs = FALSE, sig = 1, stand = FALSE, covs = FALSE, stars = NULL, digits = 2 ) ``` ### Arguments * **fit** (lavaan) - 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 ``` -------------------------------- ### lavaanPlot for CFA with Standardized Paths Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Displays all paths in a CFA model as standardized coefficients. Use the `stand` argument. ```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) ``` -------------------------------- ### lavaanPlot() Source: https://lavaanplot.alexlishinski.com/reference Plots lavaan path model with DiagrammeR. ```APIDOC ## lavaanPlot() ### Description Plots lavaan path model with DiagrammeR. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### Include All Model Paths in lavaanPlot Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Set the `include` argument to 'all' to display regression, latent variable relationships, model covariances, and error variances. This provides a comprehensive view of the model. ```R lavaanPlot2(fit, include = "all", labels = labels, graph_options = list(label = "including error variances"), node_options = list( fontname = "Helvetica"), edge_options = list(color = "grey")) ``` -------------------------------- ### lavaanPlot2() Source: https://lavaanplot.alexlishinski.com/reference Plots lavaan path model with DiagrammeR. ```APIDOC ## lavaanPlot2() ### Description Plots lavaan path model with DiagrammeR. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### buildCall() Source: https://lavaanplot.alexlishinski.com/reference Builds the Diagrammer function call. ```APIDOC ## buildCall() ### Description Builds the Diagrammer function call. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### Basic lavaanPlot visualization Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Generates a basic plot of a fitted lavaan model using lavaanPlot, with specified node and edge options. Coefficients are not displayed. ```r lavaanPlot(model = fit, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = FALSE) ``` -------------------------------- ### Plot lavaan path model with DiagrammeR Source: https://lavaanplot.alexlishinski.com/reference/lavaanplot Plots a lavaan path model using DiagrammeR. Requires a lavaan model fit object. Additional arguments can be passed to control node and edge appearance, and to include 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) ``` -------------------------------- ### Basic lavaanPlot Usage Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Generates a basic plot of the lavaan model results. Use this to visualize all paths by default. ```R lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE) ``` -------------------------------- ### buildCall Source: https://lavaanplot.alexlishinski.com/reference/buildcall Builds the Diagrammer function call for a lavaan model. ```APIDOC ## buildCall ### Description Builds the Diagrammer function call. ### Usage ```R buildCall( model = model, name = name, labels = labels, graph_options = list(overlap = "true", fontsize = "10"), node_options = list(shape = "box"), edge_options = list(color = "black"), ... ) ``` ### Arguments * **model** (lavaan fit object) - A model fit object of class lavaan. * **name** (string) - 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) - A named list of graph options for Diagrammer syntax. * **node_options** (named list) - A named list of node options for Diagrammer syntax. * **edge_options** (named list) - A named list of edge options for Diagrammer syntax. * **...** - additional arguments to be passed to `buildPaths` ### Value A string specifying the path diagram for `model` ``` -------------------------------- ### Build Paths Function Signature Source: https://lavaanplot.alexlishinski.com/reference/buildpaths Defines the signature for the buildPaths function, outlining its arguments and their purposes. Use this function to extract paths from a lavaan model. ```R buildPaths( fit, coefs = FALSE, sig = 1, stand = FALSE, covs = FALSE, stars = NULL, digits = 2 ) ``` -------------------------------- ### create_nodes Source: https://lavaanplot.alexlishinski.com/reference/create_nodes Creates node data frame and adds formatting. ```APIDOC ## create_nodes ### Description Creates node data frame and adds formatting. ### Usage ```R create_nodes(coefs, labels = NULL, node_options) ``` ### Arguments * **coefs** (table) - a coefficient table from lavaan model created by `extract_coefs` * **labels** (list) - An optional list of labels * **node_options** (list or data.frame) - a named list of graphviz node attributes, or a data frame of node options created by `formatting` ### Value an edge data frame ``` -------------------------------- ### buildLabels Function Source: https://lavaanplot.alexlishinski.com/reference/buildlabels This function takes a named list of variable labels and prepares them for use in Diagrammer plots. ```APIDOC ## buildLabels ### Description Adds variable labels to the Diagrammer plot function call. ### Usage ```R buildLabels(label_list) ``` ### Arguments * **label_list** (named list) - A named list of variable labels. ``` -------------------------------- ### Change Layout Engine with layout Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Use the `layout` graph option to specify the layout engine for generating the plot. Supported engines include dot, neato, circo, and twopi. Refer to Graphviz documentation for more options. ```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) ``` ```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) ``` ```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) ``` -------------------------------- ### Save and Embed lavaan Plots Source: https://lavaanplot.alexlishinski.com/ Demonstrates how to generate a lavaan plot and then save it as a PDF or PNG file. The embed_plot_pdf and save_png functions are used for this purpose. ```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") ``` -------------------------------- ### create_edges() Source: https://lavaanplot.alexlishinski.com/reference Creates edge data frame and adds formatting. ```APIDOC ## create_edges() ### Description Creates edge data frame and adds formatting. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### Build Diagrammer Function Call Source: https://lavaanplot.alexlishinski.com/reference/buildcall Use this function to construct the Diagrammer call for visualizing lavaan model fits. Customize graph, node, and edge options as needed. ```R buildCall( model = model, name = name, labels = labels, graph_options = list(overlap = "true", fontsize = "10"), node_options = list(shape = "box"), edge_options = list(color = "black"), ... ) ``` -------------------------------- ### getNodes() Source: https://lavaanplot.alexlishinski.com/reference Extracts the paths from the lavaan model. ```APIDOC ## getNodes() ### Description Extracts the paths from the lavaan model. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### Basic lavaanPlot Model Plot Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Generate a basic plot of a lavaan model fit object without any additional options. This is the simplest way to visualize your model. ```R lavaanPlot2(fit) ``` -------------------------------- ### lavaanPlot with Standardized Paths Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Displays all paths in the plot as standardized coefficients. Use the `stand` argument to enable this. ```R # All paths unstandardized lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, stand = TRUE) ``` -------------------------------- ### sig_stars() Source: https://lavaanplot.alexlishinski.com/reference Generates standard significance stars. ```APIDOC ## sig_stars() ### Description Generates standard significance stars. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### Create Nodes Function Signature Source: https://lavaanplot.alexlishinski.com/reference/create_nodes Defines the usage of the create_nodes function, including its arguments: coefs (coefficient table), labels (optional list of labels), and node_options (graphviz node attributes or a data frame from formatting). ```R create_nodes(coefs, labels = NULL, node_options) ``` -------------------------------- ### Apply Custom Edge Formatting Source: https://lavaanplot.alexlishinski.com/articles/conditional_formatting Apply custom formatting to specific edges using the `formatting` function with custom groups. This allows for unique styling of edges identified by custom parameter labels. ```R c_opts <- formatting(list(color = "yellow", penwidth = 8), list(color = "blue", penwidth = 10), type = "custom", groups = c("A", "B")) lavaanPlot2(fit2, include = "covs", labels = labels2, graph_options = list(label = "my first graph with signficance stars"), node_options = n_opts, edge_options = list(e_opts, c_opts), stars = c("latent"), coef_labels = TRUE) ``` -------------------------------- ### lavaanPlot with custom labels Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Visualizes a fitted lavaan model using lavaanPlot with custom labels for variables, node, and edge options. Coefficients are not displayed. ```r 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 = FALSE) ``` -------------------------------- ### getNodes Function Source: https://lavaanplot.alexlishinski.com/reference/getnodes Extracts the paths from the lavaan model. ```APIDOC ## getNodes ### Description Extracts the paths from the lavaan model. ### Usage ```R getNodes(fit) ``` ### Arguments * **fit** (lavaan) - A model fit object of class lavaan. ``` -------------------------------- ### Create Edges Function Signature Source: https://lavaanplot.alexlishinski.com/reference/create_edges Defines the signature for the create_edges function, outlining its arguments and their purposes. Use this function to generate edge data frames for plotting lavaan models. ```R create_edges( coefs, ndf, edge_options, coef_labels = FALSE, stand = FALSE, stars = NULL, sig = 1 ) ``` -------------------------------- ### buildLabels() Source: https://lavaanplot.alexlishinski.com/reference Adds variable labels to the Diagrammer plot function call. ```APIDOC ## buildLabels() ### Description Adds variable labels to the Diagrammer plot function call. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### Add Significance Stars for Covariance Paths Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Include significance stars for covariance paths by setting `stars = c("covs")` and `coef_labels = TRUE`. This helps in quickly identifying significant covariances. ```R lavaanPlot2(fit2, include = "covs", labels = labels2, graph_options = list(label = "my first graph, which is being used to illustrate how to use the new code in the lavaanPlot package"), node_options = list( fontname = "Helvetica"), edge_options = list(color = "grey"), stars = c("covs"), coef_labels = TRUE) ``` -------------------------------- ### formatting Source: https://lavaanplot.alexlishinski.com/reference/formatting Enables conditional formatting for different parts of the model. This function generates a formatting data frame that can be used with create_nodes and create_edges functions. ```APIDOC ## formatting ### Description Enables conditional formatting for different parts of the model. ### Usage ```R formatting(..., type, groups) ``` ### Arguments * `...`: Lists of node or edge options for each of the groups. * `type` (string): Type of conditional formatting being used, node, edge, or custom. 'custom' only works with edges. * `groups` (character vector): The names of custom groups. For nodes and edges, default values are set and you need to match the order. For nodes: c("latent", "obs"). For edges: c("regress", "latent", "covs"). For custom groups of edges, you must match names that you pre-multiply with coefficients in your model specification. ### Value A formatting data frame that can work with the create_nodes and create_edges functions. ``` -------------------------------- ### create_edges Source: https://lavaanplot.alexlishinski.com/reference/create_edges Creates an edge data frame and adds formatting to graph elements based on model coefficients and node data. ```APIDOC ## Function: create_edges ### Description Creates an edge data frame and adds formatting to graph elements based on model coefficients and node data. ### Usage ```R create_edges(coefs, ndf, edge_options, coef_labels = FALSE, stand = FALSE, stars = NULL, sig = 1) ``` ### Arguments * **coefs** (data.frame) - A coefficient table from a lavaan model, typically created by `extract_coefs`. * **ndf** (data.frame) - A node data frame, typically created by `create_nodes`. * **edge_options** (list or data.frame) - A named list of graphviz edge attributes, or a data frame of edge options created by `formatting`, or a list containing both edge options and custom options. * **coef_labels** (boolean) - Whether to label edges with coefficient values. Defaults to FALSE. * **stand** (boolean) - Should the coefficients being used be standardized coefficients. Defaults to FALSE. * **stars** (character vector) - A character vector indicating which parameters should include significance stars (e.g., "regress", "latent", "covs"). Defaults to NULL (no stars). * **sig** (numeric) - The significance level for determining which paths are considered significant. Defaults to 1. ### Value * **edge data frame** - A data frame representing the edges of the graph with associated formatting. ``` -------------------------------- ### Conditional Node Formatting with `formatting` Source: https://lavaanplot.alexlishinski.com/articles/conditional_formatting This snippet demonstrates how to use the `formatting` function to apply distinct styles to latent and observed variable nodes. Latent variables are styled as orange hexagons, and observed variables as blue octagons. ```R n_opts <- formatting(list(shape = "polygon", sides = "6", color = "orange"), list(shape = "polygon", sides = "8",color = "blue"), type = "node") lavaanPlot2(fit2, include = "covs", labels = labels2, graph_options = list(label = "my first graph with signficance stars"), node_options = n_opts, edge_options = list(color = "grey"), stars = c("latent"), coef_labels = TRUE) ``` -------------------------------- ### Customize Graph, Node, and Edge Options in lavaanPlot Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Control the appearance of the plot by providing named lists for `graph_options`, `node_options`, and `edge_options`. This allows for detailed customization of labels, layout, font, and colors. ```R lavaanPlot2(fit, labels = labels, graph_options = list(label = "my first graph", rankdir = "LR"), node_options = list( fontname = "Helvetica"), edge_options = list(color = "grey")) ``` -------------------------------- ### R: Create grViz Dot Language Code Source: https://lavaanplot.alexlishinski.com/reference/create_grviz Use this function to generate the DOT language code for plotting lavaan models. It accepts various arguments to customize the plot's appearance and content, including which parameters to include and specific styling for graphs, nodes, and edges. ```r create_grviz( model, labels = labels, include = include, graph_options = graph_options, node_options = node_options, edge_options = edge_options, stand = stand, ... ) ``` -------------------------------- ### Fit a lavaan CFA model Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Defines and fits a confirmatory factor analysis model using the cfa function from the lavaan package with the HolzingerSwineford1939 dataset. ```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")) ``` -------------------------------- ### lavaanPlot Function Source: https://lavaanplot.alexlishinski.com/reference/lavaanplot Plots a lavaan path model with DiagrammeR. It takes a lavaan model object and generates a path diagram. ```APIDOC ## lavaanPlot Function ### Description Plots a lavaan path model with DiagrammeR. It takes a lavaan model object and generates a path diagram. ### Usage ```R lavaanPlot(model, name = "plot", labels = NULL, ...) ``` ### Arguments * **model** (lavaan object) - A model fit object of class lavaan. * **name** (string) - 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) ``` ``` -------------------------------- ### extract_coefs Source: https://lavaanplot.alexlishinski.com/reference/extract_coefs Creates a data frame of the parameter table from lavaan model. ```APIDOC ## Function: extract_coefs ### Description Creates a data frame of the parameter table from a lavaan model. ### Usage ```R extract_coefs(model, include = NULL, stand = FALSE) ``` ### Arguments * `model` (lavaan): A fitted model of class lavaan. * `include` (character, optional): Which parameters to include in the plot. Default is all regression and latent relationships. "covs" will also include covariances, while "all" will also include error variances. * `stand` (logical): Should the coefficients being used be standardized coefficients. Default is FALSE. ### Value A data frame with lavaan model parameters. ``` -------------------------------- ### Extract Paths from lavaan Model Source: https://lavaanplot.alexlishinski.com/reference/getnodes Use getNodes to extract paths from a lavaan model fit object. Ensure the input is a model fit object of class lavaan. ```R getNodes(fit) ``` -------------------------------- ### Save and Embed lavaanPlot Source: https://lavaanplot.alexlishinski.com/articles/save_and_embed Use `embed_plot_pdf` to save a plot as a PDF and embed it in Rmarkdown, and `save_png` to save it as a PNG file. Ensure the lavaan model is fitted and plotted before calling these functions. ```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) ``` -------------------------------- ### save_png() Source: https://lavaanplot.alexlishinski.com/reference Saves a plot as a png. ```APIDOC ## save_png() ### Description Saves a plot as a png. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` -------------------------------- ### Extract Coefficients from lavaan Model Source: https://lavaanplot.alexlishinski.com/reference/extract_coefs Use this function to create a data frame of the parameter table from a fitted lavaan model. You can specify which parameters to include (e.g., 'covs', 'all') and whether to use standardized coefficients. ```r extract_coefs(model, include = NULL, stand = FALSE) ``` -------------------------------- ### Add Significance Stars for Latent Paths Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Show significance stars for latent variable paths by using `stars = c("latent")` in conjunction with `coef_labels = TRUE`. ```R lavaanPlot2(fit2, include = "covs", labels = labels2, graph_options = list(label = "my first graph with signficance stars"), node_options = list( fontname = "Helvetica"), edge_options = list(color = "grey"), stars = c("latent"), coef_labels = TRUE) ``` -------------------------------- ### Define and Fit CFA Model Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Define a confirmatory factor analysis model with latent variables and fit it to the HolzingerSwineford1939 dataset. ```r HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit2 <- cfa(HS.model, data=HolzingerSwineford1939) summary(fit2) ``` -------------------------------- ### Convert NDF and EDF to DOT Specification Source: https://lavaanplot.alexlishinski.com/reference/convert_graph Use this function to convert node and edge data frames into a DOT specification for graph visualization. It requires node data frame (ndf), edge data frame (edf), and a list of graphviz graph attributes (graph_options). ```R convert_graph(ndf, edf, graph_options) ``` -------------------------------- ### convert_graph Source: https://lavaanplot.alexlishinski.com/reference/convert_graph Uses the diagrammeR functions to turn the ndf and edf into dot. This function generates a DOT specification of the model from node and edge data frames. ```APIDOC ## convert_graph ### Description Uses the diagrammeR functions to turn the ndf and edf into dot. ### Usage ```R convert_graph(ndf, edf, graph_options) ``` ### Arguments * **ndf** (data.frame) - A node data frame created by `create_nodes`. * **edf** (data.frame) - An edge data frame created by `create_edges`. * **graph_options** (list) - A named list of graphviz graph attributes. ### Value DOT specification of model ``` -------------------------------- ### Build Labels Function in lavaanPlot Source: https://lavaanplot.alexlishinski.com/reference/buildlabels This function adds variable labels to the Diagrammer plot function call. It requires a named list of variable labels as input. ```R buildLabels(label_list) ``` -------------------------------- ### Add Significance Stars for Regression Paths Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Display significance stars next to coefficient labels for regression paths by setting `stars = c("regress")` along with `coef_labels = TRUE`. ```R lavaanPlot2(fit, include = "covs", labels = labels, graph_options = list(label = "my first graph with significance stars"), node_options = list( fontname = "Helvetica"), edge_options = list(color = "grey"), stars = c("regress"), coef_labels = TRUE) ``` -------------------------------- ### Save lavaanPlot as PNG Source: https://lavaanplot.alexlishinski.com/reference/save_png Use this function to save a lavaanPlot object to a PNG file. Specify the plot object and the desired filename. Optional width and height arguments can control the image dimensions. ```R library(lavaan) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) pl <- lavaanPlot(model = fit) if (FALSE) { # \dontrun{ save_png(pl, "plot.png") } ``` -------------------------------- ### Define Custom Model Specification Source: https://lavaanplot.alexlishinski.com/articles/conditional_formatting Define a custom model specification for lavaan. This involves specifying latent variables and their indicators, including custom parameter labels. ```R HS.model <- ' visual =~ A*x1 + x2 + x3 textual =~ x4 + x5 + B*x6 speed =~ x7 + x8 + x9 ' fit2 <- cfa(HS.model, data=HolzingerSwineford1939) ``` -------------------------------- ### Add Coefficient Labels to Edges in lavaanPlot Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Enable coefficient labels on the edges by setting `coef_labels = TRUE`. This is useful for displaying the estimated coefficients directly on the paths. ```R lavaanPlot2(fit, include = "covs", coef_labels = TRUE, labels = labels, graph_options = list(label = "including coefficient labels"), node_options = list(fontname = "Helvetica"), edge_options = list(color = "grey")) ``` -------------------------------- ### lavaanPlot for CFA with Significant Paths Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Plots only the significant paths in a CFA model. Use `sig` to specify the significance level. ```R # Significant paths lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, sig = .05) ``` -------------------------------- ### lavaanPlot2 Function Source: https://lavaanplot.alexlishinski.com/reference/lavaanplot2 Plots a lavaan path model using DiagrammeR. It allows customization of the plot through various options. ```APIDOC ## lavaanPlot2 ### Description Plots lavaan path model with DiagrammeR. ### Usage ```R lavaanPlot2( model, labels = NULL, include = NULL, gr_viz = NULL, graph_options = NULL, node_options = NULL, edge_options = NULL, stand = FALSE, ... ) ``` ### Arguments * **model** (lavaan fit object) - A model fit object of class lavaan. * **labels** (named list, optional) - An optional named list of variable labels. * **include** (character, optional) - which parameters to include in the plot. Default is all regression and latent relationships. "covs" will also include covariances, while "all will also include error variances. * **gr_viz** (grViz object, optional) - pass a gr_viz model generated from `create_grviz` to create plot from that directly. * **graph_options** (named list, optional) - a named list of graphviz graph attributes. * **node_options** (named list, optional) - a named list of graphviz node attributes. * **edge_options** (named list, optional) - a named list of graphviz edge attributes. * **stand** (logical) - Should the coefficients being used be standardized coefficients. Default is FALSE. * **...** - Additional arguments to be passed to create_grviz for creating edges. ### Value A Diagrammer plot of the path diagram for `model`. ``` -------------------------------- ### lavaanPlot with Model Covariances Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Includes double-sided edges to represent model covariances. Use the `covs` argument to enable this. ```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) ``` -------------------------------- ### Define Latent Variable Model and Fit Source: https://lavaanplot.alexlishinski.com/articles/conditional_formatting This code defines a latent variable model and fits it to the Holzinger-Swineford 1939 dataset using `cfa`. It serves as a base for demonstrating formatting options. ```R HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit2 <- cfa(HS.model, data=HolzingerSwineford1939) summary(fit2) ``` -------------------------------- ### R: Define Conditional Formatting Source: https://lavaanplot.alexlishinski.com/reference/formatting Use the `formatting` function to create a data frame for conditional formatting of model elements. Specify the type of formatting (node, edge, or custom) and define custom groups if needed. ```r formatting(..., type, groups) ``` -------------------------------- ### Include Model Covariances in lavaanPlot Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Use the `include` argument set to 'covs' to display model covariances in the plot. This helps visualize relationships between latent or observed variables. ```R lavaanPlot2(fit, include = "covs", labels = labels, graph_options = list(label = "Including covariates"), node_options = list( fontname = "Helvetica"), edge_options = list(color = "grey")) ``` -------------------------------- ### Define Custom Labels for lavaanPlot Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Use a named character vector to define custom labels for model variables. This replaces the previous list-based approach. ```R labels2 = c(visual = "Visual Ability", textual = "Textual Ability", speed = "Speed Ability") ``` -------------------------------- ### lavaanPlot2 Function Signature Source: https://lavaanplot.alexlishinski.com/reference/lavaanplot2 This is the function signature for lavaanPlot2. It outlines the available arguments for plotting lavaan path models. ```R lavaanPlot2( model, labels = NULL, include = NULL, gr_viz = NULL, graph_options = NULL, node_options = NULL, edge_options = NULL, stand = FALSE, ... ) ``` -------------------------------- ### lavaanPlot with Latent Path Significance Stars Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Adds significance stars to latent variable paths. Use `stars = "latent"` to specify 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") ``` -------------------------------- ### Embed Plot into R Markdown PDF Source: https://lavaanplot.alexlishinski.com/reference/embed_plot_pdf Use this function to save a lavaanPlot object as a PDF and embed it into an R Markdown document. Specify the plot object and the desired filename. Optional width and height arguments can control the image dimensions. ```R embed_plot_pdf(pl, "plot2.pdf") ``` -------------------------------- ### lavaanPlot with Covariance Significance Stars Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Adds significance stars to covariance paths. Use `stars = "covs"` to specify this. ```R lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = "covs") ``` -------------------------------- ### Change Plot Orientation with rankdir Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Use the `rankdir` graph option to change the overall orientation of the plot. Options include TB (top-bottom), BT (bottom-top), RL (right-left), and LR (left-right). ```R lavaanPlot(model = fit, labels = labels, graph_options = list(rankdir = "LR"), node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = TRUE, digits = 1) ``` ```R lavaanPlot(model = fit, labels = labels, graph_options = list(rankdir = "RL"), node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, covs = TRUE, stars = TRUE, digits = 1) ``` ```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) ``` -------------------------------- ### sig_stars Function Source: https://lavaanplot.alexlishinski.com/reference/sig_stars Generates standard significance stars from a vector of p-values. ```APIDOC ## sig_stars ### Description Generates standard significance stars from a vector of p-values. ### Usage ```R sig_stars(pvals) ``` ### Arguments * `pvals` (numeric vector) - A vector of p values. ``` -------------------------------- ### save_png Function Source: https://lavaanplot.alexlishinski.com/reference/save_png Saves a lavaanPlot plot object to a specified file path as a PNG image. Allows for custom width and height. ```APIDOC ## save_png ### Description Saves a plot as a png. ### Usage ```R save_png(plot, path, width = NULL, height = NULL) ``` ### Arguments * **plot** (plot object) - Required - plot object created by `lavaanPlot` * **path** (character) - 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 ### Value no return value saves plot as png ### Examples ```R library(lavaan) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) pl <- lavaanPlot(model = fit) if (FALSE) { # \dontrun{ save_png(pl, "plot.png") } ``` ``` -------------------------------- ### Generate Significance Stars Source: https://lavaanplot.alexlishinski.com/reference/sig_stars Use this function to convert a vector of p-values into significance stars. Ensure the input is a numeric vector. ```R sig_stars(pvals) ``` -------------------------------- ### lavaanPlot with Custom Decimal Places Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Controls the number of decimal places displayed for coefficient values in the plot labels. Use the `digits` argument. ```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) ``` -------------------------------- ### embed_plot_pdf Source: https://lavaanplot.alexlishinski.com/reference/embed_plot_pdf Embeds a plot into an rmarkdown pdf. This function takes a lavaanPlot object and saves it as a PDF, embedding it into the R Markdown document using `include_graphics`. ```APIDOC ## embed_plot_pdf ### Description Embeds a plot into an rmarkdown pdf. ### Usage ```R embed_plot_pdf(plot, path, width = NULL, height = NULL) ``` ### 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 ### Value No return value. Calls `include_graphics` to embed plot in pdf. ### Examples ```R library(lavaan) model <- 'mpg ~ cyl + disp + hp qsec ~ disp + hp + wt' fit <- sem(model, data = mtcars) pl <- lavaanPlot(model = fit) # To run this example, uncomment the following lines: # if (FALSE) { # embed_plot_pdf(pl, "plot2.pdf") # } ``` ``` -------------------------------- ### Add Custom Labels to lavaanPlot Source: https://lavaanplot.alexlishinski.com/articles/improvements_to_lavaanplot Customize the labels of variables in the plot using a named character vector passed to the `labels` argument. ```R labels <- c(mpg = "Miles Per Gallon", cyl = "Cylinders", disp = "Displacement", hp = "Horsepower", qsec = "Speed", wt = "Weight") lavaanPlot2(fit, labels = labels) ``` -------------------------------- ### lavaanPlot with Significant Paths Source: https://lavaanplot.alexlishinski.com/articles/intro_to_lavaanplot Filters the plot to show only significant paths based on a specified significance level. Use `sig` to control the threshold. ```R # significant standardized paths only lavaanPlot(model = fit, labels = labels, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE, sig = .05) ``` -------------------------------- ### embed_plot_pdf() Source: https://lavaanplot.alexlishinski.com/reference Embeds a plot into an rmarkdown pdf. ```APIDOC ## embed_plot_pdf() ### Description Embeds a plot into an rmarkdown pdf. ### Parameters (No parameters explicitly documented in the source) ### Returns (No return value explicitly documented in the source) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.