Drawing Simple Mediation Diagram in R

Drawing simple mediation diagram in R

While @baptiste solution might work as well, I was looking for a publishable format.

Function plotmat from library(diagram) is the one that got me the closest to my example:

path diagram

For a reproducible example use:

library(diagram)
data <- c(0, "'.47*'", 0,
0, 0, 0,
"'.36*'", "'.33* (.16)'", 0)
M<- matrix (nrow=3, ncol=3, byrow = TRUE, data=data)
plot<- plotmat (M, pos=c(1,2),
name= c( "Math self-efficacy","Math ability", "Interest in the math major"),
box.type = "rect", box.size = 0.12, box.prop=0.5, curve=0)

R draw diagram with custom coefficients

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

Sample Image

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

Print DiagrammeR object inside for-loop

Here is how to fix it:

# Create an empty list to save the htmlwidgets
plot_list <- htmltools::tagList()

for (i in 1:2){

graph <- DiagrammeR::grViz("
digraph test {

node [shape = circle]
A; B

A -> B

}
")

# Store the htmlwidget inside this list
plot_list[[i]] <- graph
}

#Print this list
plot_list


Related Topics



Leave a reply



Submit