0

我是新手,semPlots但找到了一种使用自定义布局以我想要的方式组织变量的方法sempathmatrix。变量的布局很好,可能需要稍微调整一下,但我希望向量是弯曲的。使用曲线、曲率或其他与曲线相关的参数对自定义布局没有任何作用。

关于如何使用自定义布局在此 semplot 中获取弯曲向量的任何想法?

我尝试添加curveorcurvaturecurveAdjacent参数,但没有一个对我的sempathmatrix布局做任何事情。如果我切换回树或树 2,曲线就会出现。似乎无法弄清楚。

########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))

符号图:

在此处输入图像描述

4

1 回答 1

0

您可以在创建和保存 semPaths-plot 后操作曲线。如果将绘图另存为p,则可以使用 访问控制曲线的矢量 p$graphAttributes$Edges$curve。然后,您可以用新向量替换该向量中的值,该向量由 0 到 1 的值组成,具有您希望的曲率水平。您还可以通过将边数添加到 semPaths 调用中来edgeLabels = 1:n找到向量中边的位置。n这应该有助于创建矢量以以首选方式控制曲率。

这是该过程的可重现示例。

# 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)
于 2020-07-14T08:53:49.373 回答