alishinski / lavaanPlot

R package for plotting lavaan path models with DiagrammeR
http://alexlishinski.com/lavaanPlot/
38 stars 4 forks source link

Feature Request: Plotting EFA models #48

Open jmgirard opened 4 months ago

jmgirard commented 4 months ago

It would be great to get support for plotting EFAs from lavaan.

library(lavaan)
var.names <- paste("x", 1:9, sep = "")
fit <- efa(data = HolzingerSwineford1939[,var.names], nfactors = 1:3)
summary(fit)
alishinski commented 4 months ago

So there is a way to do it that's possible currently. Whether that would fit your use case is maybe another question.

The efa() function has an option output = "lavaan" that you can use when the number of factors is given as a single integer value. e.g.:

fit <- efa(data = HolzingerSwineford1939,
           ov.names = paste("x", 1:9, sep = ""),
           nfactors = 3,
           rotation = "geomin",
           rotation.args = list(geomin.epsilon = 0.01, rstarts = 1), 
           output = "lavaan")

And if you do that, it's amenable to lavaanPlot() and lavaanPlot2().

lavaanPlot(fit)

plot_std

That's a bit messy though, but you can get a visually clearer, albeit more unorthodox, version of this with layout options. E.g.:

lavaanPlot(fit, graph_options = list(layout = "circo"))

plot_circo

That is also certainly not perfect, particularly if you wanted to start printing coef estimates on there as well, but with 3 or more factors loading on everything, I'm having a hard time imagining a much cleaner way of doing it.

But I'm open to hearing your thoughts on this.

jmgirard commented 4 months ago

I could see the first layout working if the two rows were spaced more vertically and perhaps the user could select a cutoff/threshold for loadings to plot as arrows (or perhaps those lower in magnitude than the cutoff could be dashed or something). I could also see the second layout working if the arrows had linewidth scaled by loading magnitude. It might also be nice to have an S3 method for EFA models that can accommodate multiple EFA results. So if you give it nfactors = 4:5, it will plot both models as separate plots.