1

I am having trouble generating readable plots from Lavaan in R. I have a fitted model that I try to plot with graph_sem from the tidySEM library. The plots I generate have all of the independent variables overlaid which limits readability (please see attached sreenshot). I would like to have the dependent variable (called X1) on one layer, and all of the independent variables (X2-X6) on another one.

code:

library(lavaan)
library(tidySEM)

data = data.frame(replicate(7,sample(0:1,1000,rep=TRUE)))
head(data)

model <- '

# Model data here
X1 ~ X2 + X3 + X4 + X5  + X6

'

model_fit.sem <- sem(model, data=data, group = 'X7')
summary(model_fit.sem)

graph_sem(model_fit.sem, sig = 0.05)

Result: enter image description here

  • 1
    There is no `graph_sem` library. Do you mean `tidySEM`? This package has a function called `graph_sem` – Allan Cameron Sep 10 '22 at 17:57
  • Also, because your question is so particular to your data, it's not really going to be possible to help unless you provide a reproducible example of your data and the code used to produce your model. Any solution we give using dummy data will have a different shape and will not be applicable to your own problem. – Allan Cameron Sep 10 '22 at 18:18
  • @AllanCameron Yep, sorry for the lazy description. Added code for reproductible example. The library is indeed tidySEM. – Stanislav Nosulenko Sep 10 '22 at 18:25

1 Answers1

0

You can specify a layout in graph_sem

library(lavaan)
library(tidySEM)

model <- 'X1 ~ X2 + X3 + X4 + X5 + X6'

sem(model, data, group = "X7") %>%
  graph_sem(layout = get_layout("",   "",   "X1", "",   "",
                                "",   "",   "",   "",   "",
                                "X2", "X3", "X4", "X5", "X6", rows = 3))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thanks for this. Is there a way to do this automatically without configuring the view for each model? I am asking because the tutorials I saw (for example here in step 2 https://cran.r-project.org/web/packages/tidySEM/vignettes/Plotting_graphs.html) generated much more user-friendly pictures. Maybe I should use some other library (I tried lavaanPlot but it yielded some errors like ```Error in as.character.default(new("lavaan", version = "0.6.8", call = lavaan::lavaan(model = model, : no method for coercing this S4 class to a vector``` )? – Stanislav Nosulenko Sep 10 '22 at 20:49
  • @StanislavNosulenko I'm not sure what you mean by "more user friendly pictures". The picture you get is dependent on the model you specify, and there are a huge number of customization options to make the output suit your needs. No-one can give you a general solution to laying out every model to suit your tastes without specifying layout or customization options. You could always add a wrapper function to do this for you. Your question asked for the independent variables and dependent variables in different levels - that's what this answer shows. – Allan Cameron Sep 10 '22 at 21:01
  • What I mean is that on the original picture, all the connectors are orvelaid - even the connector labels for 2 out of 5 independent variables are not visible - so it is not really a question of taste :). It seems to me that such things are usually processed automatically by the library (and I haven't seen overlays like that in any source I looked at). Thus I wonder if it could be an issue with my setup (bad library version, missing dependencies etc.) – Stanislav Nosulenko Sep 10 '22 at 21:10