【问题标题】:Curved vector arrows with custom layout in semPlotsemPlot 中具有自定义布局的曲线矢量箭头
【发布时间】:2019-11-08 21:07:04
【问题描述】:

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

关于如何使用自定义布局在此 semplot 中获取曲线向量有什么想法吗?

我尝试添加 curvecurvaturecurveAdjacent 参数,但我的 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))

SemPlot:

【问题讨论】:

    标签: r semplot


    【解决方案1】:

    您可以在创建和保存您的 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)
    

    【讨论】:

      猜你喜欢
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 2018-11-18
      • 2021-10-10
      • 1970-01-01
      相关资源
      最近更新 更多