Create a (small) graph quickly

create_manual_graph(..., simplify = TRUE, graph = c("igraph", "network"))

Arguments

...

the formulae giving the structure of the graph, see details below.

simplify

Logical scalar. By default the graph is simplified, loops and multiple edges are removed.

graph

character, the type of graph to be generated: igraph (default) or network

Value

a graph of class network or igraph

Details

This function is useful if you want to create a small (named) graph quickly, it works for both directed and undirected graphs. You need to supply one or more R expressions giving the structure of the graph. The expressions consist of vertex names and edge operators. An edge operator is a sequence of ‘-’ and ‘+’ characters, the former is for the edges and the latter is used for arrow heads. The edges can be arbitrarily long, i.e. you may use as many ‘-’ characters to “draw” them as you like.

This function mainly acts as a wrapper for graph_from_literal and almost all of the examples come from that function as well.

Examples

# undirected graph
create_manual_graph(1-2)
#> IGRAPH c9d2380 UN-- 2 1 -- 
#> + attr: name (v/c)
#> + edge from c9d2380 (vertex names):
#> [1] 1--2
##  the length of the edges does not matter, so the following this creates the same graph
create_manual_graph( 1-----2 )
#> IGRAPH c9d31b0 UN-- 2 1 -- 
#> + attr: name (v/c)
#> + edge from c9d31b0 (vertex names):
#> [1] 1--2
create_manual_graph(1-2, 1-3, 2-3, 2-4, 3-5, 4-5, 4-6, 4-7, 5-6, 6-7, 8, 
  graph ="network")
#>  Network attributes:
#>   vertices = 8 
#>   directed = FALSE 
#>   hyper = FALSE 
#>   loops = FALSE 
#>   multiple = FALSE 
#>   bipartite = FALSE 
#>   total edges= 10 
#>     missing edges= 0 
#>     non-missing edges= 10 
#> 
#>  Vertex attribute names: 
#>     vertex.names 
#> 
#> No edge attributes
# directed graph
create_manual_graph(Sam-+Mary, Sam-+Tom, Mary++Tom)
#> IGRAPH c9d5713 DN-- 3 4 -- 
#> + attr: name (v/c)
#> + edges from c9d5713 (vertex names):
#> [1] Sam ->Mary Sam ->Tom  Mary->Tom  Tom ->Mary

# bipartite graph
g_bipartite <- create_manual_graph(# listing organizations
      organization1:organization2:organization3,  
      # listing projects
      project1:project2,
      # specifying connections between organizations and projects 
      organization1:organization2 - project1, 
      organization2:organization3 - project2)
# note that the graph is bipartite mathematically, but the graph object 
# 'does not know' that yet.
snafun::extract_vertex_names(g_bipartite)
#> [1] "organization1" "organization2" "organization3" "project1"     
#> [5] "project2"     
g_bipartite <- snafun::add_vertex_attributes(g_bipartite, "type", 
  value = grepl(pattern = "^project", x = snafun::extract_vertex_names(g_bipartite)))
snafun::is_bipartite(g_bipartite)
#> [1] TRUE


# If you have many disconnected components in the graph, separate them with commas. 
# You can also give isolate vertices.
create_manual_graph( A--B, C--D, E--F, G--H, I, J, K )
#> IGRAPH c9d7ce1 UN-- 11 4 -- 
#> + attr: name (v/c)
#> + edges from c9d7ce1 (vertex names):
#> [1] A--B C--D E--F G--H

# The ‘:’ operator can be used to define vertex sets. 
# If an edge operator connects two vertex sets then every vertex from the first 
# set will be connected to every vertex in the second set. 
# The following form creates a full graph, including loop edges:
create_manual_graph( A:B:C:D -- A:B:C:D )
#> IGRAPH c9d8b9e UN-- 4 6 -- 
#> + attr: name (v/c)
#> + edges from c9d8b9e (vertex names):
#> [1] A--B A--C A--D B--C B--D C--D

