layout_from_matrix <- function(graph, coordinates) {
tibble(
node_id = igraph::V(graph)$name,
x = coordinates[, 1],
y = coordinates[, 2]
) |>
left_join(nodes, by = "node_id")
}
layout_fixed <- tribble(
~node_id, ~x, ~y,
"ORG-0002", 0.0, 0.0,
"CRD-0002", -2.2, 1.5,
"VALUE-October 15", 2.2, 1.5,
"VALUE-November 1", 2.2, -1.1,
"LOC-0001", -3.0, -3.6,
"LOC-0002", -1.0, -3.6,
"ORG-0003", 1.0, -3.6,
"LOC-0003", 3.0, -3.6
) |>
left_join(nodes, by = "node_id")
edge_label_positions <- tribble(
~from, ~to, ~label_x, ~label_y,
"ORG-0002", "CRD-0002", -1.25, 0.95,
"ORG-0002", "VALUE-October 15", 1.30, 0.95,
"ORG-0002", "VALUE-November 1", 1.35, -0.55
)
shorten_segments <- function(edge_positions, amount = 0.20) {
edge_positions |>
mutate(
x_start = x_from + amount * (x_to - x_from),
y_start = y_from + amount * (y_to - y_from),
x_end = x_from + (1 - amount) * (x_to - x_from),
y_end = y_from + (1 - amount) * (y_to - y_from),
x_mid = (x_start + x_end) / 2,
y_mid = (y_start + y_end) / 2
)
}
edge_positions <- edges |>
left_join(
layout_fixed |> select(from = node_id, x_from = x, y_from = y),
by = "from"
) |>
left_join(
layout_fixed |> select(to = node_id, x_to = x, y_to = y),
by = "to"
) |>
left_join(edge_label_positions, by = c("from", "to")) |>
shorten_segments() |>
mutate(relation_label = str_replace_all(relation, "_", " "))
node_positions <- layout_fixed |>
mutate(display_label = paste0(label, "\n", node_id))
graph_plot <- ggplot() +
geom_segment(
data = edge_positions,
aes(x = x_start, y = y_start, xend = x_end, yend = y_end),
arrow = grid::arrow(type = "closed", length = grid::unit(0.12, "inches")),
linewidth = 0.4,
lineend = "round"
) +
geom_label(
data = edge_positions,
aes(x = label_x, y = label_y, label = relation_label),
fill = "white",
linewidth = 0.15,
size = 2.8
) +
geom_label(
data = node_positions,
aes(x = x, y = y, label = display_label),
fill = "white",
linewidth = 0.25,
size = 3
) +
coord_equal(clip = "off") +
expand_limits(x = c(-3.7, 3.7), y = c(-4.1, 2.2)) +
theme_void() +
theme(plot.margin = margin(15, 15, 15, 15))
graph_plot