1

I am trying to recreate the graph that is provided by the function centralityPlot in qgraph. I got a dataframe that looks like this:

                                                       symptom structure(list(symptom = c("9", "8", "7", "6", "5", "4", "3", 
"2", "1"), lower_bound = c(0.209023862993771, -0.656057911395765, 
-0.144732954079441, -0.240150983834066, -2.09690619987396, -1.14713000698362, 
-1.78304406354482, -1.31269792892215, -1.04552934099257), mean = c(1.35359542511945, 
0.546873106351184, 0.787717966105717, 0.42221064177518, -1.18693181743255, 
-0.284265955202698, -1.19008711707311, -0.377827032555581, -0.0712852170875892
), upper_bound = c(1.9749871489344, 1.54642345677796, 1.46727206716789, 
1.10712439281518, -0.0748008645128608, 0.812125575894532, -0.510038969136605, 
0.587753574399307, 0.981045133733119)), class = "data.frame", row.names = c(NA, 
-9L))

it should look like a singular plot like this one

enter image description here

It's supposed to be doable in GGplot but so far What I've gotten is a complete mess:

temporal.dep.in.plot <- ggplot(temporal.dep.in, aes(x = symptom)) +
  ylim(NA, 2.25) +
  geom_errorbar(
    aes(ymin = lower_bound, ymax = upper_bound),
    width = 0.4,
    color = "#56B4E9"
  ) +
  geom_segment(
    aes(y = lower_bound, yend = upper_bound, xend = symptom),
    linetype = "solid",
    color = "#2166AC",
    size = 6
  ) +
  geom_point(
    aes(y = mean),
    shape = 16,
    size = 9,
    color = "#D6604D"
  ) +
  theme_classic() +
  coord_flip() +
  ylab("Z-scores") + xlab("Symptoms")  +
  theme(axis.text.y = element_text(
    face = "bold",
    colour =  c(
      "#ff0000",
               "#ffaa00",
               "#aaff00",
               "#00ff00",
               "#00ffaa",
               "#00aaff",
               "#0000ff",
               "#aa00ff",
               "#ff00aa"
    ),
    size = 14
  ))

which honestly only works trough sheer force of will.

If that's too much, the main part that I'm trying to achieve right now is actually connecting the dots (mean) with a line, which so far has not been working with many methods that I've tried.

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • Welcome to SO, Jesse! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) which means to provide your data in a reproducible manner using `dput()`, i.e. type `dput(temporal.dep.in)` into the console and copy the output into your post. – stefan Feb 01 '23 at 20:05
  • 1
    @stefan thank you, I have altered it now – Jesse Wonnink Feb 01 '23 at 21:37

2 Answers2

0

Not sure how your final plot should look like. As is it looks a quite different from the one in your image. But to connect your mean points to could use a geom_line where the important step is to set group aes to a constant value e.g. 1.

library(ggplot2)

ggplot(temporal.dep.in, aes(x = symptom)) +
  ylim(NA, 2.25) +
  geom_errorbar(
    aes(ymin = lower_bound, ymax = upper_bound),
    width = 0.4,
    color = "#56B4E9"
  ) +
  geom_segment(
    aes(y = lower_bound, yend = upper_bound, xend = symptom),
    linetype = "solid",
    color = "#2166AC",
    size = 6
  ) +
  geom_point(
    aes(y = mean),
    shape = 16,
    size = 6,
    color = "#D6604D"
  ) +
  geom_line(aes(y = mean, group = 1), color = "#D6604D", size = 1) +
  theme_classic() +
  coord_flip() +
  ylab("Z-scores") +
  xlab("Symptoms") +
  theme(axis.text.y = element_text(
    face = "bold",
    colour = c(
      "#ff0000",
      "#ffaa00",
      "#aaff00",
      "#00ff00",
      "#00ffaa",
      "#00aaff",
      "#0000ff",
      "#aa00ff",
      "#ff00aa"
    ),
    size = 14
  ))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
0

You can find the exact code used in qgraph on the Github page. First, the centralityTable function is used to create a table of centrality indices, and second the rest of the centralityPlot function is used to call ggplot2.

Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131