thomasp85 / ggraph

Grammar of Graph Graphics
https://ggraph.data-imaginist.com
Other
1.08k stars 116 forks source link

Nodes with different entry/exit points - "double elbow"? - Harris Matrix #376

Open tcwilkinson opened 1 month ago

tcwilkinson commented 1 month ago

First thanks for a great package, which I've been enjoying playing with.

I'm looking for guidance as to whether something is currently possible in ggraph, or could potentially be added.

Background: I'd like to generate "Harris Matrix"-style diagrams (both static and interactive). (https://en.wikipedia.org/wiki/Harris_matrix). These are at base directed (acyclic) graphs/DAGs used in archaeology to represent the stratigraphic and chronological relationship between different layer/cut "contexts", as described during excavation.

Using igraph's Sugiyama layout with geom_node_label and geom_edge_elbow allows me to get a long way there to data-driven Harris Matrix diagrams instead of hand-drawing, but I can't quite replicate the graphical convention of traditional Harris Matrix edges. So I can get this, using the following code:-

library(tidyverse)
library(igraph)
library(ggraph)

context_entities = data.frame(
  name=c("100","101","102","103","104","105")
)
matrix_relationships <- data.frame(
  to=   c("100","101","102","101","103","103"),
  from= c("101","102","103","103","104","105"),
  rel=  c("before","before","before","before","before","before")
)
#create graph and depth map
harris_matrix_graph <- igraph::graph_from_data_frame(matrix_relationships, directed = T,
                                                    vertices=context_entities)
# igraph origin of layout
harris_matrix_graph_layout <- harris_matrix_graph |>
  igraph::layout_with_sugiyama()

harris_matrix_graph %>% ggraph::ggraph(layout="sugiyama") +
  ggraph::geom_edge_elbow() +
  ggraph::geom_node_label(aes(label=name))

image

But to follow field conventions, what I want is something more like this:-

closer

Basically edges from both above and below connect to the same single point, disappearing behind the label (or on top of it, depending on layering). In standard HM format, edges coming in from "above" (=after, in chronological terms) would all connect at a point just above the label before an elbow plunges them down in single line into the label; similarly edges continuing "below" (=before) come out in single line before splitting at an elbow point just below.

Effectively these are visually "double elbow edges", though I'm not sure whether you couldn't also think of the problem as "tall nodes with direction-sensitive entry/exit points"

Question: Is something like this currently possible? If not, and new node/edge geoms required, where would the settings lie (i.e. is this a node drawing problem or an edge drawing problem?