Creating Non-Overlapping Edges in igraph Plot with ggraph in R

Plotting igraph with Fixed Vertex Locations and Non-Overlapping Edges

In this article, we’ll explore how to plot an igraph graph with fixed vertex locations and non-overlapping edges. We’ll go through the process of creating such a plot using R, specifically utilizing the ggraph package.

Background on igraph

igraph is a powerful library for network analysis in R. It provides a wide range of tools for creating, manipulating, and analyzing complex networks. One of its key features is its ability to handle weighted and directed graphs with multiple layers.

Understanding Edge Curvature

When plotting edges in an igraph graph, the edge curvature can make or break the visual appeal of the plot. In this article, we’ll focus on how to achieve non-overlapping edges without manually setting a specific curvature for each edge.

Introduction to ggraph

ggraph is a visualization package built on top of ggplot2 that provides an efficient way to create network plots. It supports various layout algorithms and customization options for creating visually appealing plots.

Creating the Plot with Manual Layout

Let’s start by creating the plot using manual layout. We’ll first import the necessary libraries, load the data, and set up the layout coordinates.

library(tidygraph)
library(ggraph)

# Load the graph data
g <- SEMgraph::dagitty2graph(d)

# Set the vertex coordinates manually
g_vertex_coords <- matrix(c(1, 0,
                           2, 0,
                           3, 0,
                           1, 1,
                           2, 1,
                           3, 1), nrow = 6, byrow = TRUE)

# Create the plot with manual layout
ggraph(g, layout = 'manual', x = g_vertex_coords[,1], y = g_vertex_coords[,2])

Adding Edges with Non-Overlapping Curvature

To achieve non-overlapping edges without manually setting a specific curvature for each edge, we can utilize the strength aesthetic in the geom_edge_arc function. By setting a low-valued strength, we’ll create a slight curvature that makes the edges appear to connect without overlapping.

ggraph(g, layout = 'manual', x = g_vertex_coords[,1], y = g_vertex_coords[,2]) +
  geom_edge_arc(aes(label = seq_along(from)),
                strength = 0.25,
                arrow = arrow(angle = 30, length = unit(3, 'mm'),
                              type = 'closed'), end_cap = circle(7, 'mm'),
                start_cap = circle(7, 'mm'),
                color = 'gray50',
                label_dodge = unit(2, 'mm'), vjust = -1) +
  geom_node_circle(aes(r = 0.15), fill = '#ffe0a0', color = 'orange4') +
  geom_node_text(aes(label = name)) +
  coord_equal() +
  theme_graph()

Adding Axes to the Plot

If you want to add axes to your plot, you can use the theme_bw function.

ggraph(g, layout = 'manual', x = g_vertex_coords[,1], y = g_vertex_coords[,2]) +
  geom_edge_arc(aes(label = seq_along(from)),
                strength = c(0, 0.4, -0.2, 0, -0.1, 0, 0.1, 0.1),
                arrow = arrow(angle = 30, length = unit(3, 'mm'),
                              type = 'closed'), end_cap = circle(7, 'mm'),
                start_cap = circle(7, 'mm'),
                color = 'gray50',
                label_dodge = unit(2, 'mm'), vjust = -1) +
  geom_node_circle(aes(r = 0.15), fill = '#ffe0a0', color = '#60401080',
                   size = 0.8) +
  geom_node_text(aes(label = name)) +
  coord_equal() +
  theme_bw()

Conclusion

In this article, we’ve explored how to plot an igraph graph with fixed vertex locations and non-overlapping edges using the ggraph package in R. By utilizing manual layout coordinates, a low-valued strength aesthetic, and customization options for edge appearance and node text, we can create visually appealing plots that effectively communicate network relationships.

Code Used

library(tidygraph) library(ggraph)

Load the graph data

g <- SEMgraph::dagitty2graph(d)

Set the vertex coordinates manually

g_vertex_coords <- matrix(c(1, 0, 2, 0, 3, 0, 1, 1, 2, 1, 3, 1), nrow = 6, byrow = TRUE)

Create the plot with manual layout

ggraph(g, layout = ‘manual’, x = g_vertex_coords[,1], y = g_vertex_coords[,2])

Add edges with non-overlapping curvature

ggraph(g, layout = ‘manual’, x = g_vertex_coords[,1], y = g_vertex_coords[,2]) + geom_edge_arc(aes(label = seq_along(from)), strength = 0.25, arrow = arrow(angle = 30, length = unit(3, ‘mm’), type = ‘closed’), end_cap = circle(7, ‘mm’), start_cap = circle(7, ‘mm’), color = ‘gray50’, label_dodge = unit(2, ‘mm’), vjust = -1) + geom_node_circle(aes(r = 0.15), fill = ‘#ffe0a0’, color = ‘orange4’) + geom_node_text(aes(label = name)) + coord_equal() + theme_graph()

Add axes to the plot

ggraph(g, layout = ‘manual’, x = g_vertex_coords[,1], y = g_vertex_coords[,2]) + geom_edge_arc(aes(label = seq_along(from)), strength = c(0, 0.4, -0.2, 0, -0.1, 0, 0.1, 0.1), arrow = arrow(angle = 30, length = unit(3, ‘mm’), type = ‘closed’), end_cap = circle(7, ‘mm’), start_cap = circle(7, ‘mm’), color = ‘gray50’, label_dodge = unit(2, ‘mm’), vjust = -1) + geom_node_circle(aes(r = 0.15), fill = ‘#ffe0a0’, color = ‘#60401080’, size = 0.8) + geom_node_text(aes(label = name)) + coord_equal() + theme_bw()


Last modified on 2023-11-12