2

I want to draw the following diagram in R:
enter image description here

I saw that the lavaan package has a function to build diagrams, however you need to provide an already fitted model. I just want to build the diagram above with my own coefficients and use it in Rmarkdown. Is there any straightforward way to achieve this? Colors are not important.

mat
  • 2,412
  • 5
  • 31
  • 69

1 Answers1

0

Here is one solution:

library(DiagrammeR)

grViz("
digraph {
node [shape=none] IV1; IV2; MED; DV

IV1 -> MED [label=3.2] 
IV1 -> DV [label=0.2] 
IV2 -> DV [label=9.3]
IV2 -> MED [label=0.5]
MED -> DV [label=5.1]
}"
)

Giving

enter image description here

There are vast options for customisation. See here as a starting point.

Limey
  • 10,234
  • 2
  • 12
  • 32
  • Thanks. And do you know if it would be possible to adjust the shape so that it resembles mine? You don't necessarily have to do it, just would like to know if it is possible before diving into it. – mat Mar 26 '21 at 08:42
  • It should be. I'm not an expert on the package. The link I gave you is a starting point. Beyond that, there's always Google: a search on "diagrammer position node" gave me `set_node_position()` as the [first hit](https://rdrr.io/cran/DiagrammeR/man/set_node_position.html), for example. – Limey Mar 26 '21 at 08:49
  • See also [this](https://observablehq.com/@magjac/placing-graphviz-nodes-in-fixed-positions), which lets you play interactively. – Limey Mar 26 '21 at 08:54
  • You're welcome. There also appears to be syntax for specifying node positions inside the `[]`. The availability of some options appear to depend on the graph layout (ie dot, neato, twopi, circo, etc). Some posts suggest the `rank` attribute may helpful - and less fiddly than using absolute position. Direction `LR` also appears to be an option. :=) – Limey Mar 26 '21 at 18:53
  • I will have a look into that, thanks for the extra information! – mat Mar 27 '21 at 12:00