0
votes

I'm trying to use igraph to plot a causal diagram between a few variables. I below is my code and basically everything I want in the graph, except that I cannot get the other two edge labels to move up above the edges, like the one that connects "stability" to "status".

ego <- c("Stability (high)", "Stability (high)", "Stability (high)")
alter <- c("Status", "Depressive symptoms", "Anxiety Symptoms")
association <- c("-", "-", "-")


nodes <- c("Stability (high)", "Status", "Depressive symptoms", "Anxiety Symptoms")
x <- c(-5, 5, 5, 5)
y <- c(4, 8, 4, 0)

edges <- as.data.frame(cbind(ego, alter, association))
nodes <- cbind.data.frame(nodes, x, y)

nodes$x <- as.numeric(nodes$x)
nodes$y <- as.numeric(nodes$y)


study1 <- graph_from_data_frame(edges, nodes, directed = TRUE)

E(study1)$color <- "red"

plot(study1, layout=as.matrix(nodes[,c("x","y")]),
     vertex.size = 75,
     vertex.color = "gray",
     vertex.label.color = "black",
     vertex.label.family = "Arial",
     vertex.label.cex = 0.7,
     edge.arrow.size = 0.7,
     edge.width = 3.5,
     edge.color = E(study1)$color,
     edge.label = E(study1)$association,
     edge.label.y = 0.5,
     edge.label.cex = 3,
     edge.label.color = "black")

causal diagram

1

1 Answers

2
votes

You have to specify for each label the y-coordinate, i.e. edge.label.y = c(0.6, 0.2, -0.5) . I changed your code a little bit so you can see which label is which, i.e. association <- c("A", "B", "C")

The complete code:

library(igraph)

ego <- c("Stability (high)", "Stability (high)", "Stability (high)")
alter <- c("Status", "Depressive symptoms", "Anxiety Symptoms")
association <- c("A", "B", "C")


nodes <- c("Stability (high)", "Status", "Depressive symptoms", "Anxiety Symptoms")
x <- c(-5, 5, 5, 5)
y <- c(4, 8, 4, 0)

edges <- as.data.frame(cbind(ego, alter, association))
nodes <- cbind.data.frame(nodes, x, y)

nodes$x <- as.numeric(nodes$x)
nodes$y <- as.numeric(nodes$y)


study1 <- graph_from_data_frame(edges, nodes, directed = TRUE)

E(study1)$color <- "red"

plot(study1, layout=as.matrix(nodes[,c("x","y")]),
     vertex.size = 75,
     vertex.color = "gray",
     vertex.label.color = "black",
     vertex.label.family = "Arial",
     vertex.label.cex = 0.7,
     edge.arrow.size = 0.7,
     edge.width = 3.5,
     edge.color = E(study1)$color,
     edge.label = E(study1)$association,
     edge.label.y = c(0.6, 0.2, -0.5), # specify the y-coordinate for each label
     edge.label.cex = 3,
     edge.label.color = "black")

This wil result in: the result