Skip to contents

Creating a graph object

This is how you create graph objects of class igraph or network.

Graph creation
Create an empty network
snafun
snafun::create_empty_graph(n_vertices, directed = TRUE, graph = c("igraph", "network"))
igraph
igraph::make_empty_graph()
network
network::network::initialize()
Create a manual (literal) network
snafun
snafun::create_manual_graph(A -+ B -+ C, simplify = TRUE, graph = 'igraph')
igraph
igraph::graph_from_literal(A -+ B -+ C, simplify = TRUE)
network
Create a ring network
snafun
igraph
igraph::make_ring()
network
Create a star network
snafun
igraph
igraph::make_star()
network
Create a random graph with given density
snafun
snafun::create_random_graph(
    n_vertices = 10,
    strategy = "gnm",   # to fix the number of edges
    m = 27,             # number of edges required, resulting density = .3
    directed = TRUE,    # or FALSE
    graph = c("igraph", "network"))   # pick one

snafun::create_random_graph(
    n_vertices = 10,
    strategy = "gnp",   # to fix the probability edges
    p = .3,             # probability for each edge, yields approx. density of .3
    directed = TRUE,    # or FALSE
    graph = c("igraph", "network"))
igraph
igraph::sample_gnm()
igraph::sample_gnp()

igraph::make_directed_graph(n = 10, edges = 27)
igraph::make_undirected_graph(n = 10, edges = 27)
network1
# 10 vertices, density on average .3
sna::rgraph(n = 10, m = 1, tprob = 0.30, mode = 'digraph')

# 10 vertices, 27 edges (ie. density = .3)
sna::rgnm(1, 10, m = 27)
Create a random graph with given dyad census
snafun
create_census_graph(n_vertices = 10, mut = 8, asym = 25, null = 12, 
                                   method = 'exact',
                                   graph = 'igraph')
igraph
network1
# 5 networks, each with 10 vertices, and 8 M, 25 A, and 12 N dyads
sna::rguman(n = 5, nv = 10, mut = 8, asym = 25, null = 12, 
  method = "exact")
Create a random graph with given community struture
snafun
snafun::create_community_graph(
  communitySizes = c(10, 20, 30),
  p_intra = c(0.3, 0.2, 0.3),
  p_inter = 0.2,
  p_del = 0,
  graph = 'igraph')
igraph
network
Create a graph with given components
snafun
create_components_graph(
  n_vertices,
  directed = FALSE,
  membership = NULL,
  graph = 'igraph')
igraph
network
Create random bipartite graph
snafun
snafun::create_bipartite(
  n_type1,                        # number of vertices of type 1
  n_type2,                        # number of vertices of type 2
  strategy = c("gnp", "gnm"),
  p,                              # probability of each cross-type edge
  m,                              # number of cross-type edges
  directed = FALSE,
  mode = c("out", "in", "all"),
  graph = c("igraph", "network")
)
igraph
igraph::sample_bipartite()
network2
network::network.bipartite()
Create graph object from input data
snafun
#  `x` can be: 
#   - an edgelist (in data.frame format)
#   - an incidence matrix (in matrix format)--for a bipartite graph
#   - an adjacency matrix (in matrix format)
#   - `vertices`  can be a data.frame containing vertex attributes

snafun::to_igraph(x, bipartite = FALSE, vertices = NULL)

snafun::to_network(x, bipartite = FALSE, vertices = NULL)
igraph
# from an adjacency matrix
igraph::graph_from_adjacency_matrix(adjmatrix, 
  mode = c("directed", "undirected", "max", "min", "upper", "lower", "plus"),
  weighted = NULL, diag = TRUE, add.colnames = NULL, add.rownames = NA)

igraph::make_graph(edges, ..., n = max(edges), isolates = NULL, directed = TRUE, 
  dir = directed, simplify = TRUE)

# from an adjacency list
igraph::graph_from_adj_list(adjlist, mode = c("out", "in", "all", "total"), 
  duplicate = TRUE)

# from an edgelist
igraph::graph_from_edgelist(el, directed = TRUE)

# from a data.frame
igraph::graph_from_data_frame(d, directed = TRUE, vertices = NULL)

# from an incidence matrix (in matrix format)
igraph::graph_from_incidence_matrix(incidence, directed = FALSE, 
  mode = c("all", "out", "in", "total"), multiple = FALSE,
  weighted = NULL, add.names = NULL)
network
# x can be an adjacency matrix (as a matrix), an incidence matrix (as a matrix), 
# or an edgelist (as a data.frame)
network::network(x, vertex.attr = NULL, vertex.attrnames = NULL, directed = TRUE,
  hyper = FALSE, loops = FALSE, multiple = FALSE, bipartite = FALSE, ...)

# if x is a data.frame
network::as.network(x, directed = TRUE, vertices = NULL, hyper = FALSE, 
  loops = FALSE, multiple = FALSE, bipartite = FALSE, 
  bipartite_col = "is_actor", ...)

# if x is a matrix
network::as.network(x, matrix.type = NULL, directed = TRUE, hyper = FALSE, 
  loops = FALSE, multiple = FALSE, bipartite = FALSE, ignore.eval = TRUE,
  names.eval = NULL, na.rm = FALSE, edge.check = FALSE, ...)
1 The output is a matrix object, not a 'network' object.
2 This function is quite unintuitive and cumbersome


Additional graph creation functions

The snafun package offers a few functions that assist with the manipulation of graph data in R.

  • snafun::make_edgelist(names = NULL, attribute = NULL)

The input is a data.frame (names) with edge information. The attribute is a vector that contains a node attribute for those vertices.

The function returns a vector or data.frame that can be read into igraph or network.

  • snafun::make_nodelist(names = NULL, attribute = NULL)

The input is a data.frame (names) with edge information. The attribute is another data.frame that contains the values of those edges.

The function returns an edgelist that can be read into igraph or network.