Please could someone advise how to draw a mediation diagram similar to the one attached? I have tried the Diagrammar + Graphviz but can’t get the values under stress (1.00), wellbeing (0.62) and above self-esteem (0.80) to show on the plot. Does anyone know of an R package that can recreate the mediation diagram like the one attached?
-
1depending on how you have estimated your model, perhaps cran.r-project.org/web/packages/semPlot/index.htmluser20650– user206502024-11-17 22:56:44 +00:00Commented Nov 17, 2024 at 22:56
Add a comment
|
1 Answer
Using the ggraph package might give a visually attractive result, and the colors, text, font, arrows etc are fully customizable too.
library(tidygraph)
library(ggplot2)
library(ggraph)
nodes <- c("Self-esteem\n0.80", "a\n-0.42", "Stress\n1.00",
"c\n-0.32", "Wellbeing\n0.62", "b\n0.28")
data.frame(from = nodes, to = c(nodes[-1], nodes[1])) |>
as_tbl_graph() |>
mutate(x = c(0, -0.5, -1, 0, 1, 0.5),
y = 2 * sin(pi/3) * c(1, 0.5, 0, 0, 0, 0.5)) |>
ggraph(layout = "manual", x = x, y = y) +
geom_edge_link(aes(end_cap = circle(rep(c(0, 1.5), 3))),
arrow = arrow(length = unit(3, "mm"), type = "closed")) +
geom_node_circle(aes(r = rep(2:1/10, 3),
fill = factor(rep(2:1/10, 3)))) +
geom_node_text(aes(label = name)) +
scale_fill_manual(values = c("gray85", "white"), guide = "none") +
theme_graph() +
coord_equal()
5 Comments
Baz
Thank you @Allan. Please how do I increase the size of the circle, as when I ran the code the text did not fit into the circle?
Allan Cameron
@Baz change
geom_node_circle(aes(r = rep(2:1/10, 3), to geom_node_circle(aes(r = rep(c(0.5, 0.25), 3),. The 0.5 and 0.25 are the new sizes of the large and small circles. Change these to whatever you likeAllan Cameron
Or just drag your plotting window to make the plot larger. The circles will get bigger but the text won't.
Baz
thank you so much. That works. Just out of curiosity, is there a way to draw a rectangle or square shape instead of a circle? Just similar to the one I attached. Thank you
Allan Cameron
I think geom_node_label would draw the nodes as rectangles rather than circles @Baz.
