1

I'm new to using semPlots but found a way to organize the variables the way I wanted using a custom layout sempathmatrix. The layout of the variables is good, may need some slight tweaking, but I want the vectors to be curved. Using the curve, curvature, or other curve related arguments doesn't do anything with the custom layout.

Any thoughts on how to get curved vectors in this semplot using a custom layout?

I've tried adding curve or curvature and curveAdjacent arguments but none do a darn thing with my sempathmatrix layout. If I switch back to tree or tree2, the curves show up. Can't seem to figure it out.

########Creating the Manual Lables and Layout Matrix for Cope Model Plot 

lbls<-c("Depression","Flourishing","Active\nCoping","Beh\nDisengage","Self\nCompassion","Self\nColdness")

sempathmatrix<-matrix(c(.5,.5, .5,-.5, 0,.4, 0,-.4, -.5,.5, -.5,-.5, .5,.5, .5,-.5), ncol=2,byrow=T)

####Path analysis Plot for Coping Model ######

semPaths(fit, "std", residuals = F, intercepts = F, layout = sempathmatrix, fade=F, rotation = 3, nCharNodes= 0, nodeLabels=lbls, edge.label.cex=0.7, freeStyle = T, title=F, sizeMan = 9, mar = c(5,5,5,5))

SemPlot:

enter image description here

user20650
  • 24,654
  • 5
  • 56
  • 91
Evan
  • 11
  • 1

1 Answers1

0

You can manipulate curves after creating and saving your semPaths-plot. If you save your plot as p, then you can access to vector controlling the curve with p$graphAttributes$Edges$curve. Then you can replace values in that vector with new vector consisting of values from 0 to 1 with level of curvature you wish. You can also find the position of edge in the vector by adding edgeLabels = 1:n, where n is number of edges, to your semPaths call. This should help to create vector to control the curvature in preferred manner.

Here is reproducible example of the process.

# Open the packages (install them first if you haven't yet)
library(lavaan)
library(semPlot)

# Generate data

df <- data.frame(replicate(3, rnorm(100, 0, 1)))

# Create SEM-model

model <- "
X1 ~ X2
X1 ~ X3
X2 ~ X3"

fit <- sem(model, df)

# Custom layout

x <- c(-1, -1, 1)
y <- c(-1, 1, 0)
m <- matrix(c(x,y), ncol = 2)

# Add identifier to edges to find out their position later

semPaths(fit, layout = m, edgeLabels = 1:6, edge.label.cex = 2)

# We can add curvature if we save the plot and manipulate metadata

p <- semPaths(fit, layout = m)

# This command yields access to control the curve
p$graphAttributes$Edges$curve

# By replacing this vector, we add curve to our plot
p$graphAttributes$Edges$curve <- c(0.9, 0.2, 0.5, 0, 0, 0)

# Then we can plot manipulated p with plot()-function and see the curvature
plot(p)