0

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

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

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

路径图

4

3 回答 3

0

在此处输入图像描述

由于您没有分享您的模型,我复制了一个虚拟模型。semPaths 似乎不允许我们调整 nodeNames,也许您可​​以将此图保存为对象并尝试使用“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)

输出

于 2021-08-13T23:06:12.797 回答
0

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

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

固定路径图

于 2021-08-14T00:29:47.020 回答
-1

你的页面不够大。

R中有两个图形系统,base和grid。一个semPaths使用的是基本包,它有点模仿你在纸上画画的方式:首先你设置大小,然后你画东西;你不能回去。另一个,网格,用于lattice保存ggplot2绘图,直到您调用它。网格图通常不会像基本图形那样超出页面,通常会缩放图以适应绘图区域。

这里基本上是你的问题,使用一个例子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 行空间,在右侧多出了 25 行空间。另外,请注意xpd = NA关闭剪裁,即在绘图区域之外打印的任何内容都将显示——这在基本图中也经常出现。

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

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)

在此处输入图像描述

于 2021-08-15T06:12:17.373 回答