-1

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?enter image description here

1

1 Answer 1

2

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()

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

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?
@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 like
Or just drag your plotting window to make the plot larger. The circles will get bigger but the text won't.
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
I think geom_node_label would draw the nodes as rectangles rather than circles @Baz.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.