R中的节点滑出路径图

Nodes sliding off path diagram in R

这是一个示例输出,因为数据集很大:

> dput(head(dat, n=20))
structure(list(q01 = c(2, 1, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 3, 
2, 2, 3, 1, 2, 2, 2), q02 = c(1, 1, 3, 1, 1, 1, 3, 2, 3, 4, 1, 
1, 1, 2, 2, 1, 2, 2, 3, 1), q03 = c(4, 4, 2, 1, 3, 3, 3, 3, 1, 
4, 5, 3, 3, 1, 3, 2, 5, 3, 4, 1), q04 = c(2, 3, 2, 4, 2, 2, 2, 
2, 4, 3, 2, 3, 4, 2, 4, 2, 2, 3, 2, 2), q05 = c(2, 2, 4, 3, 2, 
4, 2, 2, 5, 2, 2, 4, 3, 2, 2, 2, 1, 3, 3, 3), q06 = c(2, 2, 1, 
3, 3, 4, 2, 2, 3, 1, 1, 3, 2, 2, 2, 2, 1, 4, 1, 4), q07 = c(3, 
2, 2, 4, 3, 4, 2, 2, 5, 2, 2, 3, 3, 3, 3, 2, 1, 3, 1, 4), q08 = c(1, 
2, 2, 2, 2, 2, 2, 2, 5, 2, 2, 1, 3, 2, 2, 2, 1, 2, 1, 1), q09 = c(1, 
5, 2, 2, 4, 4, 3, 4, 3, 3, 5, 3, 2, 2, 2, 2, 4, 5, 5, 5), q10 = c(2, 
2, 2, 4, 2, 3, 2, 2, 3, 2, 2, 2, 3, 3, 3, 3, 1, 2, 2, 1), q11 = c(1, 
2, 3, 2, 2, 2, 2, 2, 5, 2, 1, 2, 3, 2, 2, 2, 1, 3, 1, 2), q12 = c(2, 
3, 3, 2, 3, 4, 2, 3, 5, 3, 3, 3, 4, 4, 3, 3, 2, 3, 3, 5), q13 = c(2, 
1, 2, 2, 3, 3, 2, 2, 5, 2, 1, 2, 4, 2, 2, 2, 1, 3, 1, 2), q14 = c(2, 
3, 4, 3, 2, 3, 2, 2, 5, 1, 2, 2, 4, 4, 3, 3, 1, 3, 2, 5), q15 = c(2, 
4, 2, 3, 2, 5, 2, 3, 5, 2, 1, 3, 4, 4, 3, 2, 1, 4, 2, 5), q16 = c(3, 
3, 3, 3, 2, 2, 2, 2, 5, 3, 2, 3, 4, 4, 4, 3, 2, 3, 3, 5), q17 = c(1, 
2, 2, 2, 2, 3, 2, 2, 5, 2, 2, 2, 3, 2, 2, 2, 2, 2, 1, 2), q18 = c(2, 
2, 3, 4, 3, 5, 2, 2, 5, 2, 2, 2, 3, 4, 3, 3, 1, 2, 1, 5), q19 = c(3, 
3, 1, 2, 3, 1, 3, 4, 2, 3, 5, 3, 2, 1, 3, 2, 4, 2, 4, 1), q20 = c(2, 
4, 4, 4, 4, 5, 2, 3, 5, 3, 3, 4, 4, 5, 4, 3, 2, 3, 2, 5), q21 = c(2, 
4, 3, 4, 2, 3, 2, 2, 5, 2, 2, 3, 4, 5, 4, 2, 1, 3, 2, 5), q22 = c(2, 
4, 2, 4, 4, 1, 4, 4, 3, 4, 5, 4, 3, 3, 4, 3, 4, 3, 4, 5), q23 = c(5, 
2, 2, 3, 4, 4, 4, 4, 3, 4, 5, 4, 4, 1, 4, 4, 4, 4, 4, 5)), variable.labels = c(q01 = "Statistics makes me cry", 
q02 = "My friends will think I'm stupid for not being able to cope with SPSS", 
q03 = "Standard deviations excite me", q04 = "I dream that Pearson is attacking me with correlation coefficients", 
q05 = "I don't understand statistics", q06 = "I have little experience of computers", 
q07 = "All computers hate me", q08 = "I have never been good at mathematics", 
q09 = "My friends are better at statistics than me", q10 = "Computers are useful only for playing games ", 
q11 = "I did badly at mathematics at school", q12 = "People try to tell you that SPSS makes statistics easier to understand but it doesn't", 
q13 = "I worry that I will cause irreparable damage because of my incompetenece with computers", 
q14 = "Computers have minds of their own and deliberately go wrong whenever I use them", 
q15 = "Computers are out to get me", q16 = "I weep openly at the mention of central tendency", 
q17 = "I slip into a coma whenever I see an equation", q18 = "SPSS always crashes when I try to use it", 
q19 = "Everybody looks at me when I use SPSS", q20 = "I can't sleep for thoughts of eigen vectors", 
q21 = "I wake up under my duvet thinking that I am trapped under a normal distribtion", 
q22 = "My friends are better at SPSS than I am", q23 = "If I'm good at statistics my friends will think I'm a nerd"
), codepage = 65001L, row.names = c(NA, 20L), class = "data.frame")

