How to create nice path diagrams for CFA models in R?

I’m trying to make good-looking path diagrams for my Confirmatory Factor Analysis (CFA) models in R. I’ve used fa.diagram from the psych package for EFA, which gives great results. But for CFA, I’m stuck.

I’ve tried pathDiagram from the sem package, but it only gives me code to paste into Graphviz. That’s not ideal, and it misses some details like correlations.

Here’s what I want:

  • A path diagram similar to fa.diagram, but for CFA models
  • Something I can do entirely in R (no copy-pasting)
  • Ideally, a simple solution that works with different graphics devices

Any ideas on how to achieve this? I’m open to using any R package that can do the job well.

Here’s a snippet of my CFA model setup:

library(sem)

# Model specification
model <- specifyModel()
F1 -> item1, lambda1, NA
F1 -> item2, lambda2, NA
F2 -> item3, lambda3, NA
F2 -> item4, lambda4, NA
F1 <-> F2, phi, NA

# Fit the model
fit <- sem(model, covariance_matrix, sample_size)

# Current attempt (not satisfactory)
pathDiagram(fit)

Any help would be appreciated!

I’ve had success using the ‘DiagrammeR’ package for creating CFA path diagrams in R. It’s versatile and allows for fully customizable diagrams without leaving the R environment.

Here’s a basic approach:

library(DiagrammeR)

grViz("\
digraph CFA {\
  rankdir=LR;\
  node [shape=box]\
  F1 [shape=circle];\
  F2 [shape=circle];\
  F1 -> {item1 item2};\
  F2 -> {item3 item4};\
  F1 -> F2 [dir=both,label='phi'];\
}")

This produces a clean, professional-looking diagram. You can adjust node shapes, colors, and layouts to suit your needs. The learning curve is a bit steeper than some alternatives, but the flexibility is worth it in my experience.

For more complex models, you might consider writing a function to generate the diagram code based on your model specification. This approach scales well for larger projects.

have u tried the lavaan package? it’s pretty awesome for CFA stuff. you can use semPlot with it to make cool diagrams. here’s a quick example:

library(lavaan)
library(semPlot)

model <- 'F1 =~ item1 + item2
          F2 =~ item3 + item4
          F1 ~~ F2'

fit <- sem(model, data=your_data)
semPaths(fit, 'std')

hope this helps! lemme know if u need more info

Hey there, Iris_92Paint! :slightly_smiling_face:

I totally get your frustration with path diagrams for CFA models. It’s a common headache in R! Have you checked out the ‘semPlot’ package? It’s pretty nifty for this kind of thing.

Here’s a quick idea:

library(semPlot)
library(lavaan)  # for specifying the model

# Specify your model in lavaan syntax
model <- '
  F1 =~ item1 + item2
  F2 =~ item3 + item4
  F1 ~~ F2
'

# Fit the model
fit <- sem(model, data=your_data)

# Create the plot
semPaths(fit, 'std', layout='tree', edge.label.cex=0.8)

This should give you a decent starting point. You can tweak the appearance with various arguments.

What do you think? Have you tried anything like this before? I’m curious to hear about your experiences with other packages too. Maybe there’s something even better out there that I haven’t discovered yet!