I am building a directed graph using tidygraph and ggraph.
The issue is that arrow tips extend inside rectangular node labels created with geom_node_label(), making arrow direction difficult to see.
I tried:
Increasing figure size with
ggsave()Changing arrow length
Using
end_cap = circle()
But circle() applies a fixed radius and does not respect the rectangular geometry of geom_node_label().
Is there a way to automatically clip arrows to the border of rectangular node labels?
Here is a minimal reproducible example:
library(tidygraph)
library(ggraph)
library(ggplot2)
library(grid)
nodes <- data.frame(
name = c(
"Sun exposure",
"Deadwood volume",
"Altitude",
"Wood-inhabiting fungi\nComposition",
"VOC\nComposition",
"Arriving beetles\nα-diversity"
)
)
edges <- data.frame(
from = c(
"Sun exposure",
"Deadwood volume",
"Altitude",
"Wood-inhabiting fungi\nComposition",
"VOC\nComposition"
),
to = rep("Arriving beetles\nα-diversity", 5)
)
graph <- tbl_graph(nodes = nodes, edges = edges, directed = TRUE)
ggraph(graph, layout = "sugiyama") +
geom_edge_link(
arrow = arrow(length = unit(4, "mm"), type = "closed"),
end_cap = circle(6, "mm"),
lineend = "round"
) +
geom_node_label(
aes(label = name),
size = 5,
label.padding = unit(0.4, "lines")
) +
theme_void()
In this example, the arrow tips overlap the rectangular labels.
How can I make arrow tips stop exactly at the label border (respecting the rectangular dimensions)?