Abstract
RGraphSpace is an R package that integrates igraph and ggplot2 graphics within spatial maps. RGraphSpace implements new geometric objects using ggplot2 prototypes, customized for representing large igraph objects in a normalized coordinate system.
Package: RGraphSpace 1.0.7
RGraphSpace is an R package that generates ggplot2 graphics for igraph objects (Csardi and Nepusz 2006), which are scaled to fit within a standard unit space, making it easier to display different graphs side by side. RGraphSpace implements new geometric objects using ggplot2 prototypes (Wickham 2016), taking into account relative sizes and positions of the input graph. By scaling graph elements, RGraphSpace can provide a framework for layered visualizations, ensuring proper alignment within a spatial map. RGraphSpace’s use of ggplot2 allows for extensive customization of aesthetics and visual style, such as colors, shapes, and line types.
This section will create a toy igraph object to demonstrate
the RGraphSpace workflow. The graph layout is configured
manually to ensure that users can easily view all the relevant arguments
needed to prepare the input data for the RGraphSpace package.
We will use the igraph’s make_star()
function to create a
simple star-like graph and then the V()
and
E()
functions are used to set attributes for vertices and
edges, respectively. The RGraphSpace package will require that
all vertices have x
, y
, and name
attributes.
#--- Load required packages
library(igraph)
library(ggplot2)
library(RGraphSpace)
# Make a 'toy' igraph with 5 nodes and 4 edges
# Note: this will be a directed graph
gtoy1 <- make_star(5, mode="out")
# Check whether the graph is directed or not
is_directed(gtoy1)
## [1] TRUE
# Check graph size
vcount(gtoy1)
## [1] 5
ecount(gtoy1)
## [1] 4
# Assign 'x' and 'y' coordinates to each vertex;
# ..this can be an arbitrary unit in (-Inf, +Inf)
V(gtoy1)$x <- c(0, 3, -6, -6, -9)
V(gtoy1)$y <- c(0, 0, 6, -6, 0)
# Assign a name to each vertex
V(gtoy1)$name <- paste0("n", 1:5)
# Plot the 'gtoy1' using standard R graphics
plot(gtoy1)
# Plot the 'gtoy1' using RGraphSpace
plotGraphSpace(gtoy1, marks = TRUE)
Next, we will demonstrate all vertex and edge attributes that can be passed to RGraphSpace methods.
# Node size (numeric in (0, 100), as '%' of the plot space)
V(gtoy1)$nodeSize <- c(10, 5, 5, 5, 5)
# Node shape (integer code between 0 and 25; see 'help(points)')
V(gtoy1)$nodeShape <- c(21, 22, 23, 24, 25)
# Node color (Hexadecimal or color name)
V(gtoy1)$nodeColor <- c("red", "#00ad39", "grey80", "blue", "cyan")
# Node line width (as in 'lwd' standard graphics; see 'help(gpar)')
V(gtoy1)$nodeLineWidth <- 1
# Node line color (Hexadecimal or color name)
V(gtoy1)$nodeLineColor <- "grey20"
# Node labels ('NA' will omit labels)
V(gtoy1)$nodeLabel <- c("V1", "V2", "V3", "V4", NA)
# Node label size (in pts)
V(gtoy1)$nodeLabelSize <- 12
# Node label color (Hexadecimal or color name)
V(gtoy1)$nodeLabelColor <- "grey40"
Given a list of edges, RGraphSpace represents only one edge for each pair of connected vertices. If there are multiple edges connecting the same vertex pairs, it will display the line attributes of the first edge in the list.
# Edge width (as in 'lwd' standard graphics; see 'help(gpar)')
E(gtoy1)$edgeLineWidth <- 1
# Edge color (Hexadecimal or color name)
E(gtoy1)$edgeLineColor <- c("red","green","blue","black")
# Edge type (as in 'lty' standard graphics; see 'help(gpar)')
E(gtoy1)$edgeLineType <- c("solid", "dotted", "dashed", "2121")
Arrowhead in directed graphs. By default, an arrow will be drawn for
each edge according to its left-to-right orientation in the edge list
(e.g. A -> B
).
# Arrowhead types in directed graphs (integer code or character)
## 0 = "---", 1 = "-->", -1 = "--|"
E(gtoy1)$arrowType <- 1
# Arrowhead length (as in 'lwd' standard graphics; see 'help(gpar)')
E(gtoy1)$arrowLength <- 1
Arrowhead in undirected graphs. By default, no arrow will be drawn in undirected graphs.
# Arrowhead types in undirected graphs (integer or character code)
## 0 = "---"
## 1 = "-->", 2 = "<--", 3 = "<->", 4 = "|->",
## -1 = "--|", -2 = "|--", -3 = "|-|", -4 = "<-|",
E(gtoy1)$arrowType <- 1
# Note: in undirected graphs, this attribute overrides the
# edge's orientation in the edge list
# Arrowhead length (as in 'lwd' standard graphics; see 'help(gpar)')
E(gtoy1)$arrowLength <- 1
… and now plot the updated igraph object with RGraphSpace:
# Plot the updated 'gtoy1' using RGraphSpace
plotGraphSpace(gtoy1, marks = "n1", mark.color = "white")