suppressPackageStartupMessages({
  library(rgraph6)
  library(dplyr)
  library(ggraph)
  library(igraph)
})


knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

rgraph6: Representing Graphs as graph6, digraph6 or sparse6 Strings

R-CMD-check rstudio mirror downloads cran version rgraph6 status badge

Functions in this package allow for encoding network data as strings of printable ASCII characters and back using 'graph6', 'sparse6', and 'digraph6' formats. This is convenient in a number of contexts, especially when working with large number of graphs. Provided functions allow to directly encode and decode graph data in the form of adjacency matrices, edgelists, network objects and igraph objects to and from these three formats.

What are 'graph6', 'sparse6' and 'digraph6' formats?

'graph6', 'sparse6' and 'digraph6' are formats for encoding graphs as strings of printable ASCII characters due to Brendan McKay. See here for format specification. Formats 'graph6' and 'sparse6' are for undirected graphs. Format 'digraph6' is for directed graphs.

Functions

Main functions for encoding network data are:

Main functions for decoding are:

Low-level functions are shown on the following graph:

u <- c("adjacency", "edgelist", "network", "igraph", "digraph6", 
       "sparse6", "graph6")

d <- tidyr::crossing(
  from = u,
  to = u
) %>%
  filter(from != to) %>%
  mutate(
    fun = paste0(to, "_from_", from)
  ) %>%
  mutate(
    fun = case_when(
      to == "graph6" & (from %in% c("adjacency", "network", "igraph")) ~ "as_graph6",
      to == "sparse6" & (from %in% c("edgelist", "network", "igraph")) ~ "as_sparse6",
      to == "digraph6" & (from %in% c("adjacency", "network", "igraph")) ~ "as_digraph6",
      TRUE ~ fun
    ),
    ok = purrr::map_lgl(fun, exists, where=asNamespace("rgraph6"))
  )

g <- d %>%
  filter(ok) %>%
  transmute(
    from, to,
    fun = paste0(fun, "()")
  ) %>%
  tidygraph::as_tbl_graph()

x <- c(1.7619, 0.8201, -2.019, 0.8277, -0.0565, -0.0451, -0.9931)
y <- c(-9e-04, -0.4761, 0.0281, 0.4898, -0.4738, 0.4898, 0.0206)
E(g)$cross <- FALSE
E(g)$cross[c(13,5,11,8)] <- TRUE
ggraph(g,layout = "manual",x = x,y = y) +
  geom_edge_parallel(
    aes(label = fun, filter = !cross),
    start_cap = circle(1),
    end_cap = circle(1),
    label_dodge = unit(-5, "mm"),
    label_push = unit(0, "mm"),
    angle_calc = "along",
    arrow = arrow(length = unit(3, "mm"),type = "closed")
  ) +
  geom_edge_parallel(
    aes(label = fun, filter = cross),
    start_cap = circle(1),
    end_cap = circle(1),
    label_dodge = unit(-5, "mm"),
    label_push = unit(-27, "mm"),
    angle_calc = "along",
    arrow = arrow(length = unit(3, "mm"),type = "closed")
  )+
  geom_node_label(aes(label = name)) +
  coord_cartesian(clip = "off") +
  scale_y_continuous(expand = expansion(add = .1)) +
  theme_void()

Examples

Encode list of igraph objects

Generate a list of igraph objects:

set.seed(666)
igraph_list <- replicate(5, igraph::sample_gnp(10, 0.1, directed=FALSE), 
                         simplify = FALSE)

Encode as 'graph6' symbols:

as_graph6(igraph_list)

Encode as 'sparse6' symbols:

as_sparse6(igraph_list)

Decode a vector of different types of symbols

Using example data g6, d6, and s6 provided with the package:

# Create a vector with a mixture of 'graph6', 'digraph6' and 'sparse6' symbols
x <- c(g6[1], s6[2], d6[3])
x

# Parse to igraph objects (package igraph required)
igraph_from_text(x)

# Parse to network objects (package network required)
network_from_text(x)

Tidy graph databases

The formats shine if we need to store large number of graphs in a data frame. Let's generate a list of random graphs as igraph objects and store them in a data frame column of graph6 symbols:

library("dplyr")

# Generate list of igraph objects
set.seed(666)

d <- tibble::tibble(
  g6 = replicate(
    10,
    igraph::random.graph.game(sample(3:12, replace=TRUE), p=.5, directed=FALSE),
    simplify=FALSE
  ) %>%
    as_graph6()
)
d

Nice and compact. We can go further by doing some computations and saving the results together with the graph data, and even save it to a simple CSV file!

d %>%
  dplyr::mutate(
    igraphs = igraph_from_text(g6),
    vc = purrr::map_dbl(igraphs, igraph::vcount),
    ec = purrr::map_dbl(igraphs, igraph::ecount),
    density = purrr::map_dbl(igraphs, igraph::edge_density)
  ) %>%
  dplyr::select(-igraphs) %>%
  write.csv(row.names = FALSE)

Installation

Install development version from GitHub with:

# install.packages("remotes")
remotes::install_github("mbojan/rgraph6", build_vignettes=TRUE)

Nightly Windows and MacOS binaries are available on R Universe:

install.packages("rgraph6", repos = "https://mbojan.r-universe.dev")

Authors, contributors and citation

pd <- read.dcf("DESCRIPTION")
p <- eval(parse(text = pd[,"Authors@R"]))
is_maintainer <- purrr::map_lgl(p, ~ "cre" %in% .x$role)
is_author <- purrr::map_lgl(p, ~ "aut" %in% .x$role)

Author and maintainer: r format(p[is_maintainer], include = c("given", "family", "email", "comment")).

Co-authors: r paste(format(p[is_author & !is_maintainer], include = c("given", "family", "comment")), collapse=", ")

To cite this package please use the following entries:

cat(format(
  readCitationFile("inst/CITATION", meta = list(Encoding = "UTF-8")), 
  style = "html"
))


mbojan/rgraph6 documentation built on Feb. 4, 2024, 3:40 p.m.