# In directed graphs, edges will be created only if the edge operator includes 
# a arrow head (‘+’) at the end of the edge:
create_manual_graph( A -+ B -+ C )
#> IGRAPH c9d982d DN-- 3 2 -- 
#> + attr: name (v/c)
#> + edges from c9d982d (vertex names):
#> [1] A->B B->C
create_manual_graph( A +- B -+ C )
#> IGRAPH c9da507 DN-- 3 2 -- 
#> + attr: name (v/c)
#> + edges from c9da507 (vertex names):
#> [1] B->A B->C
create_manual_graph( A +- B -- C )
#> IGRAPH c9db2b9 DN-- 3 1 -- 
#> + attr: name (v/c)
#> + edge from c9db2b9 (vertex names):
#> [1] B->A
# Thus in the third example no edge is created between vertices B and C.


# Mutual edges can be also created with a simple edge operator:
create_manual_graph( A +-+ B +---+ C ++ D + E)
#> IGRAPH c9dbf68 DN-- 5 8 -- 
#> + attr: name (v/c)
#> + edges from c9dbf68 (vertex names):
#> [1] A->B B->A B->C C->B C->D D->C D->E E->D
# Note again that the length of the edge operators is arbitrary.

# If the vertex names include spaces or other special characters then you 
# need to quote them:
create_manual_graph( "this is" +- "a silly" -+ "graph here" )
#> IGRAPH c9dcd27 DN-- 3 2 -- 
#> + attr: name (v/c)
#> + edges from c9dcd27 (vertex names):
#> [1] a silly->this is    a silly->graph here
# You can include any character in the vertex names this way, 
# even ‘+’ and ‘-’ characters.


# More examples
# A simple undirected graph
g <- create_manual_graph(
  Alice - Bob - Cecil - Alice,
  Daniel - Cecil - Eugene,
  Cecil - Gordon
)
g
#> IGRAPH c9ddb4d UN-- 6 6 -- 
#> + attr: name (v/c)
#> + edges from c9ddb4d (vertex names):
#> [1] Alice--Bob    Alice--Cecil  Bob  --Cecil  Cecil--Daniel Cecil--Eugene
#> [6] Cecil--Gordon

# Another undirected graph, ":" notation
g2 <- create_manual_graph(Alice - Bob:Cecil:Daniel, Cecil:Daniel - Eugene:Gordon)
g2
#> IGRAPH c9dea39 UN-- 6 7 -- 
#> + attr: name (v/c)
#> + edges from c9dea39 (vertex names):
#> [1] Alice --Bob    Alice --Cecil  Alice --Daniel Cecil --Eugene Cecil --Gordon
#> [6] Daniel--Eugene Daniel--Gordon

# A directed graph
g3 <- create_manual_graph(
  Alice +-+ Bob --+ Cecil +-- Daniel,
  Eugene --+ Gordon:Helen
)
g3
#> IGRAPH c9dfab3 DN-- 7 6 -- 
#> + attr: name (v/c)
#> + edges from c9dfab3 (vertex names):
#> [1] Alice ->Bob    Bob   ->Alice  Bob   ->Cecil  Daniel->Cecil  Eugene->Gordon
#> [6] Eugene->Helen 

# A graph with isolate vertices
g4 <- create_manual_graph(Alice -- Bob -- Daniel, Cecil:Gordon, Helen)
g4
#> IGRAPH c9e0bb9 UN-- 6 2 -- 
#> + attr: name (v/c)
#> + edges from c9e0bb9 (vertex names):
#> [1] Alice--Bob    Bob  --Daniel
snafun::extract_vertex_names(g4)
#> [1] "Alice"  "Bob"    "Daniel" "Cecil"  "Gordon" "Helen" 

# "Arrows" can be arbitrarily long
g5 <- create_manual_graph(Alice +---------+ Bob)
g5
#> IGRAPH c9e23fd DN-- 2 2 -- 
#> + attr: name (v/c)
#> + edges from c9e23fd (vertex names):
#> [1] Alice->Bob   Bob  ->Alice

# Special vertex names
g6 <- create_manual_graph("+" -- "-", "*" -- "/", "%%" -- "%/%")
g6
#> IGRAPH c9e3770 UN-- 6 3 -- 
#> + attr: name (v/c)
#> + edges from c9e3770 (vertex names):
#> [1] + ---   * --/   %%--%/%