我主要复制了另一个 semPath 模型,但对其进行了编辑以适合我使用的数据集。首先是节点:

nodeNames <- c(
  "Statistics makes me cry.",
  "My friends think I'm stupid for not being able to cope with SPSS.",
  "Standard deviations excite me.",
  "I dream that Pearson is attacking me with correlation coefficients.",
  "I don't understand statistics.",
  "I have little experience with computers.",
  "All computers hate me.",
  "I've never been good at mathematics.",
  "SPSS Anxiety"
)

那么实际的semPath:

semPaths(onefac8items_a,
         what = "std", # this argument controls what the color of edges represent. In this case, standardized parameters
         whatLabels = "est", 
         style = "lisrel", 
         residScale = 8, 
         theme = "colorblind",
         manifests = paste0("q",1:8),
         nCharNodes = 0, 
         reorder = FALSE, 
         nodeNames = nodeNames, 
         legend.cex = 0.5, 
         rotation = 2, 
         layout = "tree2", 
         cardinal = "lat cov",  
         curvePivot = TRUE, 
         sizeMan = 4,
         sizeLat = 10, 
         mar = c(2,5,2,5.5), 
         filetype = "pdf", width = 8, height = 6, filename = "SPSS Anxiety" 
)

所以我真的只有一个问题。当我尝试 运行 我的路径图时,节点看起来像是从页面右侧滑出。我该如何解决?下面是我所指的图片:

因为你没有分享你的模型,所以我复制了一个虚拟模型。似乎 semPaths 不允许我们调整节点名称,也许您可​​以将此图另存为对象并尝试使用“plot()”函数重现以重新缩放,因为 semPaths 具有很多属性。

semPaths(fit,
     what = "std",
     style = "lisrel", 
     residScale = 8, 
     theme = "colorblind",
     nCharNodes = 4, 
     reorder = FALSE, 
     nodeNames = nodeNames, 
     legend.cex = 0.35, 
     rotation = 2, 
     layout = "tree2", 
     cardinal = "lat cov",  
     curvePivot = TRUE)

或者我们可以在 plotOptions:

中更改 GLRatio
a<-semPaths(onefac8items_a,
         what = "std",
         whatLabels = "est", 
         style = "lisrel", 
         residScale = 8, 
         theme = "colorblind",
         nCharNodes = 0, 
         reorder = FALSE, 
         nodeNames = nodeNames, 
         legend.cex = 0.5, 
         rotation = 2, 
         layout = "tree2", 
         cardinal = "lat cov",  
         curvePivot = TRUE, 
         sizeMan = 4,
         sizeLat = 10, 
         mar = c(2,5,2,5.5)
)

a$plotOptions$GLratio<-1  # you may need to play with this number
plot(a)

我最终只是在节点中缩短了我的问题,它解决了问题。我想您可以在图例中输入多少文本是有限制的:

nodeNames <- c(
  "Statistics makes me cry.",
  "Friends think I'm stupid because I cant do SPSS.",
  "Standard deviations excite me.",
  "I dream that Pearson is attacking me with correlations.",
  "I don't understand statistics.",
  "I have little experience with computers.",
  "All computers hate me.",
  "I've never been good at mathematics.",
  "SPSS Anxiety"
)

您的页面不够大。

