I'm running a confirmatory factor analysis using lavaan
package's cfa
function, and then plotting the model paths using semPaths
. I get as a result a large and wide image, which I'd like to save ideally as an svg image, in order to be able to edit it manually in a visual editor software afterwards.
The problem I run into is that it saves as an image the dimensions of the tiny plots window in my RStudio environment: in other words, it saves with all the labels squished and overlapping on top of each other. It seems to reduce the graphic itself but the labels and numbers are overlapping.
I can, of course, zoom my plot window and try to save it but it still saves as a squished image. I can do a screenshot but it is a very bad quality screenshot, which doesn't serve my purpose.
It's hard to give a reproducible example here because you need a lot of data to run my CFA model or a very simple model to run on a tiny example dataset but let's try with the following:
install.packages("lavaan") #CFA and SEM
library(lavaan)
install.packages("semPlot") #CFA and SEM plotting
library(semPlot)
install.packages("semptools") #in my case, for marking sign. in semPlot objects
library(semptools)
> dput(exampledata)
structure(list(Subject_idx = 1:50, language_use_family_general = c(-0.625716966920359,
-0.321020009115662, -0.321020009115662, -0.321020009115662, -0.778065445822707,
-0.168671530213314, -0.625716966920359, -0.321020009115662, -0.321020009115662,
-0.625716966920359, -0.47336848801801, -0.47336848801801, 1.35481325881017,
-0.778065445822707, -0.168671530213314, -0.47336848801801, -0.168671530213314,
2.4212526111266, -0.168671530213314, -0.321020009115662, -0.625716966920359,
-0.930413924725055, 0.28837390649373, -0.778065445822707, -0.625716966920359,
-0.47336848801801, 1.50716173771252, -0.625716966920359, -0.778065445822707,
-0.625716966920359, -0.47336848801801, -0.778065445822707, -0.625716966920359,
-0.778065445822707, -0.47336848801801, 3.48769196344304, -0.625716966920359,
0.136025427591382, -0.321020009115662, -0.168671530213314, -0.625716966920359,
-0.321020009115662, -0.47336848801801, -0.47336848801801, -0.321020009115662,
-0.47336848801801, -0.47336848801801, -0.47336848801801, -0.625716966920359,
-0.625716966920359), language_use_internet_browsing = c(-0.372047774082144,
-0.372047774082144, -0.372047774082144, 0.547129079532565, 1.46630593314727,
-0.372047774082144, 0.547129079532565, 1.46630593314727, -0.372047774082144,
0.547129079532565, 0.547129079532565, 1.46630593314727, -2.21040148131156,
-0.372047774082144, -0.372047774082144, -0.372047774082144, 0.547129079532565,
-2.21040148131156, 1.46630593314727, -0.372047774082144, 0.547129079532565,
1.46630593314727, -0.372047774082144, 0.547129079532565, 0.547129079532565,
0.547129079532565, -1.29122462769685, 0.547129079532565, -1.29122462769685,
-0.372047774082144, 0.547129079532565, 1.46630593314727, -0.372047774082144,
-0.372047774082144, -0.372047774082144, -0.372047774082144, -0.372047774082144,
0.547129079532565, 0.547129079532565, 0.547129079532565, -1.29122462769685,
0.547129079532565, 0.547129079532565, -0.372047774082144, 0.547129079532565,
1.46630593314727, -0.372047774082144, 0.547129079532565, -0.372047774082144,
1.46630593314727), language_use_reading = c(-1.43924154848578,
0.592628872905908, 0.592628872905908, 0.592628872905908, 0.592628872905908,
0.592628872905908, 0.592628872905908, 0.592628872905908, -0.423306337789934,
-0.423306337789934, -0.423306337789934, -0.423306337789934, -2.45517675918162,
0.592628872905908, -0.423306337789934, 0.592628872905908, 0.592628872905908,
-2.45517675918162, 0.592628872905908, 0.592628872905908, 0.592628872905908,
1.60856408360175, 0.592628872905908, -0.423306337789934, 0.592628872905908,
0.592628872905908, -1.43924154848578, 0.592628872905908, -1.43924154848578,
1.60856408360175, 0.592628872905908, 1.60856408360175, -0.423306337789934,
-0.423306337789934, -0.423306337789934, -0.423306337789934, -0.423306337789934,
0.592628872905908, 0.592628872905908, 0.592628872905908, -1.43924154848578,
0.592628872905908, -1.43924154848578, 0.592628872905908, 0.592628872905908,
1.60856408360175, -0.423306337789934, -0.423306337789934, 0.592628872905908,
0.592628872905908), language_use_shopping = c(-0.0356835901471926,
0.713671802943855, -0.0356835901471926, -0.0356835901471926,
0.713671802943855, 0.713671802943855, 0.713671802943855, -0.785038983238241,
0.713671802943855, -0.0356835901471926, -0.785038983238241, 0.713671802943855,
-2.28374976942034, 0.713671802943855, 0.713671802943855, 0.713671802943855,
-0.0356835901471926, -2.28374976942034, 0.713671802943855, 0.713671802943855,
0.713671802943855, 0.713671802943855, 0.713671802943855, -0.0356835901471926,
0.713671802943855, 0.713671802943855, -2.28374976942034, 0.713671802943855,
-0.0356835901471926, 0.713671802943855, 0.713671802943855, -0.785038983238241,
-0.0356835901471926, 0.713671802943855, 0.713671802943855, -1.53439437632929,
0.713671802943855, -0.785038983238241, 0.713671802943855, -0.0356835901471926,
-0.0356835901471926, 0.713671802943855, 0.713671802943855, 0.713671802943855,
0.713671802943855, 0.713671802943855, 0.713671802943855, -2.28374976942034,
-0.0356835901471926, 0.713671802943855), language_use_healthcare = c(-0.0958727469627751,
0.575236481776649, 0.575236481776649, 0.575236481776649, 0.575236481776649,
0.575236481776649, 0.575236481776649, -0.0958727469627751, 0.575236481776649,
0.575236481776649, -0.766981975702199, 0.575236481776649, -2.10920043318105,
0.575236481776649, 0.575236481776649, 0.575236481776649, -1.43809120444162,
-2.10920043318105, 0.575236481776649, 0.575236481776649, 0.575236481776649,
0.575236481776649, 0.575236481776649, 0.575236481776649, 0.575236481776649,
0.575236481776649, -2.10920043318105, 0.575236481776649, 0.575236481776649,
0.575236481776649, 0.575236481776649, 0.575236481776649, 0.575236481776649,
0.575236481776649, 0.575236481776649, -1.43809120444162, 0.575236481776649,
-0.766981975702199, -0.0958727469627751, 0.575236481776649, 0.575236481776649,
0.575236481776649, 0.575236481776649, 0.575236481776649, 0.575236481776649,
0.575236481776649, 0.575236481776649, -2.10920043318105, 0.575236481776649,
0.575236481776649), language_use_notes = c(-0.0311803738713032,
-0.0311803738713032, -0.904230842267795, -0.904230842267795,
1.71492056292168, -0.0311803738713032, 0.841870094525189, 1.71492056292168,
1.71492056292168, -0.0311803738713032, -0.904230842267795, -0.904230842267795,
-1.77728131066429, 0.841870094525189, 0.841870094525189, -0.0311803738713032,
0.841870094525189, -1.77728131066429, 0.841870094525189, 0.841870094525189,
0.841870094525189, -0.0311803738713032, 0.841870094525189, -0.0311803738713032,
0.841870094525189, -0.904230842267795, -0.904230842267795, -0.0311803738713032,
-1.77728131066429, -0.0311803738713032, 0.841870094525189, 1.71492056292168,
-0.0311803738713032, -0.0311803738713032, -0.0311803738713032,
-0.0311803738713032, -0.0311803738713032, 0.841870094525189,
0.841870094525189, 0.841870094525189, -1.77728131066429, -0.0311803738713032,
-0.904230842267795, 0.841870094525189, 0.841870094525189, 1.71492056292168,
-1.77728131066429, 1.71492056292168, 0.841870094525189, -0.0311803738713032
), language_use_work = c(0.132059702984447, 0.132059702984447,
1.01950090703993, -1.19910210309878, -1.19910210309878, 1.01950090703993,
0.575780305012189, 1.01950090703993, -1.19910210309878, 1.01950090703993,
1.01950090703993, -1.19910210309878, -0.755381501071037, 1.01950090703993,
-1.19910210309878, -1.19910210309878, -1.19910210309878, -0.755381501071037,
-1.19910210309878, 1.01950090703993, -1.19910210309878, 1.01950090703993,
1.01950090703993, 1.01950090703993, 0.575780305012189, -1.19910210309878,
-0.755381501071037, 1.01950090703993, 1.01950090703993, 1.01950090703993,
-1.19910210309878, 1.01950090703993, 1.01950090703993, -1.19910210309878,
0.575780305012189, -0.755381501071037, -1.19910210309878, -1.19910210309878,
1.01950090703993, 1.01950090703993, 0.575780305012189, 1.01950090703993,
1.01950090703993, -1.19910210309878, 1.01950090703993, -1.19910210309878,
-0.755381501071037, -1.19910210309878, 0.575780305012189, 1.01950090703993
)), row.names = c(NA, 50L), class = "data.frame")
exampledata_model <- '
VARIABLE =~ language_use_family_general + language_use_internet_browsing + language_use_reading + language_use_work +
language_use_shopping + language_use_notes
'
#fit the model
fit_exampledata_model <- cfa(exampledata_model, estimator = "MLM",
data = exampledata, std.lv = TRUE)
summary(fit_exampledata_model, fit.measures = TRUE, std = TRUE)
## Plot the model
semplot_exampledata <- semPaths(fit_exampledata_model, what="std", whatLabels="std",
style="ram", layout="spring", sizeMan=7, sizeLat=5, sizeMan2=3, sizeLat2=4,
groups="latents", edge.color="black", structural=FALSE,
fixedStyle=c("navy",lty=1), freeStyle=c("black",lty=1),
optimizeLatRes=TRUE, pastel=TRUE, rainbowStart=0.2,
edge.label.cex=1.5, asize=2, esize=2, label.cex=4, edge.label.bg=FALSE,
open=FALSE, edge.label.color="black", edge.label.position=.5,
nCharEdges=0, nCharNodes=0, label.prop=1.5)
plot(semplot_exampledata)
semplot_exampledata_sig <- mark_sig(semplot_exampledata, fit_exampledata_model,
alphas = c(`*` = 0.05, `**` = 0.01, `***` = 0.001))
qgraph(semplot_exampledata_sig)
What I so far tried: 1.
qgraph(semplot_exampledata_sig, filetype = "pdf", height = 5, width = 10)
Still same squished result.
2.
ggsave(plot = semplot_exampledata_sig, filename = '...\\semplot_exampledata_sig.pdf',
width = 260, height = 190)
Does not work on the type of object my plot seems to be (qgraph)
3.
install.packages("magick")
library(magick)
image_write(semplot_exampledata_sig, path = "semplot_exampledata_sig.png", format = "png")
Does not seem to do the purpose as it wants an initial image.
- I, of course, tried other image formats as well (in my desperation), but the result was always the same.
Finally, here are is my version info (OS is Windows): R version 4.0.3 (2020-10-10).
Any ideas of what I'm doing wrong would be highly valued! Thank you for the patience to read this long post.