rggraphtidygraph

R ggraph: how to plot bi-directional arrows in network?


I would like to do a network graph in R using ggraph where an an edge relationship goes both ways A -> B and B -> A. I would like top plot this with ggraph as a single bi-directional arrow? A solution on ggraph's Github issue is to use arrow(ends="both"), but this will plot two bi-directional arrows, while I want a single bi-directional arrow?

Is this possible with ggraph? Or maybe I should specify my underlying graph differently?

library(tidygraph)
library(ggraph)
#> Loading required package: ggplot2
df <- data.frame(form=c(2,1,3), to = c(1,2,1)) |> 
                       as_tbl_graph() 

df |> 
  ggraph(layout = 'kk') + 
  geom_node_point() +
  geom_edge_link(arrow = arrow(ends="last"),
                 end_cap = circle(8, 'mm'),
                 start_cap = circle(3, 'mm'))

Created on 2023-06-06 with reprex v2.0.2


Solution

  • If you set the cap circle()-size to the same value for start and end it will appear like one edge with arrows on both sides. Alternatively you can use igraph::plot().

    library(tidygraph)
    library(ggraph)
    
    df <- data.frame(form=c(2,1,3), to = c(1,2,1)) |> 
      as_tbl_graph() 
    
    df |> 
      ggraph(layout = 'kk') + 
      geom_node_point() +
      geom_edge_link(arrow = arrow(ends="last"),
                     end_cap = circle(8, 'mm'),
                     start_cap = circle(8, 'mm'))
    
    

    
    plot(df)