R中有两种图形系统,base和grid。 semPaths 使用的是基本包,它模仿了您在纸上绘图的方式:首先设置大小,然后绘制内容;你不能回去。另一个是网格,用于 latticeggplot2 中,它会保存绘图直到您调用它。网格图通常不会像基本图形那样 运行 离开页面,通常会缩放这些图以适合绘图区域。

这基本上是你的问题,使用 lavaan::cfa

中的示例
library('lavaan')
library('semPlot')

nodeNames <- c(
  "Statistics makes me cry.",
  "My friends think I'm stupid for not being able to cope with SPSS.",
  "Standard deviations excite me.",
  "I dream that Pearson is attacking me with correlation coefficients.",
  "I don't understand statistics.",
  "I have little experience with computers.",
  "All computers hate me.",
  "I've never been good at mathematics.",
  "SPSS Anxiety"
)

?semPlot::semPaths
example(cfa)
semPaths(
  fit,
  what = "std", # this argument controls what the color of edges represent. In this case, standardized parameters
  whatLabels = "est", 
  style = "lisrel", 
  residScale = 8, 
  theme = "colorblind",
  # manifests = paste0("q",1:8),
  nCharNodes = 0, 
  reorder = FALSE, 
  nodeNames = nodeNames, 
  legend.cex = 0.5, 
  rotation = 2, 
  layout = "tree2", 
  cardinal = "lat cov",  
  curvePivot = TRUE, 
  sizeMan = 4,
  sizeLat = 10,
  mar = c(2,5,2,5.5),
  filetype = "pdf", width = 8, height = 6, filename = "SPSS-Anxiety"
)

我不确定 semPaths 在这里对大小做了什么,因为它肯定不会出现 8x6

$ identify -verbose SPSS-Anxiety.pdf | grep "Print size"
8:  Print size: 11.1944x6

我猜它弥补了额外的功能以适合所有东西,但它做得不是很好。

将基本地块保存到文件的典型方法是

pdf() ## or png() or jpg() etc
plotting code
dev.off() ## or graphics.off() to close everything not just the current device

为此,您需要从代码中删除 filetype 部分

pdf('SPSS-Anxiety-2.pdf', width = 8, height = 6)
par(oma = c(0, 2, 0, 25), xpd = NA)
semPaths(
  fit,
  what = "std", # this argument controls what the color of edges represent. In this case, standardized parameters
  whatLabels = "est", 
  style = "lisrel", 
  residScale = 8, 
  theme = "colorblind",
  # manifests = paste0("q",1:8),
  nCharNodes = 0,
  reorder = FALSE, 
  nodeNames = nodeNames, 
  legend.cex = 0.5, 
  rotation = 2, 
  layout = "tree2", 
  cardinal = "lat cov",  
  curvePivot = TRUE, 
  sizeMan = 4,
  sizeLat = 10,
  mar = c(2,5,2,5.5)
)
dev.off()

现在我得到了 8x6 的东西

$ identify -verbose SPSS-Anxiety-2.pdf | grep "Print size"
8:  Print size: 8x6

我增加了外边距的大小,oma?par,这让我在左边多了 2 行 space,在右边多了 25 行。另外,请注意 xpd = NA 关闭剪裁,即打印在绘图区域之外的任何内容都将显示——这在基本图中也经常出现。

但是对于某些文本来说,这是很多浪费 space。我会缩小它或将文本分成多行。您可以使用 strwrap 将白色 space 处的每个标签拆分为 <= 某个最大宽度:

par(oma = c(0, 0, 3, 0))
semPaths(
  fit,
  what = "std", # this argument controls what the color of edges represent. In this case, standardized parameters
  whatLabels = "est", 
  style = "lisrel", 
  residScale = 8, 
  theme = "colorblind",
  # manifests = paste0("q",1:8),
  nCharNodes = 0,
  reorder = FALSE, 
  nodeNames = sapply(nodeNames, function(x)
    paste(strwrap(x, 30), collapse = '\n      ')),
  legend.cex = 0.5, 
  rotation = 2, 
  layout = "tree2", 
  cardinal = "lat cov",  
  curvePivot = TRUE, 
  sizeMan = 4,
  sizeLat = 10,
  mar = c(2,5,2,5.5)
)
title('Anxiety and Depression SEM Path Diagram', outer = TRUE)