One useful feature of
semPlot::semPaths() is using setting the argument
layout to a layout matrix to specify the location of each node in a structural equation model. This guide briefly explains how to use
layout, and then introduces the helper function
semptools that can be used to construct a layout matrix. Last, it describes how
factor_layout is used in
set_sem_layout() to specify the layout of the latent factors only, and let
decide the positions of their indicators.
Suppose we have a path model with four variables,
x4. In psychology, this model is usually presented this way:
If we fit the model by
lavaan::lavaan() and use
semPlot::semPaths() to generate the plot without using
layout, this is the resulting plot:
This layout is different from the convention used in psychology.
layout, we first decide the grid to be used to position the variable (nodes). For example, for the conceptual diagram, we can try a 3 by 4 grid
The pair of numbers in each cell denote the location of the cell in a 3 by 4 matrix as indexed in an R matrix. An empty column was added between
x4 because we want to move
x4 further away to the right.
We then create a matrix of the same dimension as the grid, and initialize the cells by
NA, which denoted a cell with nothing.
We then set the position of each variable (node, as called internally in a plot by
semPlot::semPaths()) by setting the corresponding cell to the name of this variable as appeared in the
We can then set
layout to this matrix to tell
semPlot::semPaths() how to position the four variables:
Alternatively, we can type the matrix as it would appear if printed, and set
byrow = TRUE:
We need to type more because we need to include
NA for all the empty cells. However, this approach let us see immediately how the variables will be positioned. We just place the variables in the target cell, without knowing the coordinates. This is a WYSIWYG (what-you-see-is-what-you-get) approach. This is the approach used in the Quick Start Guides.
The WYSIWYG approach in the previous section has one drawback: It is not easy to change the position of the variables and the dimension of the grid. In real research, trial-and-error is usually needed to find a desirable layout. For example, if we want to add a column or row, we need ty type several
NAs to crate it.
layout_matrix() function in
semptools is designed to generate the matrix using the coordinates of the variables. Instead of specifying the dimension ourselves,
layout_matrix() will try to figure out the dimension based on the coordinates automatically.
For example, to generate the same layout above, we can do this:
m2 <- layout_matrix(x1 = c(1, 1), x2 = c(3, 1), x3 = c(2, 2), x4 = c(2, 4)) m2 #> [,1] [,2] [,3] [,4] #> [1,] "x1" NA NA NA #> [2,] NA "x3" NA "x4" #> [3,] "x2" NA NA NA p_pa <- semPaths(fit_pa, whatLabels = "est", sizeMan = 10, edge.label.cex = 1.15, style = "ram", nCharNodes = 0, nCharEdges = 0, layout = m2)
Suppose we want to move
x4 closer to
x3. Instead of deleting the 3rd columns of
NA, we can
just change the coordinates of
The best approach to specify the layout depends on the situation at hand. During the trial-and-error phrase, using
layout_matrix() is good for changing the layout. When the layout has been finalized, for readability, typing the matrix row-by-row may be better (although we can still use
layout_matrix() to form the matrix and then print the matrix, as we did above).
If we use
semPlot::semPaths() for a structural models with latent factors and we want to draw both the factors and their indicators, we need to specify the positions of all nodes, that is, all the indicators and all the factors. The grid will be very large and it is difficult to determine the positions of the indicators.
factor_layout argument in
set_sem_layout() is developed to solve this problem. It works like
set_sem_layout(), we only need to specify the positions of the latent factors. As in
layout, we create a matrix to specify the positions of the factors. The procedure is identical to what illustrated above for a path model, and all the approaches presented above can be used. The only difference is, only the names of the latent factors need to be present in the matrix, and the size of the grid only need to consider the factors.
For example, suppose we have 14 indicators and four factors, and this is the model:
If we want to draw both the factors and the indicators, the plot will have 18 nodes:
Suppose we want to position the factors this way:
Again, we decide the grid to use, which is 3 by 3 in this example:
Therefore, this is the matrix to be used:
layout_matrix() can also be used to set up the orientation of the indicators of each factor:
We can then use this matrix in
set_sem_layout() (please refer to
?set_sem_layout on how to specify other arguments):
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") indicator_push <- c(f3 = 2.5, f4 = 2.5, f1 = 1.5, f2 = 1.5) indicator_spread <- c(f1 = 2, f2 = 2, f3 = 2, f4 = 1.75) loading_position <- c(f2 = .6, f3 = .8, f4 = .8) p2 <- set_sem_layout(p, indicator_order = indicator_order, indicator_factor = indicator_factor, factor_layout = m_sem, factor_point_to = point_to, indicator_push = indicator_push, indicator_spread = indicator_spread, loading_position = loading_position) plot(p2)
This make it much easier to specify the positions of factors in a structural equation model with latent factors.