| Title: | Customizing Structural Equation Modelling Plots |
|---|---|
| Description: | Most function focus on specific ways to customize a graph. They use a 'qgraph' output as the first argument, and return a modified 'qgraph' object. This allows the functions to be chained by a pipe operator. |
| Authors: | Shu Fai Cheung [aut, cre] (ORCID: <https://orcid.org/0000-0002-9871-9448>), Mark Hok Chio Lai [aut] (ORCID: <https://orcid.org/0000-0002-9196-7406>) |
| Maintainer: | Shu Fai Cheung <[email protected]> |
| License: | GPL-3 |
| Version: | 0.3.3.19 |
| Built: | 2026-06-28 15:23:08 UTC |
| Source: | https://github.com/sfcheung/semptools |
Add a fit object (e.g., 'lavaan' output) to the a 'qgraph' object as an attribute.
add_object(semPaths_plot, object)add_object(semPaths_plot, object)
semPaths_plot |
A
qgraph::qgraph object generated by
|
object |
Should be the object,
such as the output of |
It adds an object to
a qgraph::qgraph object as the
attribute "semptools_fit_object",
to be retrieved by other functions
that need to access the original
output used in semPlot::semPaths()
to create a diagram.
The original qgraph::qgraph object
set to semPaths_plot, with the
attribute "semptools_fit_object"
set to object.
library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 + x06 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 + x03 f4 =~ x11 + x12 + x13 + x14 ' fit <- lavaan::cfa(mod, cfa_example) p <- semPaths(fit, whatLabels = "est", sizeMan = 3.25, node.width = 1, edge.label.cex = .75, mar = c(10, 5, 10, 5), DoNotPlot = TRUE) p <- add_object(p, fit) attr(p, "semptools_fit_object")library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 + x06 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 + x03 f4 =~ x11 + x12 + x13 + x14 ' fit <- lavaan::cfa(mod, cfa_example) p <- semPaths(fit, whatLabels = "est", sizeMan = 3.25, node.width = 1, edge.label.cex = .75, mar = c(10, 5, 10, 5), DoNotPlot = TRUE) p <- add_object(p, fit) attr(p, "semptools_fit_object")
Replace the residual variances of exogenous variables by their R-squares in a qgraph::qgraph object.
add_rsq( semPaths_plot, object = NULL, digits = 2L, rsq_string = "R2=", ests = NULL )add_rsq( semPaths_plot, object = NULL, digits = 2L, rsq_string = "R2=", ests = NULL )
semPaths_plot |
A qgraph object generated by
|
object |
The object used by semPaths to generate the plot. Use
the same argument name used in |
digits |
Integer indicating number of decimal places for the R-squares. Default is 2L. |
rsq_string |
The string before the
R-squares. Default is |
ests |
A data.frame from the
|
Modify a qgraph::qgraph object generated by
semPaths by setting the labels
of the residuals of endogenous variables to their
R-squares.
Require either the original object used in the semPaths call, or a data frame with the R-square for each endogenous variable.
Currently supports only plots based on lavaan
output.
If the input is a qgraph::qgraph object, the function returns a qgraph based on the original one, with R-squares added. If the input is a list of qgraph objects, the function returns a list of the same length.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2 <- add_rsq(p_pa, fit_pa) plot(p_pa2) mod_cfa <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::sem(mod_cfa, cfa_example) lavaan::parameterEstimates(fit_cfa)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] p_cfa <- semPlot::semPaths(fit_cfa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0) # Place standard errors on a new line p_cfa2 <- add_rsq(p_cfa, fit_cfa) plot(p_cfa2) mod_sem <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' # Can be used with mark_se() and mark_sig() fit_sem <- lavaan::sem(mod_sem, sem_example) lavaan::parameterEstimates(fit_sem)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] p_sem <- semPlot::semPaths(fit_sem, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0) # Mark significance, and then add standard errors p_sem2 <- mark_sig(p_sem, fit_sem) p_sem3 <- mark_se(p_sem2, fit_sem, sep = "\n") p_sem4 <- add_rsq(p_sem3, fit_sem) plot(p_sem4)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2 <- add_rsq(p_pa, fit_pa) plot(p_pa2) mod_cfa <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::sem(mod_cfa, cfa_example) lavaan::parameterEstimates(fit_cfa)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] p_cfa <- semPlot::semPaths(fit_cfa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0) # Place standard errors on a new line p_cfa2 <- add_rsq(p_cfa, fit_cfa) plot(p_cfa2) mod_sem <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' # Can be used with mark_se() and mark_sig() fit_sem <- lavaan::sem(mod_sem, sem_example) lavaan::parameterEstimates(fit_sem)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] p_sem <- semPlot::semPaths(fit_sem, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0) # Mark significance, and then add standard errors p_sem2 <- mark_sig(p_sem, fit_sem) p_sem3 <- mark_se(p_sem2, fit_sem, sep = "\n") p_sem4 <- add_rsq(p_sem3, fit_sem) plot(p_sem4)
Use a named vector or named arguments to create a matrix of the directions of indicators of factors.
auto_factor_point_to(factor_layout, ...)auto_factor_point_to(factor_layout, ...)
factor_layout |
A layout matrix
to be used in the |
... |
Additional arguments. If
the first argument is not named, then
it should be a named vector of
directions, names being the names of
the factors, and directions can be
one of these values: |
A helper function to make
it easier to create the matrix
used by set_sem_layout() to
indicate where the indicators of
each factor should be positioned.
It works in two modes. If the
first argument is a named vector,
such as c(f1 = "up", f2 = "down"),
then this vector will be used to
create the direction matrix.
If the arguments are named, such as
auto_factor_point_to(factor_layout, f1 = "up", f2 = "down",
then the names are treated as the
factor names, and the values of
the arguments are treated as the
directions.
The matrix created can then be used
for the argument factor_point_to
in set_sem_layout().
A character matrix of the same
dimension as factor_layout. The
cells of factor names are replaced
by the directions to place their
indicators.
factor_layout <- matrix(c("f1", NA, NA, NA, "f3", "f4", "f2", NA, NA), byrow = TRUE, 3, 3) factor_point_to <- auto_factor_point_to(factor_layout, f1 = "left", f2 = "left", f3 = "down", f4 = "down") factor_point_tofactor_layout <- matrix(c("f1", NA, NA, NA, "f3", "f4", "f2", NA, NA), byrow = TRUE, 3, 3) factor_point_to <- auto_factor_point_to(factor_layout, f1 = "left", f2 = "left", f3 = "down", f4 = "down") factor_point_to
Determine the order of indicators and match indicators and factors based on a plot from a 'qgraph' object.
auto_indicator_order(semPaths_plot, add_isolated_manifest = FALSE)auto_indicator_order(semPaths_plot, add_isolated_manifest = FALSE)
semPaths_plot |
A
qgraph::qgraph object generated by
|
add_isolated_manifest |
Logical. Whether observed variables that are not indicators will be included in the output as "factors", each with one indicator (the observed variable). |
It inspects a qgraph::qgraph object and find variables that are the indicators of latent factors.
The output can be used in the
argument indicator_order of
set_cfa_layout() and
set_sem_layout(). It can also be
modified, such as reordered, as
necessary.
If the generated order is used, there
is no need to call this function
manually because set_cfa_layout()
and set_sem_layout() will
automatically call this function, if
indicator_order is not set.
It assumes that observed variables
are represented by squares (shape
set to "square") and latent
variables represented by circles
or ovals (shape set to "circle").
An observed variable is considered as an indicator if there is an arrow pointing to it from a latent variable.
If an indicator loaded on more than one latent variable, it will only be matched to one of them, determined by the order of appearance in the internal storage.
It uses node names, not node labels, in generating the output.
A named character vector. The values are the indicators identified. The names are the latent factors the indicators loaded on.
set_sem_layout() and
set_cfa_layout().
library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 + x06 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 + x03 f4 =~ x11 + x12 + x13 + x14 ' fit <- lavaan::cfa(mod, cfa_example) p <- semPaths(fit, whatLabels = "est", sizeMan = 3.25, node.width = 1, edge.label.cex = .75, mar = c(10, 5, 10, 5), DoNotPlot = TRUE) indicator_order <- auto_indicator_order(p) indicator_order p2 <- set_cfa_layout(p, indicator_order = indicator_order) plot(p2) # set_cfa_layout() will call auto_indicator_order() # automatically if indicator_order is not set. p3 <- set_cfa_layout(p) plot(p3)library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 + x06 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 + x03 f4 =~ x11 + x12 + x13 + x14 ' fit <- lavaan::cfa(mod, cfa_example) p <- semPaths(fit, whatLabels = "est", sizeMan = 3.25, node.width = 1, edge.label.cex = .75, mar = c(10, 5, 10, 5), DoNotPlot = TRUE) indicator_order <- auto_indicator_order(p) indicator_order p2 <- set_cfa_layout(p, indicator_order = indicator_order) plot(p2) # set_cfa_layout() will call auto_indicator_order() # automatically if indicator_order is not set. p3 <- set_cfa_layout(p) plot(p3)
Set the layout of variables in a mediation model in the typical left-to-right style automatically.
auto_layout_mediation( object, x = NULL, y = NULL, exclude = NULL, v_pos = c("middle", "lower", "upper"), v_preference = c("upper", "lower"), output = c("matrix", "xy"), update_plot = TRUE )auto_layout_mediation( object, x = NULL, y = NULL, exclude = NULL, v_pos = c("middle", "lower", "upper"), v_preference = c("upper", "lower"), output = c("matrix", "xy"), update_plot = TRUE )
object |
It can be the output of
|
x |
The variables that will be
treated as (pure) |
y |
The variables that will be
treated as (pure) |
exclude |
The variables to be
omitted from the plot, typically the
covariates ("control variables") in a
model. If |
v_pos |
How the mediators are to
be positioned vertically in the
first pass. If
|
v_preference |
The preference in
shifting the mediators upward
( |
output |
The format of the
output, used if |
update_plot |
Logical. Used
if |
Typically, a path model with some
x variables, some y variables,
and some mediators are drawn from
left to right. This function tries
to generate the layout matrix
automatically, meeting the following
requirements:
The predictor(s), x variables(x),
is/are placed to the left.
The outcome variable(s), y
variable(s), is/are placed to the
right.
The mediator(s) are positioned
between x variable(s) and y
variable(s) such that all paths
point to the right. That is,
no vertical path.
The vertical position(s) of the mediator(s) will be adjusted such that no path passes through a mediator. That is, all paths are visible and not blocked by any mediator.
The function supports a multigroup model and a list of plots. However, the structural parts of all groups or plots must be identical: same variables, and same paths.
If object is a lavaan-class
object, or if update_plot is FALSE,
it returns
a two-dimension layout matrix of the
position of the nodes, or a
two-column matrix of the x-y positions
of the nodes, depending on the
argument output.
If object is a qgraph object
and update_plot is TRUE, it
returns a qgraph object with the
the modified layout.
If object is a list of
qgraph::qgraph objects and
update_plot is TRUE, then it returns
a
list of processed qgraph::qgraph objects.
set_sem_layout(). The
output of auto_layout_mediation()
can be used by set_sem_layout().
library(lavaan) library(semPlot) # Create a dummy dataset mod_pa <- " m11 ~ c1 + x1 m21 ~ c2 + m11 m2 ~ m11 + c3 m22 ~ m11 + c3 y ~ m2 + m21 + m22 + x1 " fit <- lavaan::sem( mod_pa, do.fit = FALSE ) dat <- simulateData( parameterTable(fit), sample.nobs = 500, seed = 1234 ) fit <- lavaan::sem( mod_pa, dat ) # Set the layout m <- auto_layout_mediation( fit, exclude = c("c1", "c2", "c3") ) pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3")) semPaths( pm, whatLabels = "est", layout = m ) # v_pos = "lower" m <- auto_layout_mediation( fit, exclude = c("c1", "c2", "c3"), v_pos = "lower" ) pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3")) p0 <- semPaths( pm, whatLabels = "est", layout = m ) # v_pos = "upper" m <- auto_layout_mediation( fit, exclude = c("c1", "c2", "c3"), v_pos = "upper" ) pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3")) p0 <- semPaths( pm, whatLabels = "est", layout = m ) # Can modify a qgraph pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3")) p <- semPaths( pm, whatLabels = "est" ) p2 <- auto_layout_mediation(p) plot(p2)library(lavaan) library(semPlot) # Create a dummy dataset mod_pa <- " m11 ~ c1 + x1 m21 ~ c2 + m11 m2 ~ m11 + c3 m22 ~ m11 + c3 y ~ m2 + m21 + m22 + x1 " fit <- lavaan::sem( mod_pa, do.fit = FALSE ) dat <- simulateData( parameterTable(fit), sample.nobs = 500, seed = 1234 ) fit <- lavaan::sem( mod_pa, dat ) # Set the layout m <- auto_layout_mediation( fit, exclude = c("c1", "c2", "c3") ) pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3")) semPaths( pm, whatLabels = "est", layout = m ) # v_pos = "lower" m <- auto_layout_mediation( fit, exclude = c("c1", "c2", "c3"), v_pos = "lower" ) pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3")) p0 <- semPaths( pm, whatLabels = "est", layout = m ) # v_pos = "upper" m <- auto_layout_mediation( fit, exclude = c("c1", "c2", "c3"), v_pos = "upper" ) pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3")) p0 <- semPaths( pm, whatLabels = "est", layout = m ) # Can modify a qgraph pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3")) p <- semPaths( pm, whatLabels = "est" ) p2 <- auto_layout_mediation(p) plot(p2)
A sample dataset for fitting a confirmatory factor analysis model.
cfa_examplecfa_example
An object of class data.frame with 200 rows and 14 columns.
Fourteen variables (x01 to x14), 200 cases.
Sample model to fit (in lavaan::model.syntax notation)
mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 '
Change the labels of selected nodes.
change_node_label( semPaths_plot, label_list = NULL, label.cex, label.scale, label.prop, label.norm )change_node_label( semPaths_plot, label_list = NULL, label.cex, label.scale, label.prop, label.norm )
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. |
label_list |
A list of named lists. Each named list should
have two named values: |
label.cex |
Identical to the same argument in
|
label.scale |
Identical to the same argument in
semPlot::semPaths. A logical value that determine whether labels
wil be scaled (resized) to the nodes they attach to. It has no
default. If not set, then this option in the |
label.prop |
Identical to the same argument in
semPlot::semPaths. A numeric vector of length equal to the number
of nodes. If |
label.norm |
Identical to the same argument in
semPlot::semPaths. It must be a string. All labels as wide as or
narrower than this string will have the same font size, while all
labels wider than this string will be rescaled to have the same
width as this string. It has no default. If not set, then this
option in the |
Modify a qgraph::qgraph object generated by semPlot::semPaths and change the labels of selected nodes.
A qgraph::qgraph based on the original one, with node
attributes of selected nodes modified.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
library(semPlot) library(lavaan) mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- sem(mod_pa, pa_example) parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_label_list <- list(list(node = "x3", to = "mediator"), list(node = "x4", to = expression(gamma))) p_pa2 <- change_node_label(p_pa, my_label_list) plot(p_pa2)library(semPlot) library(lavaan) mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- sem(mod_pa, pa_example) parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_label_list <- list(list(node = "x3", to = "mediator"), list(node = "x4", to = expression(gamma))) p_pa2 <- change_node_label(p_pa, my_label_list) plot(p_pa2)
Get the values of an arbitrary attribute of selected edges.
get_edge_attribute( semPaths_plot, edges = NULL, attribute_name = NULL, check_direction = TRUE )get_edge_attribute( semPaths_plot, edges = NULL, attribute_name = NULL, check_direction = TRUE )
semPaths_plot |
A
qgraph::qgraph object generated by
semPlot::semPaths, or a similar
qgraph object modified by other
semptools functions. Unlike some
other functions, this function does
not support a list of qgraph::qgraph objects.
If |
edges |
A character vector of
the edges. Each edge is denoted by
|
attribute_name |
The name of the attribute from which the values will be retrieved. |
check_direction |
If |
From a qgraph::qgraph()
object generated by
semPlot::semPaths(), get the
values of an attribute of selected
edges.
This function is designed to be a general one that gets the values of the attributes named by the user.
values
This argument can be set in three ways.
For a named vector, the name of an
element should be the path as
specified by lavaan::model.syntax
or as appeared in
lavaan::parameterEstimates().
For example, if the attributes to be
changed are the colors of selected
edges, to change the color of the
path regressing y on x, the name
should be "y ~ x". To change the
color of the covariance between x1
and x2, the name should be "x1 ~~ x2". Therefore, c("y ~ x1" = "red", "x1 ~~ x2" = "blue") changes the
colors of the path from x1 to y
and the covariance between x1 and
x2 to "red" and "blue",
respectively.
The order of the two nodes may matter for covariances. Therefore, if the attribute of a covariance is not changed, try switching the order of the two nodes.
For a list of named lists, each named
list should have three named values:
from, to, and new_value. The
attribute of the edge from from to
to will be set to new_value.
The second approach is no longer recommended, though kept for backward compatibility.
The last approach is setting values
to a one-element vector with no name.
All edges in plot will then have the
selected attributes set to this value.
A named vector of the values.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) get_edge_attribute( p_pa, edges = c("x2 ~~ x1", "x4 ~ x1"), attribute_name = "labels") get_edge_attribute( p_pa, edges = c("x2 ~~ x1", "x4 ~ x1"), attribute_name = "color")mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) get_edge_attribute( p_pa, edges = c("x2 ~~ x1", "x4 ~ x1"), attribute_name = "labels") get_edge_attribute( p_pa, edges = c("x2 ~~ x1", "x4 ~ x1"), attribute_name = "color")
Get the values of an arbitrary attributes of selected nodes.
get_node_attribute( semPaths_plot, nodes = NULL, attribute_name = NULL, check_nodes = TRUE )get_node_attribute( semPaths_plot, nodes = NULL, attribute_name = NULL, check_nodes = TRUE )
semPaths_plot |
A
qgraph::qgraph object generated by
semPlot::semPaths, or a similar
qgraph object modified by other
semptools functions. Unlike some
other functions, this function does
not support a list of qgraph::qgraph objects.
If |
nodes |
A character vector of
the names of nodes. If |
attribute_name |
The name of the attribute from which the values will be retrieved. |
check_nodes |
Logical. If |
From a qgraph::qgraph object generated by semPlot::semPaths, and get the values of an attribute of selected nodes.
This function is designed to be a general one that gets the values of the attributes named by the user.
A named vector of the values.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) get_node_attribute( p_pa, nodes = c("x1", "x3"), attribute_name = "color" ) get_node_attribute( p_pa, attribute_name = "label.cex" )mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) get_node_attribute( p_pa, nodes = c("x1", "x3"), attribute_name = "color" ) get_node_attribute( p_pa, attribute_name = "label.cex" )
Check which parameters in a lavaan output are the residual variance of a dependent variable.
is_dv_residvar(lavaan_out)is_dv_residvar(lavaan_out)
lavaan_out |
A lavaan::lavaan object. |
Check which parameters in a lavaan output are the variance of a dependent variable. Indicators of a latent variable will be excluded.
A boolean vector with length equal to the number of rows in the lavaan output.
mod <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod, pa_example) is_dv_residvar(fit_pa) mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::cfa(mod, cfa_example) is_dv_residvar(fit_cfa) mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' fit_sem <- lavaan::sem(mod, sem_example) is_dv_residvar(fit_sem)mod <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod, pa_example) is_dv_residvar(fit_pa) mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::cfa(mod, cfa_example) is_dv_residvar(fit_cfa) mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' fit_sem <- lavaan::sem(mod, sem_example) is_dv_residvar(fit_sem)
Keep or drop nodes from an semPlotModel object.
drop_nodes(object, nodes) keep_nodes(object, nodes)drop_nodes(object, nodes) keep_nodes(object, nodes)
object |
An an |
nodes |
A character vector of the nodes to be kept or removed. |
These functions can be used to edit the nodes in an
semPlot::semPlotModel generated by semPlot::semPlotModel().
The edited object can then be passed to semPlot::semPaths() to
generate a path diagram.
Use keep_nodes() to specify the nodes to be kept. All other nodes
will be removed.
Use drop_nodes() to specify the nodes to be dropped. All other
nodes will be kept.
An object of the class semPlot::semPlotModel.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) pm_pa <- semPlot::semPlotModel(fit_pa) semPlot::semPaths(pm_pa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) pm_pa2 <- drop_nodes(pm_pa, c("x3")) semPlot::semPaths(pm_pa2, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) pm_pa3 <- keep_nodes(pm_pa, c("x1", "x3", "x4")) semPlot::semPaths(pm_pa3, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) pm_pa <- semPlot::semPlotModel(fit_pa) semPlot::semPaths(pm_pa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) pm_pa2 <- drop_nodes(pm_pa, c("x3")) semPlot::semPaths(pm_pa2, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) pm_pa3 <- keep_nodes(pm_pa, c("x1", "x3", "x4")) semPlot::semPaths(pm_pa3, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m)
Determine the order of indicators and match indicators and factors based on a 'lavaan' model syntax.
lavaan_indicator_order(model_syntax)lavaan_indicator_order(model_syntax)
model_syntax |
A string that
should be a model specified in
|
It generates a named vector
for the argument indicator_order
of set_cfa_layout() and
set_sem_layout() using a lavaan
model syntax.
A variable is considered an indicator
if it is on the right-hand side
of the operator =~.
If an indicator loaded on more than one latent variable, it will only be matched to one of them, determined by the order of appearance in the internal storage.
A named character vector. The values are the indicators in the model syntax. The names are the latent factors the indicators loaded on.
set_sem_layout() and
set_cfa_layout().
mod <- 'f1 =~ x01 + x02 + x03 + x06 f4 =~ x11 + x12 + x13 + x14 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 + x03 ' lavaan_indicator_order(mod) mod <- 'f1 =~ x01 + x02 + x03 + x06 f3 =~ x08 + x09 + x10 + x03 f2 =~ x04 + x05 + x06 + x07 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f3 ' lavaan_indicator_order(mod)mod <- 'f1 =~ x01 + x02 + x03 + x06 f4 =~ x11 + x12 + x13 + x14 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 + x03 ' lavaan_indicator_order(mod) mod <- 'f1 =~ x01 + x02 + x03 + x06 f3 =~ x08 + x09 + x10 + x03 f2 =~ x04 + x05 + x06 + x07 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f3 ' lavaan_indicator_order(mod)
Create the layout matrix from a list of coordinates for semPaths.
layout_matrix(...)layout_matrix(...)
... |
Each node in the matrix is specified by this form: |
The layout argument in semPlot::semPaths() accepts a
matrix with node labels as the elements, and NA for empty cells.
This function allows user to create the matrix using a list of
coordinates for the node labels.
Note that semPlot::semPaths() only
treat a matrix as a layout matrix as
a matrix of grid if it has three or
more columns. Therefore, if the resulting
matrix has only one or two columns, it
will be converted to a matrix with
three columns, by inserting a column
of NAs for a two-column matrix, and
inserting the resulting matrix between
two columns of NAs for a one-column
matrix.
A layout matrix for the layout argument of
semPlot::semPaths().
# Suppose this is the layout to be created: m0 <- matrix(c("x1", NA, NA, NA, "x2", "x3", NA, NA, NA, "x4", NA, "x5"), byrow = TRUE, 3, 4) # This call will create the same matrix. m1 <- layout_matrix(x1 = c(1, 1), x2 = c(2, 1), x3 = c(2, 2), x4 = c(3, 2), x5 = c(3, 4)) #The two matrices should be identical. m0 == m1# Suppose this is the layout to be created: m0 <- matrix(c("x1", NA, NA, NA, "x2", "x3", NA, NA, NA, "x4", NA, "x5"), byrow = TRUE, 3, 4) # This call will create the same matrix. m1 <- layout_matrix(x1 = c(1, 1), x2 = c(2, 1), x3 = c(2, 2), x4 = c(3, 2), x5 = c(3, 4)) #The two matrices should be identical. m0 == m1
Add standard error or confidence interval estimates, in parentheses, to parameter estimates (edge labels) in a qgraph::qgraph object.
mark_se( semPaths_plot, object = NULL, sep = " ", digits = 2L, ests = NULL, std_type = FALSE ) mark_ci( semPaths_plot, object = NULL, sep = " ", digits = 2L, ests = NULL, std_type = FALSE )mark_se( semPaths_plot, object = NULL, sep = " ", digits = 2L, ests = NULL, std_type = FALSE ) mark_ci( semPaths_plot, object = NULL, sep = " ", digits = 2L, ests = NULL, std_type = FALSE )
semPaths_plot |
A qgraph object generated by
|
object |
The object used by semPaths to generate the plot. Use
the same argument name used in |
sep |
A character string to separate the coefficient and the
standard error (in parentheses). Default to " " (one space). Use
|
digits |
Integer indicating number of decimal places for the appended standard errors. Default is 2L. |
ests |
A data.frame from the
|
std_type |
If standardized solution is used in the plot,
set this either to the type of standardization (e.g., |
Modify a qgraph::qgraph object generated by
semPaths (currently in parentheses) to the
labels. Require either the original object used in the semPaths call,
or a data frame with the standard error (or confidence interval) for
each parameter. The latter option is for standard errors/confidence
interval not computed by lavaan but by other functions.
Currently supports only plots based on lavaan
output.
This function is a variant of, and can be combined with, the
mark_sig function.
If the input is a qgraph::qgraph object, the function returns a qgraph based on the original one, with standard error or confidence interval estimates appended. If the input is a list of qgraph objects, the function returns a list of the same length.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2 <- mark_se(p_pa, fit_pa) plot(p_pa2) mod_cfa <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::sem(mod_cfa, cfa_example) lavaan::parameterEstimates(fit_cfa)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] p_cfa <- semPlot::semPaths(fit_cfa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0) # Place standard errors on a new line p_cfa2 <- mark_se(p_cfa, fit_cfa, sep = "\n") plot(p_cfa2) mod_sem <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' fit_sem <- lavaan::sem(mod_sem, sem_example) lavaan::parameterEstimates(fit_sem)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] p_sem <- semPlot::semPaths(fit_sem, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0) # Mark significance, and then add standard errors p_sem2 <- mark_sig(p_sem, fit_sem) p_sem3 <- mark_se(p_sem2, fit_sem, sep = "\n") plot(p_sem3) # Add confidence intervals p_sem4 <- mark_ci(p_sem, fit_sem, sep = "\n") plot(p_sem4)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2 <- mark_se(p_pa, fit_pa) plot(p_pa2) mod_cfa <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::sem(mod_cfa, cfa_example) lavaan::parameterEstimates(fit_cfa)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] p_cfa <- semPlot::semPaths(fit_cfa, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0) # Place standard errors on a new line p_cfa2 <- mark_se(p_cfa, fit_cfa, sep = "\n") plot(p_cfa2) mod_sem <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' fit_sem <- lavaan::sem(mod_sem, sem_example) lavaan::parameterEstimates(fit_sem)[ , c("lhs", "op", "rhs", "est", "pvalue", "se")] p_sem <- semPlot::semPaths(fit_sem, whatLabels = "est", style = "ram", nCharNodes = 0, nCharEdges = 0) # Mark significance, and then add standard errors p_sem2 <- mark_sig(p_sem, fit_sem) p_sem3 <- mark_se(p_sem2, fit_sem, sep = "\n") plot(p_sem3) # Add confidence intervals p_sem4 <- mark_ci(p_sem, fit_sem, sep = "\n") plot(p_sem4)
Mark parameter estimates (edge labels) based on p-value.
mark_sig( semPaths_plot, object = NULL, alphas = c(`*` = 0.05, `**` = 0.01, `***` = 0.001), ests = NULL, std_type = FALSE, ests_r2 = NULL, r2_prefix = "R2=" )mark_sig( semPaths_plot, object = NULL, alphas = c(`*` = 0.05, `**` = 0.01, `***` = 0.001), ests = NULL, std_type = FALSE, ests_r2 = NULL, r2_prefix = "R2=" )
semPaths_plot |
A qgraph::qgraph object generated by semPaths, or a similar qgraph object modified by other semptools functions. |
object |
The object used by semPaths to generate the plot. Use
the same argument name used in semPaths to make the meaning of this
argument obvious. Currently only object of class
|
alphas |
A named numeric vector. Each element is the cutoff (level of significance), and the name of it is the symbol to be used if p-value is less than this cutoff. The default is c("" = .05, "" = .01, "" = .001). |
ests |
A data.frame from the
|
std_type |
If standardized solution is used in the plot,
set this either to the type of standardization (e.g., |
ests_r2 |
A data.frame with
these columns: |
r2_prefix |
The prefix used to
identify R-squares in |
Modify a qgraph::qgraph object generated by semPaths and add marks (currently asterisk, "*") to the labels based on their p-values. Require either the original object used in the semPaths call, or a data frame with the p-values for each parameter. The latter option is for p-values not computed by lavaan but by other functions.
Currently supports only plots based on lavaan output.
Normally, lavaan does not compute
p-values for R-squares. If
R-squares are detected in the plot
(based on r2_prefix), they will not
be marked, except for the following
conditions:
Users set ests to a data frame
with R-squares (op = "r2") as well
as their p-values (under pvalue)
computed by some methods.
Users set ests_r2 to a data frame
storing only the p-values for
R-squares computed by some methods
(and let the function find the
p-values for other parameters from
object).
A qgraph::qgraph based on the original one, with marks appended to edge labels based on their p-values.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2 <- mark_sig(p_pa, fit_pa) plot(p_pa2) mod_cfa <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::sem(mod_cfa, cfa_example) lavaan::parameterEstimates(fit_cfa)[, c("lhs", "op", "rhs", "est", "pvalue")] p_cfa <- semPlot::semPaths(fit_cfa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0) p_cfa2 <- mark_sig(p_cfa, fit_cfa) plot(p_cfa2) mod_sem <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' fit_sem <- lavaan::sem(mod_sem, sem_example) lavaan::parameterEstimates(fit_sem)[, c("lhs", "op", "rhs", "est", "pvalue")] p_sem <- semPlot::semPaths(fit_sem, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0) p_sem2 <- mark_sig(p_sem, fit_sem) plot(p_sem2)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2 <- mark_sig(p_pa, fit_pa) plot(p_pa2) mod_cfa <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::sem(mod_cfa, cfa_example) lavaan::parameterEstimates(fit_cfa)[, c("lhs", "op", "rhs", "est", "pvalue")] p_cfa <- semPlot::semPaths(fit_cfa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0) p_cfa2 <- mark_sig(p_cfa, fit_cfa) plot(p_cfa2) mod_sem <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' fit_sem <- lavaan::sem(mod_sem, sem_example) lavaan::parameterEstimates(fit_sem)[, c("lhs", "op", "rhs", "est", "pvalue")] p_sem <- semPlot::semPaths(fit_sem, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0) p_sem2 <- mark_sig(p_sem, fit_sem) plot(p_sem2)
Move the nodes in an
plot generated by semPlot::semPaths().
move_node(semPaths_plot, move_by = NULL, check_nodes = TRUE)move_node(semPaths_plot, move_by = NULL, check_nodes = TRUE)
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
move_by |
A named list. The
names are the names of nodes to be
moved. Each element of the list can
be either a named numeric vector or
an unnamed vector of two numbers. If
a named vector, |
check_nodes |
Logical. If |
Modify a qgraph::qgraph object generated by semPlot::semPaths and move selected nodes.
The change assumes that the layout is defined with 0 as the center, -1 to 1 from left to right, and from bottom to top.
A qgraph::qgraph based on
the original one, with the selected
nodes moved.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_changed <- move_node(p_pa, list(x3 = c(x = -.25, y = -.25), x4 = c(y = .5))) plot(p_changed)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_changed <- move_node(p_pa, list(x3 = c(x = -.25, y = -.25), x4 = c(y = .5))) plot(p_changed)
A sample dataset for fitting a path analysis model.
pa_examplepa_example
An object of class data.frame with 100 rows and 4 columns.
Four variables (x1 to x4), 100 cases.
Sample model to fit (in lavaan::model.syntax notation)
mod <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 '
A sample dataset for fitting a path analysis model, with three control variables.
pa_example_3covspa_example_3covs
An object of class data.frame with 100 rows and 7 columns.
Four variables (x1 to x4), and three control variables
(cov1, cov2, cov3), 100 cases.
Sample model to fit (in lavaan::model.syntax notation)
mod <- ' x3 ~ x1 + x2 + cov1 +cov2 + cov3 x4 ~ x1 + x3 + cov1 +cov2 + cov3 '
Simple-to-use functions for generating plots of common models.
quick_parallel_mediation( object, x, m, y, mediators_position = c("center", "top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) q_parallel( object, x, m, y, mediators_position = c("center", "top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) quick_serial_mediation( object, x, m, y, mediators_position = c("top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) q_serial( object, x, m, y, mediators_position = c("top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) quick_simple_mediation( object, x, m, y, mediators_position = c("top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) q_simple( object, x, m, y, mediators_position = c("top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL )quick_parallel_mediation( object, x, m, y, mediators_position = c("center", "top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) q_parallel( object, x, m, y, mediators_position = c("center", "top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) quick_serial_mediation( object, x, m, y, mediators_position = c("top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) q_serial( object, x, m, y, mediators_position = c("top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) quick_simple_mediation( object, x, m, y, mediators_position = c("top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL ) q_simple( object, x, m, y, mediators_position = c("top", "bottom"), what = "path", whatLabels = "est", style = c("lisrel", "ram"), nCharNodes = 0, nCharEdges = 0, sizeMan = NULL, sizeLat = NULL, edge.label.cex = NULL, intercepts = FALSE, ..., plot_now = TRUE, do_mark_se = TRUE, do_mark_sig = TRUE, do_rotate_resid = TRUE, do_add_rsq = TRUE, add_notes = FALSE, notes = NULL )
object |
A |
x |
The name of the |
m |
The name(s) of the |
y |
The name of the |
mediators_position |
For a
simple mediation model, it can be
either |
what |
The same argument of
|
whatLabels |
The same argument
of |
style |
The same argument of
|
nCharNodes |
The same argument of
|
nCharEdges |
The same argument of
|
sizeMan |
The same argument of
|
sizeLat |
The same argument of
|
edge.label.cex |
The same
argument of |
intercepts |
The same
argument of |
... |
Other arguments to be
passed to to |
plot_now |
Logical. If |
do_mark_se |
Logical. If |
do_mark_sig |
Logical. If
|
do_rotate_resid |
Logical. If
|
do_add_rsq |
Logical. If
|
add_notes |
Logical. If
|
notes |
A string, the notes to
be printed if |
A collection of functions for generating the plots of common models. They are designed to need as few arguments as possible to have a plot that should need minimal postprocessing.
Currently, functions are available for these plots:
Simple mediation models (a model
with only one mediator):
q_simple() or
quick_simple_mediation().
Parallel mediation models (a model
with one or more paths between two
variables, each path with only one
mediator): q_parallel() or
quick_parallel_mediation().
Serial mediation models (a model
with one main path between two
variables, withe one or more
mediators along the path):
q_serial() or
quick_serial_mediation().
For these three functions, if the
default settings are desired, users
only need to supply the lavaan
output, and specify:
The x variable (predictor) to
be included.
The m variable(s) to be included,
which is a character vector if the
model has more than one mediator.
The y variable to be included.
These variables can be observed
variables or latent factors. Indicators
of latent variables will not be drawn
(unless they are listed in x,
m, or y).
The layout is determined by the
argument mediators_position, with
two or more preset layouts for each
model.
By default, the following will be added to the plot:
Asterisks included to denote the
significance test results
(implemented by mark_sig()).
Standard errors included for free
parameters (implemented by
mark_se()).
R-squares are drawn in place of
error variances for the m
variable(s) and y variable
(implemented by add_rsq()).
These options can be turned off if so desired.
Unlike other function in semptools,
these functions are usually used to
plot a model immediately. Therefore,
the resulting plot will be plotted by
default. Turned this off by setting
plot_now to FALSE (analogous to
setting DoNotPlot to FALSE when
calling semPlot::semPaths()).
Although the plot is designed to be
ready-to-plot, it can be further
processed by other semptools
functions if necessary, just like the
plot of semPlot::semPaths().
For readability, it is common for researchers to omit some variables when drawing a model.
For example:
m ~ x + c1 + c2
y ~ m + x + c1 + c2
If c1 and c2 are control variables,
researchers want to draw only these\
paths
m ~ x
y ~ m + x
The quick plot functions can be used for this purpose. Only selected variables will be included in the plots.
Researchers may also want to draw
several plots, one for each pair of
the predictor (the x-variable) and
the outcome variable (the
y-variable).
For example,
m ~ x + c1 + c2
y1 ~ m + x + c1 + c2
y2 ~ m + x + c1 + c2
For this model, in addition to
excluding the control variables,
researchers may want to generate two
diagrams, one for y1:
m ~ x
y1 ~ m + x
and the other for y2:
m ~ x
y2 ~ m + x
Note that all the functions will not
check the models. The specification
of x, m, and y are assumed to
be valid for the fitted models.
A qgraph::qgraph generated
by semPlot::semPaths() and
customized by other semptools
functions is returned invisibly.
Called for its side effect.
library(lavaan) library(semPlot) # ---- Parallel Mediation Model mod_parallel <- 'x04 ~ x01 x05 ~ x01 x06 ~ x01 x07 ~ x01 x10 ~ x04 + x05 + x06 + x07 + x01' fit_parallel <- lavaan::sem(mod_parallel, sem_example) q_parallel(fit_parallel, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x10") q_parallel(fit_parallel, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x10", mediators_position = "top") q_parallel(fit_parallel, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x10", mediators_position = "bottom") # Suppress some elements for readability q_parallel(fit_parallel, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x10", mediators_position = "bottom", do_mark_se = FALSE) # ---- Serial Mediation Model mod_serial <- 'x04 ~ x01 x05 ~ x04 + x01 x06 ~ x04 + x05 + x01 x07 ~ x04 + x05 + x06 + x01 x08 ~ x04 + x05 + x06 + x07 + x01' fit_serial <- lavaan::sem(mod_serial, sem_example) q_serial(fit_serial, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x08") q_serial(fit_serial, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x08", mediators_position = "bottom") # Suppress some elements for readability q_serial(fit_serial, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x08", mediators_position = "bottom", do_mark_se = FALSE) # ---- Simple Mediation Model: With Control Variables mod_pa <- 'x3 ~ x1 + x2 x4 ~ x3 + x1 + x2' fit_pa <- lavaan::sem(mod_pa, pa_example) mod_sem <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3' fit_sem <- lavaan::sem(mod_sem, sem_example) q_simple(fit_pa, x = "x1", m = "x3", y = "x4") # Drawing latent factors only q_simple(fit_sem, x = "f1", m = "f3", y = "f4", whatLabels = "std", mediators_position = "bottom")library(lavaan) library(semPlot) # ---- Parallel Mediation Model mod_parallel <- 'x04 ~ x01 x05 ~ x01 x06 ~ x01 x07 ~ x01 x10 ~ x04 + x05 + x06 + x07 + x01' fit_parallel <- lavaan::sem(mod_parallel, sem_example) q_parallel(fit_parallel, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x10") q_parallel(fit_parallel, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x10", mediators_position = "top") q_parallel(fit_parallel, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x10", mediators_position = "bottom") # Suppress some elements for readability q_parallel(fit_parallel, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x10", mediators_position = "bottom", do_mark_se = FALSE) # ---- Serial Mediation Model mod_serial <- 'x04 ~ x01 x05 ~ x04 + x01 x06 ~ x04 + x05 + x01 x07 ~ x04 + x05 + x06 + x01 x08 ~ x04 + x05 + x06 + x07 + x01' fit_serial <- lavaan::sem(mod_serial, sem_example) q_serial(fit_serial, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x08") q_serial(fit_serial, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x08", mediators_position = "bottom") # Suppress some elements for readability q_serial(fit_serial, x = "x01", m = c("x04", "x05", "x06", "x07"), y = "x08", mediators_position = "bottom", do_mark_se = FALSE) # ---- Simple Mediation Model: With Control Variables mod_pa <- 'x3 ~ x1 + x2 x4 ~ x3 + x1 + x2' fit_pa <- lavaan::sem(mod_pa, pa_example) mod_sem <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3' fit_sem <- lavaan::sem(mod_sem, sem_example) q_simple(fit_pa, x = "x1", m = "x3", y = "x4") # Drawing latent factors only q_simple(fit_sem, x = "f1", m = "f3", y = "f4", whatLabels = "std", mediators_position = "bottom")
Rescale the layout of a
qgraph object, such as the output
of semptools functions that modify
the output of semPlot::semPaths().
rescale_layout(semPaths_plot, x_min = -1, x_max = 1, y_min = -1, y_max = 1)rescale_layout(semPaths_plot, x_min = -1, x_max = 1, y_min = -1, y_max = 1)
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
x_min, x_max, y_min, y_max
|
The
ranges of x-coordinates and
y-coordinates after rescaling.
Default is -1 for |
The plot generated by some
functions, such as
set_sem_layout(), may have the area
underused for some model. This
function rescale the layout matrix,
just like what the rescale argument
of semPlot::semPaths() does.
A qgraph::qgraph based on
the original one, with the layout
matrix rescaled.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + x04 f4 ~ f3 + x05' fit_sem <- sem(mod, sem_example) p <- semPaths(fit_sem, whatLabels="est", sizeMan = 5, nCharNodes = 0, nCharEdges = 0, edge.width = 0.8, node.width = 0.7, edge.label.cex = 0.6, mar = c(10, 10, 10, 10), DoNotPlot = TRUE) plot(p) indicator_order <- c("x04", "x05", "x01", "x02", "x03", "x11", "x12", "x13", "x14", "x08", "x09", "x10") indicator_factor <- c("x04", "x05", "f1", "f1", "f1", "f4", "f4", "f4", "f4", "f3", "f3", "f3") factor_layout <- matrix(c( "f1", "f3", "f4", "x04", "x05", NA), byrow = TRUE, 2, 3) factor_point_to <- matrix(c("left", "up", "right", NA, NA, NA), byrow = TRUE, 2, 3) p2 <- set_sem_layout(p, indicator_order = indicator_order, indicator_factor = indicator_factor, factor_layout = factor_layout, factor_point_to = factor_point_to) # The original plot with too much unused area plot(p2) rect(-1, -1, 1, 1) rect(-1.5, -1.5, 1.5, 1.5) # Expand the plot p3 <- p2 p3 <- rescale_layout(p3) plot(p3) rect(-1, -1, 1, 1) rect(-1.5, -1.5, 1.5, 1.5)library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + x04 f4 ~ f3 + x05' fit_sem <- sem(mod, sem_example) p <- semPaths(fit_sem, whatLabels="est", sizeMan = 5, nCharNodes = 0, nCharEdges = 0, edge.width = 0.8, node.width = 0.7, edge.label.cex = 0.6, mar = c(10, 10, 10, 10), DoNotPlot = TRUE) plot(p) indicator_order <- c("x04", "x05", "x01", "x02", "x03", "x11", "x12", "x13", "x14", "x08", "x09", "x10") indicator_factor <- c("x04", "x05", "f1", "f1", "f1", "f4", "f4", "f4", "f4", "f3", "f3", "f3") factor_layout <- matrix(c( "f1", "f3", "f4", "x04", "x05", NA), byrow = TRUE, 2, 3) factor_point_to <- matrix(c("left", "up", "right", NA, NA, NA), byrow = TRUE, 2, 3) p2 <- set_sem_layout(p, indicator_order = indicator_order, indicator_factor = indicator_factor, factor_layout = factor_layout, factor_point_to = factor_point_to) # The original plot with too much unused area plot(p2) rect(-1, -1, 1, 1) rect(-1.5, -1.5, 1.5, 1.5) # Expand the plot p3 <- p2 p3 <- rescale_layout(p3) plot(p3) rect(-1, -1, 1, 1) rect(-1.5, -1.5, 1.5, 1.5)
Rotate the residuals of selected nodes.
rotate_resid(semPaths_plot, rotate_resid_list = NULL, check_nodes = TRUE)rotate_resid(semPaths_plot, rotate_resid_list = NULL, check_nodes = TRUE)
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
rotate_resid_list |
A named vector or a list of named list. For
a named vector, the name of an element is the node for which its
residual is to be rotated, and the value is the degree to rotate.
The 12 o'clock position is zero degree. Positive degree denotes
clockwise rotation, and negative degree denotes anticlockwise
rotation. For example, |
check_nodes |
Logical. If |
Modify a qgraph::qgraph object generated by semPlot::semPaths and rotate the residuals of selected nodes. Currently only supports "ram" and similar styles of semPlot::semPaths.
A qgraph::qgraph object based on the original one, with
loopRotation attributes of selected nodes modified.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_rotate_resid_vector <- c(x3 = 45, x4 = -45) p_pa2v <- rotate_resid(p_pa, my_rotate_resid_vector) plot(p_pa2v) my_rotate_resid_list <- list(list(node = "x3", rotate = 45), list(node = "x4", rotate = -45)) p_pa2l <- rotate_resid(p_pa, my_rotate_resid_list) plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_rotate_resid_vector <- c(x3 = 45, x4 = -45) p_pa2v <- rotate_resid(p_pa, my_rotate_resid_vector) plot(p_pa2v) my_rotate_resid_list <- list(list(node = "x3", rotate = 45), list(node = "x4", rotate = -45)) p_pa2l <- rotate_resid(p_pa, my_rotate_resid_list) plot(p_pa2l)
Move the edge labels away from path intersections.
safe_edge_label_position( object, layout = NULL, default_pos = 0.5, tolerance = 0.05, update_plot = TRUE )safe_edge_label_position( object, layout = NULL, default_pos = 0.5, tolerance = 0.05, update_plot = TRUE )
object |
It can be the output of
|
layout |
A layout matrix.
Required if |
default_pos |
Used if |
tolerance |
If the distance between a position and an intersection is greater than this distance, then a position is considered safe and will not be adjusted. |
update_plot |
Logical. Used on
if |
This function identify all intersection points between two paths in a model, and set the position of an edge label to the mid-point of a line segment between an intersection point and the another intersection point or the origin/destination of a path.
This function is intended for having
a "likely" readable graph with as
little user-intervention as possible.
If precise control of the edge label
positions is desired, use
set_edge_label_position().
The function supports a multigroup model and a list of plots. However, the structural parts of all groups or plots must be identical: same variables, and same paths.
If object is a lavaan-class
object, it returns
a named numeric vector of edge
positions to be used by
set_edge_label_position().
If object is a qgraph object
and update_plot is TRUE, it
returns a qgraph object with the
adjusted edge label positions.
Otherwise, it returns a named vector
of the position to be adjusted, as
for a lavaan-class object.
If object is a list of
qgraph::qgraph objects and
update_plot is TRUE, then it returns
a
list of processed qgraph::qgraph objects.
set_edge_label_position()
on setting the positions of edge
labels.
library(lavaan) library(semPlot) # Create a dummy dataset mod_pa <- " m11 ~ c1 + x1 m12 ~ c2 + m11 + m21 m21 ~ c1 + x1 m22 ~ c1 + m21 + m11 y ~ m12 + m22 + x1 " fit <- lavaan::sem( mod_pa, do.fit = FALSE ) dat <- simulateData( parameterTable(fit), sample.nobs = 500, seed = 1234 ) fit <- lavaan::sem( mod_pa, dat ) # Set the layout m <- auto_layout_mediation( fit, exclude = c("c1", "c2", "c3") ) pos_new <- safe_edge_label_position( fit, layout = m ) pos_new pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2")) p <- semPaths( pm, whatLabels = "est", layout = m, DoNotPlot = TRUE ) # Three labels overlap with each other plot(p) # Update the plot p_safe <- p |> safe_edge_label_position() # Three labels do not overlap in this plot plot(p_safe) # Set the position manually p_safe2 <- p |> set_edge_label_position(pos_new) plot(p_safe2)library(lavaan) library(semPlot) # Create a dummy dataset mod_pa <- " m11 ~ c1 + x1 m12 ~ c2 + m11 + m21 m21 ~ c1 + x1 m22 ~ c1 + m21 + m11 y ~ m12 + m22 + x1 " fit <- lavaan::sem( mod_pa, do.fit = FALSE ) dat <- simulateData( parameterTable(fit), sample.nobs = 500, seed = 1234 ) fit <- lavaan::sem( mod_pa, dat ) # Set the layout m <- auto_layout_mediation( fit, exclude = c("c1", "c2", "c3") ) pos_new <- safe_edge_label_position( fit, layout = m ) pos_new pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2")) p <- semPaths( pm, whatLabels = "est", layout = m, DoNotPlot = TRUE ) # Three labels overlap with each other plot(p) # Update the plot p_safe <- p |> safe_edge_label_position() # Three labels do not overlap in this plot plot(p_safe) # Set the position manually p_safe2 <- p |> set_edge_label_position(pos_new) plot(p_safe2)
Rotate the residuals (or R-squares) to avoid overlapping with paths.
safe_resid_position( object, layout, default_angle = 0, style = c("1200", "geometry"), update_plot = TRUE )safe_resid_position( object, layout, default_angle = 0, style = c("1200", "geometry"), update_plot = TRUE )
object |
It can be the output of
|
layout |
A layout matrix.
Required if |
default_angle |
Used if |
style |
The convention for
the angles. If |
update_plot |
Logical. Used on
if |
This function identify all directed paths connected to a node, and find the largest arc with no directed paths. The residual (or R-square) is then set to the mid-point of this arc.
This function is intended for having
a "likely" readable graph with as
little user-intervention as possible.
If precise control of the positions
is desired, use rotate_resid().
Only directed paths (single-headed arrows) will be considered. Bidirectional paths such as covariances are not taken into account.
The function supports a multigroup model and a list of plots. However, the structural parts of all groups or plots must be identical: same variables, and same paths.
If object is a lavaan-class
object, it returns
a named numeric vector of residual
angles to be used by
rotate_resid().
If object is a qgraph object
and update_plot is TRUE, it
returns a qgraph object with the
residuals's angles adjusted.
Otherwise, it returns a named vector
of the angles, as
for a lavaan-class object.
If object is a list of
qgraph::qgraph objects and
update_plot is TRUE, then it returns
a
list of processed qgraph::qgraph objects.
rotate_resid()
on rotating a residual.
library(lavaan) library(semPlot) # Create a dummy dataset mod_pa <- " m11 ~ x1 m21 ~ m11 m2 ~ m11 m22 ~ m11 y ~ m2 + m21 + m22 + x1 " fit <- lavaan::sem( mod_pa, do.fit = FALSE ) dat <- simulateData( parameterTable(fit), sample.nobs = 500, seed = 1234 ) fit <- lavaan::sem( mod_pa, dat ) # Set the layout m <- auto_layout_mediation( fit ) p <- semPaths( fit, whatLabels = "est", layout = m, DoNotPlot = TRUE ) |> safe_edge_label_position() plot(p) # Update the plot p_safe <- p |> safe_resid_position() plot(p_safe) # Set the positon manually pos_new <- safe_resid_position(p, update_plot = FALSE) pos_new p_safe2 <- p |> rotate_resid(pos_new) plot(p_safe2)library(lavaan) library(semPlot) # Create a dummy dataset mod_pa <- " m11 ~ x1 m21 ~ m11 m2 ~ m11 m22 ~ m11 y ~ m2 + m21 + m22 + x1 " fit <- lavaan::sem( mod_pa, do.fit = FALSE ) dat <- simulateData( parameterTable(fit), sample.nobs = 500, seed = 1234 ) fit <- lavaan::sem( mod_pa, dat ) # Set the layout m <- auto_layout_mediation( fit ) p <- semPaths( fit, whatLabels = "est", layout = m, DoNotPlot = TRUE ) |> safe_edge_label_position() plot(p) # Update the plot p_safe <- p |> safe_resid_position() plot(p_safe) # Set the positon manually pos_new <- safe_resid_position(p, update_plot = FALSE) pos_new p_safe2 <- p |> rotate_resid(pos_new) plot(p_safe2)
A sample dataset for fitting a latent variable model with two 2nd-order factors.
sem_2nd_order_examplesem_2nd_order_example
An object of class data.frame with 500 rows and 21 columns.
Twenty one variables (x01 to x21), 500 cases.
Sample model to fit (in lavaan::model.syntax notation)
mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f5 =~ x15 + x16 + x17 + x18 f6 =~ x19 + x20 + x21 f21 =~ 1*f1 + f3 + f4 f22 =~ 1*f2 + f5 + f6 f22 ~ f21 '
A sample dataset for fitting a latent variable model.
sem_examplesem_example
An object of class data.frame with 200 rows and 14 columns.
Fourteen variables (x01 to x14), 100 cases.
Sample model to fit (in lavaan::model.syntax notation)
mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 '
Configure the layout of factors and adjust other aspects of a CFA graph by semPaths.
set_cfa_layout( semPaths_plot, indicator_order = NULL, indicator_factor = NULL, fcov_curve = 0.4, loading_position = 0.5, point_to = "down" )set_cfa_layout( semPaths_plot, indicator_order = NULL, indicator_factor = NULL, fcov_curve = 0.4, loading_position = 0.5, point_to = "down" )
semPaths_plot |
A qgraph::qgraph object generated by semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
indicator_order |
A string vector of the indicators. The order
of the names is the order of the indicators in the graph, when they
are drawn on the bottom of the graph. The indicators should be
grouped by the factors on which they load on. For example, if x1,
x2, x4 load on f2, and x3, x5, x6 load on f1, then vector should be
either c("x1", "x2", "x4", "x3", "x5", "x6") or c("x3", "x5", "x6",
"x1", "x2", "x4"). Indicators within a group can be ordered in any
way. If it is a named vector, its names will be used for the
argument |
indicator_factor |
A string vector of the same length of the
indicator order, storing the name of the factor for which each of
the indicator in indicator_factor loads on. For example, if x1, x2,
x4 load on f2, and x3, x5, x6 load on f1, and indicator_order is
c("x3", "x5", "x6", "x1", "x2", "x4"), then indicator_factor should
be c("f2", "f2", "f2", "f1", "f1", "f1"). If |
fcov_curve |
A number used to set the curvature of the inter-factor covariances. Default is .4. |
loading_position |
The positions of all factor loadings. Default is .5, on the middle of the arrows. Larger the number, closer the loadings to the indicators. Smaller the number, closer the loadings to the factors. |
point_to |
Can be "down", "left", "up", or "right". Specify the direction that the factors "point" to the indicators. Default is "down". |
Modify a qgraph::qgraph object generated by semPaths based on a confirmatory factor analysis model.
A qgraph::qgraph based on the original one, with various
aspects of the model modified.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::sem(mod, cfa_example) lavaan::parameterEstimates(fit_cfa)[, c("lhs", "op", "rhs", "est", "pvalue")] p <- semPaths(fit_cfa, whatLabels="est", sizeMan = 2.5, nCharNodes = 0, nCharEdges = 0, edge.width = 0.8, node.width = 0.7, edge.label.cex = 0.6, style = "ram", mar = c(10,10,10,10)) indicator_order <- c("x04", "x05", "x06", "x07", "x01", "x02", "x03", "x11", "x12", "x13", "x14", "x08", "x09", "x10") indicator_factor <- c( "f2", "f2", "f2", "f2", "f1", "f1", "f1", "f4", "f4", "f4", "f4", "f3", "f3", "f3") p2 <- set_cfa_layout(p, indicator_order, indicator_factor, fcov_curve = 1.5, loading_position = .8) plot(p2) # Use a named vector for indicator_order indicator_order2 <- c(f2 = "x04", f2 = "x05", f2 = "x06", f2 = "x07", f1 = "x01", f1 = "x02", f1 = "x03", f4 = "x11", f4 = "x12", f4 = "x13", f4 = "x14", f3 = "x08", f3 = "x09", f3 = "x10") p2 <- set_cfa_layout(p, indicator_order = indicator_order2, fcov_curve = 1.5, loading_position = .8) plot(p2) # Use automatically generated indicator_order and indicator_factor p2 <- set_cfa_layout(p, fcov_curve = 1.5, loading_position = .8) plot(p2) p2 <- set_cfa_layout(p, indicator_order, indicator_factor, fcov_curve = 1.5, loading_position = .8, point_to = "left") plot(p2) p2 <- set_cfa_layout(p, indicator_order, indicator_factor, fcov_curve = 1.5, loading_position = .8, point_to = "up") plot(p2) p2 <- set_cfa_layout(p, indicator_order, indicator_factor, fcov_curve = 1.5, loading_position = .8, point_to = "right") plot(p2)library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 ' fit_cfa <- lavaan::sem(mod, cfa_example) lavaan::parameterEstimates(fit_cfa)[, c("lhs", "op", "rhs", "est", "pvalue")] p <- semPaths(fit_cfa, whatLabels="est", sizeMan = 2.5, nCharNodes = 0, nCharEdges = 0, edge.width = 0.8, node.width = 0.7, edge.label.cex = 0.6, style = "ram", mar = c(10,10,10,10)) indicator_order <- c("x04", "x05", "x06", "x07", "x01", "x02", "x03", "x11", "x12", "x13", "x14", "x08", "x09", "x10") indicator_factor <- c( "f2", "f2", "f2", "f2", "f1", "f1", "f1", "f4", "f4", "f4", "f4", "f3", "f3", "f3") p2 <- set_cfa_layout(p, indicator_order, indicator_factor, fcov_curve = 1.5, loading_position = .8) plot(p2) # Use a named vector for indicator_order indicator_order2 <- c(f2 = "x04", f2 = "x05", f2 = "x06", f2 = "x07", f1 = "x01", f1 = "x02", f1 = "x03", f4 = "x11", f4 = "x12", f4 = "x13", f4 = "x14", f3 = "x08", f3 = "x09", f3 = "x10") p2 <- set_cfa_layout(p, indicator_order = indicator_order2, fcov_curve = 1.5, loading_position = .8) plot(p2) # Use automatically generated indicator_order and indicator_factor p2 <- set_cfa_layout(p, fcov_curve = 1.5, loading_position = .8) plot(p2) p2 <- set_cfa_layout(p, indicator_order, indicator_factor, fcov_curve = 1.5, loading_position = .8, point_to = "left") plot(p2) p2 <- set_cfa_layout(p, indicator_order, indicator_factor, fcov_curve = 1.5, loading_position = .8, point_to = "up") plot(p2) p2 <- set_cfa_layout(p, indicator_order, indicator_factor, fcov_curve = 1.5, loading_position = .8, point_to = "right") plot(p2)
Set the curve attributes of selected edges.
set_curve( semPaths_plot, curve_list = NULL, how = c("value", "ratio"), check_direction = TRUE )set_curve( semPaths_plot, curve_list = NULL, how = c("value", "ratio"), check_direction = TRUE )
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
curve_list |
A named vector or a list of named list. For a
named vector, the name of an element should be the path as
specified by lavaan::model.syntax or as appeared in
|
how |
How the width will be changed.
If |
check_direction |
If |
Modified a qgraph::qgraph object generated by semPlot::semPaths and change the curve attributes of selected edges.
If how is set to "ratio", then the
new curve value is equal to the
old curve value multiplied by the
supplied value. This mode lets users
increase or decrease the curvature
of a curve without knowing the original
value.
A qgraph::qgraph based on the original one, with curve
attributes for selected edges changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_curve_vector <- c("x2 ~~ x1" = -1, "x4 ~ x1" = 1) p_pa2v <- set_curve(p_pa, my_curve_vector) plot(p_pa2v) my_curve_list <- list(list(from = "x1", to = "x2", new_curve = -1), list(from = "x1", to = "x4", new_curve = 1)) p_pa2l <- set_curve(p_pa, my_curve_list) plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_curve_vector <- c("x2 ~~ x1" = -1, "x4 ~ x1" = 1) p_pa2v <- set_curve(p_pa, my_curve_vector) plot(p_pa2v) my_curve_list <- list(list(from = "x1", to = "x2", new_curve = -1), list(from = "x1", to = "x4", new_curve = 1)) p_pa2l <- set_curve(p_pa, my_curve_list) plot(p_pa2l)
Set arbitrary attributes of selected edges.
set_edge_attribute( semPaths_plot, values = NULL, attribute_name = NULL, how = c("value", "ratio"), check_direction = TRUE )set_edge_attribute( semPaths_plot, values = NULL, attribute_name = NULL, how = c("value", "ratio"), check_direction = TRUE )
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
values |
A named vector or a list of named list. See the Details section on how to set this argument. |
attribute_name |
The name of the attribute to be changed. |
how |
How the width will be changed.
If |
check_direction |
If |
Modify a qgraph::qgraph object generated by semPlot::semPaths and change the selected attributes of selected edges.
This function is designed to be a general one that changes the attributes named by the user. The user needs to make sure that the attribute actually exists, and the values are valid for the named attribute.
values
This argument can be set in three ways.
For a named vector, the name of an
element should be the path as
specified by lavaan::model.syntax
or as appeared in
lavaan::parameterEstimates().
For example, if the attributes to be
changed are the colors of selected
edges, to change the color of the
path regressing y on x, the name
should be "y ~ x". To change the
color of the covariance between x1
and x2, the name should be "x1 ~~ x2". Therefore, c("y ~ x1" = "red", "x1 ~~ x2" = "blue") changes the
colors of the path from x1 to y
and the covariance between x1 and
x2 to "red" and "blue",
respectively.
The order of the two nodes may matter for covariances. Therefore, if the attribute of a covariance is not changed, try switching the order of the two nodes.
For a list of named lists, each named
list should have three named values:
from, to, and new_value. The
attribute of the edge from from to
to will be set to new_value.
The second approach is no longer recommended, though kept for backward compatibility.
The last approach is setting values
to a one-element vector with no name.
All edges in plot will then have the
selected attributes set to this value.
A qgraph::qgraph based on
the original one, with the selected
attributes of selected edges changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_values_vector <- c("x2 ~~ x1" = "red", "x4 ~ x1" = "blue") p_pa2v <- set_edge_attribute(p_pa, values = my_values_vector, attribute_name = "color") plot(p_pa2v) my_values_list <- list(list(from = "x1", to = "x2", new_value = "red"), list(from = "x1", to = "x4", new_value = "blue")) p_pa2l <- set_edge_attribute(p_pa, values = my_values_list, attribute_name = "color") plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_values_vector <- c("x2 ~~ x1" = "red", "x4 ~ x1" = "blue") p_pa2v <- set_edge_attribute(p_pa, values = my_values_vector, attribute_name = "color") plot(p_pa2v) my_values_list <- list(list(from = "x1", to = "x2", new_value = "red"), list(from = "x1", to = "x4", new_value = "blue")) p_pa2l <- set_edge_attribute(p_pa, values = my_values_list, attribute_name = "color") plot(p_pa2l)
Set the colors of selected edges.
set_edge_color(semPaths_plot, color_list = NULL, check_direction = TRUE)set_edge_color(semPaths_plot, color_list = NULL, check_direction = TRUE)
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
color_list |
A named vector or a list of named list. See the Details section on how to set this argument. |
check_direction |
If |
Modified a qgraph::qgraph object generated by semPlot::semPaths and change the colors of selected edges.
color_list
This argument can be set in two ways.
For a named vector, the name of an
element should be the path as
specified by lavaan::model.syntax
or as appeared in
lavaan::parameterEstimates().
For example, to change the color of the
path regressing y on x, the name
should be "y ~ x". To change the
color of the covariance between x1
and x2, the name should be "x1 ~~ x2". Therefore, c("y ~ x1" = "red", "x1 ~~ x2" = "blue") changes the
colors of the path from x1 to y
and the covariance between x1 and
x2 to "red" and "blue",
respectively.
The order of the two nodes may matter for covariances. Therefore, if the attribute of a covariance is not changed, try switching the order of the two nodes.
For a list of named lists, each named
list should have three named values:
from, to, and new_color. The
attribute of the edge from from to
to will be set to new_color.
The second approach is no longer recommended, though kept for backward compatibility.
A qgraph::qgraph based on
the original one, with colors for
selected edges changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_color_vector <- c("x2 ~~ x1" = "red", "x4 ~ x1" = "blue") p_pa2v <- set_edge_color(p_pa, my_color_vector) plot(p_pa2v) my_color_list <- list(list(from = "x1", to = "x2", new_color = "red"), list(from = "x1", to = "x4", new_color = "blue")) p_pa2l <- set_edge_color(p_pa, my_color_list) plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_color_vector <- c("x2 ~~ x1" = "red", "x4 ~ x1" = "blue") p_pa2v <- set_edge_color(p_pa, my_color_vector) plot(p_pa2v) my_color_list <- list(list(from = "x1", to = "x2", new_color = "red"), list(from = "x1", to = "x4", new_color = "blue")) p_pa2l <- set_edge_color(p_pa, my_color_list) plot(p_pa2l)
Set the attributes of selected edges, such as the label position.
set_edge_label_position( semPaths_plot, position_list = NULL, check_direction = TRUE ) set_edge_label(semPaths_plot, label_list = NULL, check_direction = TRUE) set_edge_label_bg(semPaths_plot, label_bg_list = NULL, check_direction = TRUE) set_edge_label_size( semPaths_plot, label_size_list = NULL, how = c("ratio", "value"), check_direction = TRUE )set_edge_label_position( semPaths_plot, position_list = NULL, check_direction = TRUE ) set_edge_label(semPaths_plot, label_list = NULL, check_direction = TRUE) set_edge_label_bg(semPaths_plot, label_bg_list = NULL, check_direction = TRUE) set_edge_label_size( semPaths_plot, label_size_list = NULL, how = c("ratio", "value"), check_direction = TRUE )
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
position_list |
The new
position. See 'Details' on how to set
this argument. If a list of named
list is used, the element for new
position should be named
|
check_direction |
If |
label_list |
The new label. See
'Details' on how to set this
argument. If a list of named list is
used, the element for new label
should be named |
label_bg_list |
The new
background color. See 'Details' on
how to set this argument. If a list
of named list is used, the element
for new background color should be
named |
label_size_list |
How the
label sizes are to be changed.
See 'Details' on
how to set this argument. If a list
of named list is used, the element
for new background color should be
named |
how |
How the width will be changed.
If |
Modify a qgraph::qgraph object generated by semPlot::semPaths and change the attributes of the edge label of selected edges.
Currently, the following attributes are supported:
Edge label position.
Edge label content.
Edge label background color.
Edge label size.
There are three approach to specify the value.
First, the values can be a named
vector of the values, the recommended
approach. The name of an
element should be the path as
specified by lavaan::model.syntax
or as appeared in
lavaan::parameterEstimates(). For
example, to change position of the
edge label of the path regressing y
on x, the name should be "y ~ x".
Second, the values can be specified
by a list of named lists, each named
list should have three named values:
from, to, and the new value (name
depends on the attribute). The value
of the edge label of the edge from
from to to will be set to this
new value. For example, for
set_edge_label_position(),
list(list(from = "x1", to = "y", new_position = .2), list(from = "x2", to = "y", new_position = .7)) is
equivalent to the named vector above.
The second approach is no longer recommended. It was supported for backward compatibility.
Third, the value can be a single value, which will be used to set the attribute of all edges.
When setting the edge position,
the value is the position. The
mid-point of the edge is 0.5. The
closer the value to 1, the closer the
label to the left-hand-side node (y
in this example). The closer the
value to 0, the close the label to
the right-hand-side node (x in this
example). For example, c("y ~ x1" = .2, "y ~ x2" = .7) moves the path
coefficient from x1 to y closer
to x, and the path coefficient from
x2 to y closer to y.
The function set_edge_label_size()
works in two modes. With how = "ratio",
the new label size is the original
size multiplied by the supplied value.
For example, if the value is 2, the size
of a label is doubled. With how = "value",
the new size is set to be equal to the
supplied value. For example, if the value is
2, the size of a label is set to 2.
label positions for selected edges
changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) # ==== set_edge_label_position ==== my_position_vector <- c( "x3 ~ x2" = .25, "x4 ~ x1" = .75 ) p_pa2v <- set_edge_label_position( p_pa, my_position_vector ) plot(p_pa2v) # This approach is no longer recommended my_position_list <- list( list( from = "x2", to = "x3", new_position = .25 ), list( from = "x1", to = "x4", new_position = .75) ) p_pa2l <- set_edge_label_position( p_pa, my_position_list ) plot(p_pa2l) # ==== set_edge_label ==== my_label_vector <- c( "x3 ~ x2" = "a", "x4 ~ x1" = "b" ) p_pa3v <- set_edge_label( p_pa, my_label_vector ) plot(p_pa3v) # This approach is no longer recommended my_label_list <- list( list( from = "x2", to = "x3", new_label = "a" ), list( from = "x1", to = "x4", new_position = "a") ) p_pa3l <- set_edge_label( p_pa, my_label_list ) plot(p_pa3l) # ==== set_edge_label_bg ==== my_label_bg_vector <- c( "x3 ~ x2" = "red", "x4 ~ x1" = "#00FF00" ) p_pa4v <- set_edge_label_bg( p_pa, my_label_bg_vector ) plot(p_pa4v) # This approach is no longer recommended my_label_bg_list <- list( list( from = "x2", to = "x3", new_label_bg = "red" ), list( from = "x1", to = "x4", new_label_bg = "#00FF00") ) p_pa4l <- set_edge_label_bg( p_pa, my_label_bg_list ) plot(p_pa4l) # ==== set_edge_label_size ==== my_label_size_vector <- c( "x3 ~ x2" = 2, "x4 ~ x1" = 3 ) p_pa5v <- set_edge_label_size( p_pa, my_label_size_vector ) plot(p_pa5v) # This approach is no longer recommended my_label_size_list <- list( list( from = "x2", to = "x3", new_label_size = 2 ), list( from = "x1", to = "x4", new_label_size = 3) ) p_pa5l <- set_edge_label_size( p_pa, my_label_size_list ) plot(p_pa5l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) # ==== set_edge_label_position ==== my_position_vector <- c( "x3 ~ x2" = .25, "x4 ~ x1" = .75 ) p_pa2v <- set_edge_label_position( p_pa, my_position_vector ) plot(p_pa2v) # This approach is no longer recommended my_position_list <- list( list( from = "x2", to = "x3", new_position = .25 ), list( from = "x1", to = "x4", new_position = .75) ) p_pa2l <- set_edge_label_position( p_pa, my_position_list ) plot(p_pa2l) # ==== set_edge_label ==== my_label_vector <- c( "x3 ~ x2" = "a", "x4 ~ x1" = "b" ) p_pa3v <- set_edge_label( p_pa, my_label_vector ) plot(p_pa3v) # This approach is no longer recommended my_label_list <- list( list( from = "x2", to = "x3", new_label = "a" ), list( from = "x1", to = "x4", new_position = "a") ) p_pa3l <- set_edge_label( p_pa, my_label_list ) plot(p_pa3l) # ==== set_edge_label_bg ==== my_label_bg_vector <- c( "x3 ~ x2" = "red", "x4 ~ x1" = "#00FF00" ) p_pa4v <- set_edge_label_bg( p_pa, my_label_bg_vector ) plot(p_pa4v) # This approach is no longer recommended my_label_bg_list <- list( list( from = "x2", to = "x3", new_label_bg = "red" ), list( from = "x1", to = "x4", new_label_bg = "#00FF00") ) p_pa4l <- set_edge_label_bg( p_pa, my_label_bg_list ) plot(p_pa4l) # ==== set_edge_label_size ==== my_label_size_vector <- c( "x3 ~ x2" = 2, "x4 ~ x1" = 3 ) p_pa5v <- set_edge_label_size( p_pa, my_label_size_vector ) plot(p_pa5v) # This approach is no longer recommended my_label_size_list <- list( list( from = "x2", to = "x3", new_label_size = 2 ), list( from = "x1", to = "x4", new_label_size = 3) ) p_pa5l <- set_edge_label_size( p_pa, my_label_size_list ) plot(p_pa5l)
Set the line types of selected edges.
set_edge_line_type( semPaths_plot, line_type_list = NULL, check_direction = TRUE )set_edge_line_type( semPaths_plot, line_type_list = NULL, check_direction = TRUE )
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
line_type_list |
A named vector or a list of named list. See the Details section on how to set this argument. |
check_direction |
If |
Modified a qgraph::qgraph object generated by semPlot::semPaths and change the widths of selected edges.
line_type_list
This argument can be set in three ways.
For a named vector, the name of an
element should be the path as
specified by lavaan::model.syntax
or as appeared in
lavaan::parameterEstimates().
For example, to change the line type of the
path regressing y on x, the name
should be "y ~ x". To change the
line type of the covariance between x1
and x2, the name should be "x1 ~~ x2". Therefore, c("y ~ x1" = "dotted", "x1 ~~ x2" = "dashed") changes the
line types of the path from x1 to y
and the covariance between x1 and
x2 to "dotted" and "dashed",
respectively.
The order of the two nodes may matter for covariances. Therefore, if the attribute of a covariance is not changed, try switching the order of the two nodes.
For a list of named lists, each named
list should have three named values:
from, to, and new_line_type. The
attribute of the edge from from to
to will be set to new_line_type.
The second approach is no longer recommended, though kept for backward compatibility.
The third approach is one single unnamed value. This value will be applied to all edges.
The supported line types are the
same line types supported by
the lty argument of par():
"blank" (0), "solid" (1),
"dashed" (2), "dotted" (3),
"dotdash" (4), "longdash" (5),
and "twodash" (6). The new line
type can be specified in either
strings or integers, but not both.
A qgraph::qgraph based on
the original one, with widths for
selected edges changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_line_type_vector <- c( "x2 ~~ x1" = "dotted", "x4 ~ x1" = "dashed" ) p_pa2v <- set_edge_line_type( p_pa, my_line_type_vector) plot(p_pa2v) my_line_type_list <- list( list( from = "x1", to = "x2", new_line_type = "dotted"), list( from = "x1", to = "x4", new_line_type = "dashed") ) p_pa2l <- set_edge_line_type( p_pa, my_line_type_list ) plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_line_type_vector <- c( "x2 ~~ x1" = "dotted", "x4 ~ x1" = "dashed" ) p_pa2v <- set_edge_line_type( p_pa, my_line_type_vector) plot(p_pa2v) my_line_type_list <- list( list( from = "x1", to = "x2", new_line_type = "dotted"), list( from = "x1", to = "x4", new_line_type = "dashed") ) p_pa2l <- set_edge_line_type( p_pa, my_line_type_list ) plot(p_pa2l)
Set the line widths of selected edges.
set_edge_line_width( semPaths_plot, line_width_list = NULL, how = c("ratio", "value"), check_direction = TRUE )set_edge_line_width( semPaths_plot, line_width_list = NULL, how = c("ratio", "value"), check_direction = TRUE )
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
line_width_list |
A named vector or a list of named list. See the Details section on how to set this argument. |
how |
How the width will be changed.
If |
check_direction |
If |
Modified a qgraph::qgraph object generated by semPlot::semPaths and change the widths of selected edges.
line_width_list
This argument can be set in three ways.
For a named vector, the name of an
element should be the path as
specified by lavaan::model.syntax
or as appeared in
lavaan::parameterEstimates().
For example, to change the line width of the
path regressing y on x, the name
should be "y ~ x". To change the
line type of the covariance between x1
and x2, the name should be "x1 ~~ x2". Therefore, c("y ~ x1" = 2, "x1 ~~ x2" = 3) changes the
line widths of the path from x1 to y
and the covariance between x1 and
x2by 2 and 3,
respectively.
The order of the two nodes may matter for covariances. Therefore, if the attribute of a covariance is not changed, try switching the order of the two nodes.
For a list of named lists, each named
list should have three named values:
from, to, and new_line_width. The
attribute of the edge from from to
to will be set to new_line_width.
The second approach is no longer recommended, though kept for backward compatibility.
The third approach is one single unnamed value. This value will be applied to all edges.
A qgraph::qgraph based on
the original one, with widths for
selected edges changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_line_width_vector <- c( "x2 ~~ x1" = 2, "x4 ~ x1" = 3 ) p_pa2v <- set_edge_line_width( p_pa, my_line_width_vector) plot(p_pa2v) my_line_width_list <- list( list( from = "x1", to = "x2", new_line_width = 2), list( from = "x1", to = "x4", new_line_width = 3) ) p_pa2l <- set_edge_line_width( p_pa, my_line_width_list ) plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_line_width_vector <- c( "x2 ~~ x1" = 2, "x4 ~ x1" = 3 ) p_pa2v <- set_edge_line_width( p_pa, my_line_width_vector) plot(p_pa2v) my_line_width_list <- list( list( from = "x1", to = "x2", new_line_width = 2), list( from = "x1", to = "x4", new_line_width = 3) ) p_pa2l <- set_edge_line_width( p_pa, my_line_width_list ) plot(p_pa2l)
Set some attributes of a plot.
set_graph_margins( semPaths_plot, bottom, left, top, right, how = c("ratio", "value") ) node_labels_equal_scale(semPaths_plot, equal_scale) set_node_labels_equal_scale(semPaths_plot)set_graph_margins( semPaths_plot, bottom, left, top, right, how = c("ratio", "value") ) node_labels_equal_scale(semPaths_plot, equal_scale) set_node_labels_equal_scale(semPaths_plot)
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
bottom, left, right, top
|
The values
for the corresponding sides. Please
refer to 'Details' on how these values
are used. Their default values depend
on |
how |
How the width will be changed.
If |
equal_scale |
Logical. Set the value
|
Functions that modify a qgraph::qgraph object generated by semPlot::semPaths and change the widths of selected attributes.
For numerical attributes, there are two possible modes.
If how = "ratio", then the new value
is equal to the old value multiplied
by the user value. For example, if
the original margins are c(2, 3, 4, 5)
and the user value is 2, then the new
margins are c(4, 6, 8, 10). If the
user values are c(3, 1, 1, 1), then
the new margins are c(6, 3, 4, 5).
If how = "value", then the new value
is equal to the user value. For example, if
the original margins are c(2, 3, 4, 5)
and the user value is 2, then the new
margins are c(2, 2, 2, 2). If
the user values are c(1, 3, 5, 7),
then the new margins are c(1, 3, 5, 7).
The function set_graph_margins() is
for modifying the margins of a generated
plot. It can be useful for increasing
or decreasing one or more margins of
a plot without regenerating it.
The function node_labels_equal_scale() is
for setting the value label.scale.equal
of a plot. By default, label.scale.equal
may be FALSE, and the labels of nodes
may be of different font sizes.
Use this function, with equal_scale
set to TRUE, will make all node labels
have the same font size.
The function set_node_labels_equal_scale()
is just a wrapper of node_labels_equal_scale()
with equal_scale set to TRUE.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) # ==== set_graph_margins ==== p2 <- set_graph_margins(p_pa, bottom = 2) plot(p2) p3 <- set_graph_margins(p_pa, bottom = 2, how = "value") plot(p3) # ==== node_labels_equal_scale ==== dat <- pa_example colnames(dat)[1] <- "Mediator" mod_pa2 <- 'x1 ~~ x2 Mediator ~ x1 + x2 x4 ~ x1 + Mediator ' fit_pa2 <- lavaan::sem(mod_pa2, dat) m2 <- matrix(c("x1", NA, NA, NA, "Mediator", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa2 <- semPlot::semPaths(fit_pa2, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, sizeMan = 10, layout = m2) p4 <- node_labels_equal_scale(p_pa2, TRUE) plot(p4) p5 <- set_node_labels_equal_scale(p_pa2) plot(p5)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) # ==== set_graph_margins ==== p2 <- set_graph_margins(p_pa, bottom = 2) plot(p2) p3 <- set_graph_margins(p_pa, bottom = 2, how = "value") plot(p3) # ==== node_labels_equal_scale ==== dat <- pa_example colnames(dat)[1] <- "Mediator" mod_pa2 <- 'x1 ~~ x2 Mediator ~ x1 + x2 x4 ~ x1 + Mediator ' fit_pa2 <- lavaan::sem(mod_pa2, dat) m2 <- matrix(c("x1", NA, NA, NA, "Mediator", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa2 <- semPlot::semPaths(fit_pa2, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, sizeMan = 10, layout = m2) p4 <- node_labels_equal_scale(p_pa2, TRUE) plot(p4) p5 <- set_node_labels_equal_scale(p_pa2) plot(p5)
Set arbitrary attributes of selected nodes
set_node_attribute( semPaths_plot, values = NULL, attribute_name = NULL, how = c("value", "ratio"), check_nodes = TRUE )set_node_attribute( semPaths_plot, values = NULL, attribute_name = NULL, how = c("value", "ratio"), check_nodes = TRUE )
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
values |
A named vector or a list of named list. See the Details section on how to set this argument. |
attribute_name |
The name of the attribute to be changed. |
how |
How the width will be changed.
If |
check_nodes |
Logical. If |
Modify a qgraph::qgraph object generated by semPlot::semPaths and change the selected attributes of selected nodes.
This function is designed to be a general one that changes the attributes named by the user. The user needs to make sure that the attribute actually exists, and the values are valid for the named attribute.
values
This argument can be set in three ways.
For a named vector, the name of an element should be the nodes for which their attributes are to be changed. The names need to the displayed names if plotted, which may be different from the names in mode.
For example, if the attributes to be
changed are the colors of selected
nodes, to change the color of x
is to be changed, the name
should be "x". Therefore,
c("y" = "red", "x" = "red") changes
the colors of the nodes y and x
to "red" and "blue",
respectively.
For a list of named lists, each named
list should have two named values:
node and new_value. The
attribute of node
will be set to new_value.
The second approach is no longer recommended, though kept for backward compatibility.
The last approach is setting values
to a one-element vector with no name.
All nodes in plot will then have the
selected attributes set to this value.
A qgraph::qgraph based on
the original one, with the selected
attributes of selected nodes changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_color_vector <- c(x3 = "red", x4 = "blue") p_pa2v <- set_node_attribute(p_pa, my_color_vector, attribute_name = "color") plot(p_pa2v) my_color_list <- list(list(node = "x3", new_value = "green"), list(node = "x4", new_value = "red")) p_pa2l <- set_node_attribute(p_pa, my_color_list, attribute_name = "color") plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) lavaan::parameterEstimates(fit_pa)[, c("lhs", "op", "rhs", "est", "pvalue")] m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_color_vector <- c(x3 = "red", x4 = "blue") p_pa2v <- set_node_attribute(p_pa, my_color_vector, attribute_name = "color") plot(p_pa2v) my_color_list <- list(list(node = "x3", new_value = "green"), list(node = "x4", new_value = "red")) p_pa2l <- set_node_attribute(p_pa, my_color_list, attribute_name = "color") plot(p_pa2l)
Set the attributes of the borders of selected nodes.
set_node_border_color(semPaths_plot, border_color = NULL, check_nodes = TRUE) set_node_border_width( semPaths_plot, border_width = NULL, how = c("ratio", "value"), check_nodes = TRUE )set_node_border_color(semPaths_plot, border_color = NULL, check_nodes = TRUE) set_node_border_width( semPaths_plot, border_width = NULL, how = c("ratio", "value"), check_nodes = TRUE )
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
border_color |
A named vector or a list of named list. See the Details section on how to set this argument. |
check_nodes |
Logical. If |
border_width |
A named vector or a list of named list. See the Details section on how to set this argument. |
how |
How the width will be changed.
If |
Modify a qgraph::qgraph object generated by semPlot::semPaths and change the attributes of the borders of selected nodes.
border_color
and border_width
This argument can be set in three ways.
For a named vector, the name of an element should be the nodes for which the attributes are to be changed. The names need to the displayed names if plotted, which may be different from the names in the model.
For example, to change the color of
x, the name should be "x".
Therefore, c("y" = "red", "x" = "blue") changes the colors of the
nodes y and x to "red" and
"blue", respectively.
For a list of named lists, each named
list should have two named values:
node, plus new_border_color or
new_border_width.
The second approach is no longer recommended, though kept for backward compatibility.
The last approach is setting
border_color or
border_width to a one-element
vector with no name. All nodes in
plot will then have the corresponding
attributes set to this value.
There are also two modes for changing
border width. If how = "ratio",
then the new border width is equal to
the original border width multiplied
by the supplied value. For example,
if the supplied value is 2, and the
original border width is 1.5, the new
border width is 3.
If how = "value", then the new
border width is set to the user
supplied value. For example, if the
supplied value is 2, then the new
border width is 2, regardless of the
original border width.
A qgraph::qgraph based on
the original one, with the corresponding
attribute of
of selected nodes changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_border_color_vector <- c(x3 = "red", x4 = "blue") p_pa2v <- set_node_border_color(p_pa, my_border_color_vector) plot(p_pa2v) p_pa2l <- set_node_border_color(p_pa, "green") plot(p_pa2l) my_border_width_vector <- c(x3 = 2, x4 = 3) p_pa3v <- set_node_border_width(p_pa, my_border_width_vector) plot(p_pa3v) p_pa3l <- set_node_border_width(p_pa, 3) plot(p_pa3l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_border_color_vector <- c(x3 = "red", x4 = "blue") p_pa2v <- set_node_border_color(p_pa, my_border_color_vector) plot(p_pa2v) p_pa2l <- set_node_border_color(p_pa, "green") plot(p_pa2l) my_border_width_vector <- c(x3 = 2, x4 = 3) p_pa3v <- set_node_border_width(p_pa, my_border_width_vector) plot(p_pa3v) p_pa3l <- set_node_border_width(p_pa, 3) plot(p_pa3l)
Set the colors of selected nodes.
set_node_color(semPaths_plot, colors = NULL, check_nodes = TRUE)set_node_color(semPaths_plot, colors = NULL, check_nodes = TRUE)
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
colors |
A named vector or a list of named list. See the Details section on how to set this argument. |
check_nodes |
Logical. If |
Modify a qgraph::qgraph object generated by semPlot::semPaths and change the colors of selected nodes.
color_list
This argument can be set in three ways.
For a named vector, the name of an element should be the nodes for which the colors are to be changed. The names need to the displayed names if plotted, which may be different from the names in the model.
For example, to change the color of x,
the name
should be "x". Therefore,
c("y" = "red", "x" = "red") changes
the colors of the nodes y and x
to "red" and "blue",
respectively.
For a list of named lists, each named
list should have two named values:
node and new_color. The
color of node
will be set to new_color.
The second approach is no longer recommended, though kept for backward compatibility.
The last approach is setting color_list
to a one-element vector with no name.
All nodes in plot will then have the
colors set to this value.
A qgraph::qgraph based on
the original one, with the colors
of selected nodes changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_color_vector <- c(x3 = "red", x4 = "blue") p_pa2v <- set_node_color(p_pa, my_color_vector) plot(p_pa2v) p_pa2l <- set_node_color(p_pa, "green") plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) my_color_vector <- c(x3 = "red", x4 = "blue") p_pa2v <- set_node_color(p_pa, my_color_vector) plot(p_pa2v) p_pa2l <- set_node_color(p_pa, "green") plot(p_pa2l)
Set the attributes of labels of selected nodes.
set_node_label_size(semPaths_plot, values = NULL) set_node_label_color(semPaths_plot, values = NULL)set_node_label_size(semPaths_plot, values = NULL) set_node_label_color(semPaths_plot, values = NULL)
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
values |
A named vector or a list of named list. See the Details section on how to set this argument. |
Modify a qgraph::qgraph object generated by semPlot::semPaths and change the label attributes of selected nodes.
The function set_node_label_size()
set the attribute label.cex of
the plot, corresponding to the argument
label.cex of semPlot::semPaths().
It controls the size of the label of
a node.
The function set_node_label_color()
set the attribute label.color of
the plot, corresponding to the argument
label.color of semPlot::semPaths().
It controls the color of the label of
a node.
These arguments can be set in three ways.
For a named vector, the name of an element should be the nodes for which the attributes are to be changed. The names need to the displayed names if plotted, which may be different from the names in the model.
For example, to change the label-related attribute of x,
the name
should be "x". Therefore,
c("y" = 2, "x" = 3) changes
the selected attributes of the nodes y and x
to 2 and 3,
respectively.
For a list of named lists, each named
list should have two named values:
node and new_value. The
size-related attribute of node
will changed based on new_value.
The second approach is no longer recommended, though kept for backward compatibility.
The last approach is setting the size-related attribute to a one-element vector with no name. All nodes in plot will then have their sizes changed based on this value.
A qgraph::qgraph based on
the original one, with the sizes
of selected nodes changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2v <- set_node_label_size(p_pa, c(x1 = 5, x2 = 10)) plot(p_pa2v) p_pa2l <- set_node_label_color(p_pa, "red") plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2v <- set_node_label_size(p_pa, c(x1 = 5, x2 = 10)) plot(p_pa2v) p_pa2l <- set_node_label_color(p_pa, "red") plot(p_pa2l)
Set the sizes of selected nodes.
set_node_size( semPaths_plot, values = NULL, how = c("ratio", "value"), check_nodes = TRUE ) set_node_width( semPaths_plot, values = NULL, how = c("ratio", "value"), check_nodes = TRUE ) set_node_height( semPaths_plot, values = NULL, how = c("ratio", "value"), check_nodes = TRUE ) set_node_shape(semPaths_plot, values = NULL, check_nodes = TRUE)set_node_size( semPaths_plot, values = NULL, how = c("ratio", "value"), check_nodes = TRUE ) set_node_width( semPaths_plot, values = NULL, how = c("ratio", "value"), check_nodes = TRUE ) set_node_height( semPaths_plot, values = NULL, how = c("ratio", "value"), check_nodes = TRUE ) set_node_shape(semPaths_plot, values = NULL, check_nodes = TRUE)
semPaths_plot |
A qgraph::qgraph object generated by semPlot::semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
values |
A named vector or a list of named list. See the Details section on how to set this argument. |
how |
How the width will be changed.
If |
check_nodes |
Logical. If |
Modify a qgraph::qgraph object generated by semPlot::semPaths and change the sizes of selected nodes.
These arguments can be set in three ways.
For a named vector, the name of an element should be the nodes for which the attributes are to be changed. The names need to the displayed names if plotted, which may be different from the names in the model.
For example, to change the size-related attribute of x,
the name
should be "x". Therefore,
c("y" = 2, "x" = 3) changes
the selected attributes of the nodes y and x
to 2 and 3,
respectively.
For a list of named lists, each named
list should have two named values:
node and new_value. The
size-related attribute of node
will changed based on new_value.
The second approach is no longer recommended, though kept for backward compatibility.
The last approach is setting the size-related attribute to a one-element vector with no name. All nodes in plot will then have their sizes changed based on this value.
How width and height are used
depends on shape. The supported
values of shape depends on
qgraph. Common values are
are "square", "rectangle",
"circle", "rectangle", and "ellipse". For
"square" and "circle", only the
value of width is used to determine
the size of the node.
There are also two modes for changing
an attribute. If how = "ratio",
then the new value, such as size,
is equal to
the original value multiplied
by the supplied value. For example,
if the supplied value is 2, and the
original size is 5, the new
size is 10.
If how = "value", then the new
value is set to the user
supplied value. For example, if the
supplied value is 15, then the new
value is 15, regardless of the
original value.
A qgraph::qgraph based on
the original one, with the sizes
of selected nodes changed.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2v <- set_node_size( p_pa, c(x1 = 5, x2 = 10), how = "value" ) plot(p_pa2v) p_pa2v2 <- set_node_size( p_pa, c(x1 = 0.5, x2 = 2), how = "ratio" ) plot(p_pa2v2) p_pa2l <- set_node_shape(p_pa, "circle") plot(p_pa2l)mod_pa <- 'x1 ~~ x2 x3 ~ x1 + x2 x4 ~ x1 + x3 ' fit_pa <- lavaan::sem(mod_pa, pa_example) m <- matrix(c("x1", NA, NA, NA, "x3", "x4", "x2", NA, NA), byrow = TRUE, 3, 3) p_pa <- semPlot::semPaths(fit_pa, whatLabels="est", style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m) p_pa2v <- set_node_size( p_pa, c(x1 = 5, x2 = 10), how = "value" ) plot(p_pa2v) p_pa2v2 <- set_node_size( p_pa, c(x1 = 0.5, x2 = 2), how = "ratio" ) plot(p_pa2v2) p_pa2l <- set_node_shape(p_pa, "circle") plot(p_pa2l)
Configure the layout of factors and adjust other aspects of an SEM graph by semPlot::semPaths.
set_sem_layout( semPaths_plot, indicator_order = NULL, indicator_factor = NULL, factor_layout = NULL, factor_point_to = NULL, indicator_push = NULL, indicator_spread = NULL, loading_position = 0.5 )set_sem_layout( semPaths_plot, indicator_order = NULL, indicator_factor = NULL, factor_layout = NULL, factor_point_to = NULL, indicator_push = NULL, indicator_spread = NULL, loading_position = 0.5 )
semPaths_plot |
A qgraph::qgraph object generated by semPaths, or a similar qgraph object modified by other semptools functions. It can also be a list of qgraph::qgraph objects, probably though not necessarily from a multigroup model. If it is a list of qgraph::qgraph objects, then the function will be applied to all the objects. |
indicator_order |
A string vector of the indicators. The order
of the names is the order of the indicators in the graph, when they
are drawn on the bottom of the graph. The indicators should be
grouped by the factors on which they load on. For example, if x1,
x2, x4 load on f2, and x3, x5, x6 load on f1, then vector should be
either c("x1", "x2", "x4", "x3", "x5", "x6") or c("x3", "x5", "x6",
"x1", "x2", "x4"). Indicators within a group can be ordered in any
way. If it is a named vector, its names will be used for the
argument |
indicator_factor |
A string vector of the same length of the
indicator order, storing the name of the factor for which each of
the indicator in indicator_factor loads on. For example, if x1, x2,
x4 load on f2, and x3, x5, x6 load on f1, and indicator_order is
c("x3", "x5", "x6", "x1", "x2", "x4"), then indicator_factor should
be c("f2", "f2", "f2", "f1", "f1", "f1"). If |
factor_layout |
A matrix of arbitrary size. This matrix will serve as a grid for users to specify where each latent factor should be placed approximately on the graph. Each cell should contain NA or the name of a latent factor. The locations of all latent factors must be explicitly specified by this matrix. |
factor_point_to |
Can be a named character vector with
names being the names of factors, or a matrix of the same size as |
indicator_push |
(Optional) This argument is used to adjust the
positions of the indicators of selected latent factors. It can be
named vector or a list of named lists. For a named vector, The name
is the factor of which the indicators will be "pushed", and the
value is how "hard" the push is: the multiplier to the distance
from the factor to the indicators. If this value is 1, then there
is no change. If this value is greater than 1, then the indicators
are pushed away from the latent factor. If this value is less than
1, then the indicators are pulled toward the latent factor. For
example, to push the indicators of |
indicator_spread |
(Optional) This argument is used to adjust
the distance between indicators of selected latent factors. It can
be a named vector or a list of named lists. For a named vector, the
name is the factor of which the indicators will be spread out. The
value is the multiplier to the distance between neighboring
indicators. If this value is equal to 1, there is no change. Larger
than one, the indicators will be "spread" away from each other.
Less than one, the indicators will be placed closer to each others.
For example, to spread the indicators of |
loading_position |
(Optional) Default is .5. This is used
adjust the position of the loadings. If this is one single number,
it will be used to set the positions of all loadings. If it is .5,
the loadings are placed on the center of the arrows. Larger the
number, closer the loadings to the indicators. Smaller the number,
closer to the latent factors. This argument also accepts a named
vector or a list of named lists, allowing users to specify the
positions of loadings for each factor separately. For a named
vector, in each element, the name is the factor whose loadings will
be moved. The value is the positions of its loadings. The default
is .50. We only need to specify the positions for factors to be
changed from .50 to other values. For example, move the loadings of
|
Modify a qgraph::qgraph object generated by semPaths
based on an SEM model with latent factors. Since version 0.2.9.5,
this function natively supports observed exogenous variable.
If a variable is listed in both indicator_order and
indicator_factor, as if it is both a factor and an indicator,
this function will assume that it is an observed exogenous variable.
It will be positioned as a factor according to factor_layout,
but no indicators will be drawn.
For versions older than 0.2.9.5, an observed exogenous variable needs to be specified as an one-indicator factor in the model specification for this function to work.
A qgraph::qgraph based on the original one, with various
aspects of the model modified.
If semPaths_plot is a list of
qgraph::qgraph objects, then a
list of processed qgraph::qgraph objects
will be returned.
library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' fit_sem <- lavaan::sem(mod, sem_example) lavaan::parameterEstimates(fit_sem)[, c("lhs", "op", "rhs", "est", "pvalue")] p <- semPaths(fit_sem, whatLabels="est", sizeMan = 5, nCharNodes = 0, nCharEdges = 0, edge.width = 0.8, node.width = 0.7, edge.label.cex = 0.6, style = "ram", mar = c(10,10,10,10)) indicator_order <- c("x04", "x05", "x06", "x07", "x01", "x02", "x03", "x11", "x12", "x13", "x14", "x08", "x09", "x10") indicator_factor <- c( "f2", "f2", "f2", "f2", "f1", "f1", "f1", "f4", "f4", "f4", "f4", "f3", "f3", "f3") factor_layout <- matrix(c("f1", NA, NA, NA, "f3", "f4", "f2", NA, NA), byrow = TRUE, 3, 3) factor_point_to <- matrix(c("left", NA, NA, NA, "down", "down", "left", NA, NA), byrow = TRUE, 3, 3) indicator_push <- c(f3 = 2, f4 = 1.5) indicator_spread <- c(f1 = 2, f2 = 2) loading_position <- c(f1 = .5, f2 = .8, f3 = .8) # Pipe operator can be used if desired p2 <- set_sem_layout(p, indicator_order = indicator_order, indicator_factor = indicator_factor, factor_layout = factor_layout, factor_point_to = factor_point_to, indicator_push = indicator_push, indicator_spread = indicator_spread, loading_position = loading_position) p2 <- set_curve(p2, c("f2 ~ f1" = -1, "f4 ~ f1" = 1.5)) p2 <- mark_sig(p2, fit_sem) p2 <- mark_se(p2, fit_sem, sep = "\n") plot(p2) # Use a named vector for indicator_order indicator_order2 <- c(f2 = "x04", f2 = "x05", f2 = "x06", f2 = "x07", f1 = "x01", f1 = "x02", f1 = "x03", f4 = "x11", f4 = "x12", f4 = "x13", f4 = "x14", f3 = "x08", f3 = "x09", f3 = "x10") p2 <- set_sem_layout(p, indicator_order = indicator_order2, factor_layout = factor_layout, factor_point_to = factor_point_to, indicator_push = indicator_push, indicator_spread = indicator_spread, loading_position = loading_position) plot(p2) # Use automatically generated indicator_order and indicator_factor p2 <- set_sem_layout(p, factor_layout = factor_layout, factor_point_to = factor_point_to, indicator_push = indicator_push, indicator_spread = indicator_spread, loading_position = loading_position) plot(p2) # Use named character vector for factor_point_to directions <- c(f1 = "left", f2 = "left", f3 = "down", f4 = "down") p2v2 <- set_sem_layout(p, indicator_order = indicator_order, indicator_factor = indicator_factor, factor_layout = factor_layout, factor_point_to = directions, indicator_push = indicator_push, indicator_spread = indicator_spread, loading_position = loading_position) p2v2 <- set_curve(p2v2, c("f2 ~ f1" = -1, "f4 ~ f1" = 1.5)) p2v2 <- mark_sig(p2v2, fit_sem) p2v2 <- mark_se(p2v2, fit_sem, sep = "\n") plot(p2v2) #Lists of named list which are equivalent to the vectors above: #indicator_push <- list(list(node = "f3", push = 2), # list(node = "f4", push = 1.5)) #indicator_spread <- list(list(node = "f1", spread = 2), # list(node = "f2", spread = 2)) #loading_position <- list(list(node = "f1", position = .5), # list(node = "f2", position = .8), # list(node = "f3", position = .8))library(lavaan) library(semPlot) mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ f1 + f2 f4 ~ f1 + f3 ' fit_sem <- lavaan::sem(mod, sem_example) lavaan::parameterEstimates(fit_sem)[, c("lhs", "op", "rhs", "est", "pvalue")] p <- semPaths(fit_sem, whatLabels="est", sizeMan = 5, nCharNodes = 0, nCharEdges = 0, edge.width = 0.8, node.width = 0.7, edge.label.cex = 0.6, style = "ram", mar = c(10,10,10,10)) indicator_order <- c("x04", "x05", "x06", "x07", "x01", "x02", "x03", "x11", "x12", "x13", "x14", "x08", "x09", "x10") indicator_factor <- c( "f2", "f2", "f2", "f2", "f1", "f1", "f1", "f4", "f4", "f4", "f4", "f3", "f3", "f3") factor_layout <- matrix(c("f1", NA, NA, NA, "f3", "f4", "f2", NA, NA), byrow = TRUE, 3, 3) factor_point_to <- matrix(c("left", NA, NA, NA, "down", "down", "left", NA, NA), byrow = TRUE, 3, 3) indicator_push <- c(f3 = 2, f4 = 1.5) indicator_spread <- c(f1 = 2, f2 = 2) loading_position <- c(f1 = .5, f2 = .8, f3 = .8) # Pipe operator can be used if desired p2 <- set_sem_layout(p, indicator_order = indicator_order, indicator_factor = indicator_factor, factor_layout = factor_layout, factor_point_to = factor_point_to, indicator_push = indicator_push, indicator_spread = indicator_spread, loading_position = loading_position) p2 <- set_curve(p2, c("f2 ~ f1" = -1, "f4 ~ f1" = 1.5)) p2 <- mark_sig(p2, fit_sem) p2 <- mark_se(p2, fit_sem, sep = "\n") plot(p2) # Use a named vector for indicator_order indicator_order2 <- c(f2 = "x04", f2 = "x05", f2 = "x06", f2 = "x07", f1 = "x01", f1 = "x02", f1 = "x03", f4 = "x11", f4 = "x12", f4 = "x13", f4 = "x14", f3 = "x08", f3 = "x09", f3 = "x10") p2 <- set_sem_layout(p, indicator_order = indicator_order2, factor_layout = factor_layout, factor_point_to = factor_point_to, indicator_push = indicator_push, indicator_spread = indicator_spread, loading_position = loading_position) plot(p2) # Use automatically generated indicator_order and indicator_factor p2 <- set_sem_layout(p, factor_layout = factor_layout, factor_point_to = factor_point_to, indicator_push = indicator_push, indicator_spread = indicator_spread, loading_position = loading_position) plot(p2) # Use named character vector for factor_point_to directions <- c(f1 = "left", f2 = "left", f3 = "down", f4 = "down") p2v2 <- set_sem_layout(p, indicator_order = indicator_order, indicator_factor = indicator_factor, factor_layout = factor_layout, factor_point_to = directions, indicator_push = indicator_push, indicator_spread = indicator_spread, loading_position = loading_position) p2v2 <- set_curve(p2v2, c("f2 ~ f1" = -1, "f4 ~ f1" = 1.5)) p2v2 <- mark_sig(p2v2, fit_sem) p2v2 <- mark_se(p2v2, fit_sem, sep = "\n") plot(p2v2) #Lists of named list which are equivalent to the vectors above: #indicator_push <- list(list(node = "f3", push = 2), # list(node = "f4", push = 1.5)) #indicator_spread <- list(list(node = "f1", spread = 2), # list(node = "f2", spread = 2)) #loading_position <- list(list(node = "f1", position = .5), # list(node = "f2", position = .8), # list(node = "f3", position = .8))
Convert a named vector to a list of lists, to be used
by various functions in semptools.
to_list_of_lists(input, name1 = NULL, name2 = NULL, name3 = NULL)to_list_of_lists(input, name1 = NULL, name2 = NULL, name3 = NULL)
input |
A named vector |
name1 |
The name for the first element in the list-in-list.
Default is |
name2 |
The name for the second element in the list-in-list.
Defaultis |
name3 |
The name for the third element in the list-in-list.
Default is |
This function is not to be used by users, but to be used
internally by other functions of semptools.
A list of lists.
x <- c("x1 ~~ x2" = -1, "x4 ~ x1" = 1) to_list_of_lists(x, name1 = "from", name2 = "to", name3 = "new_curve") #list(list(from = "x1", to = "x2", new_curve = -1), # list(from = "x1", to = "x4", new_curve = 1)) y <- c(x1 = 0, x2 = 180, x3 = 140, x4 = 140) to_list_of_lists(y, name1 = "node", name2 = "rotate") #list(list(node = "x1", rotate = 0), # list(node = "x2", rotate = 180), # list(node = "x3", rotate = 140), # list(node = "x4", rotate = 140))x <- c("x1 ~~ x2" = -1, "x4 ~ x1" = 1) to_list_of_lists(x, name1 = "from", name2 = "to", name3 = "new_curve") #list(list(from = "x1", to = "x2", new_curve = -1), # list(from = "x1", to = "x4", new_curve = 1)) y <- c(x1 = 0, x2 = 180, x3 = 140, x4 = 140) to_list_of_lists(y, name1 = "node", name2 = "rotate") #list(list(node = "x1", rotate = 0), # list(node = "x2", rotate = 180), # list(node = "x3", rotate = 140), # list(node = "x4", rotate = 140))