The union of two or more graphs are created. The graphs may have identical or overlapping vertex sets.

make_union(..., byname = "auto")

Arguments

...

Graph objects or lists of graph objects. Allowed graph classes are igraph, network, matrix.

byname

A logical scalar, or the character scalar "auto". Whether to perform the operation based on symbolic vertex names. If it is "auto", that means TRUE if all graphs are named and FALSE otherwise. A warning is generated if "auto" and some (but not all) graphs are named.

Value

a graph object of class igraph, network, or matrix.

Details

make_union() creates the union of two or more graphs. The graphs to be combined can be of class igraph, network, or matrix. Edges which are included in at least one graph will be part of the new graph.

If the byname argument is TRUE (or "auto" and all graphs are named), the operation is performed on symbolic vertex names instead of the internal numeric vertex ids.

make_union() keeps the attributes of all graphs. All graph, vertex and edge attributes are copied to the result. If an attribute is present in multiple graphs and would result a name clash, then this attribute is renamed by adding suffixes: _1, _2, etc.

The name vertex attribute is treated specially if the operation is performed based on symbolic vertex names. In this case name must be present in all graphs, and it is not renamed in the result graph.

An error is generated if some input graphs are directed and others are undirected.

The actual union is performed through the union function, which also is the basis of the documentation of this function.

What make_union() mainly adds is the opportunity to combine graphs of different classes, not just of class igraph. In fact, the intrepid can even combine graphs of different types together (although that will rarely be useful). The output will be a graph with the same class as that of the first graph passed to this function.

Examples

net1 <- snafun::create_manual_graph(
  D - A:B:F:G, A - C - F - A, B - E - G - B, A - B, F - G,
  H - F:G, H - I - J
)
net2 <- snafun::create_manual_graph(D - A:F:Y, B - A - X - F - H - Z, F - Y)
net1_net2 <- make_union(net1, net2)
#> Error in eval(kall[[1]]): object 'net1' not found
if (FALSE) { # \dontrun{
  opar <- par()
  par("mfrow")
  snafun::plot(net1)
  snafun::plot(net2)
  snafun::plot(net1_net2)
  snafun::plot(igraph::union(net1, net2))
  par(opar)
} # }

make_union(snafun::to_network(net1), net2)
#> Error in eval(kall[[1]]): object 'net1' not found
make_union(net1, snafun::to_network(net2))
#> Error in eval(kall[[1]]): object 'net1' not found
make_union(snafun::to_matrix(net1), net2)
#> Error in eval(kall[[1]]): object 'net1' not found
make_union(net1, snafun::to_matrix(net2))
#> Error in eval(kall[[1]]): object 'net1' not found

g <- snafun::create_manual_graph(1-2, 1-3, 2-3, 2-4, 3-5, 4-5, 4-6, 4-7, 5-6, 6-7)
# Obtaining subgraphs
h <- snafun::extract_subgraph(g, v_to_keep = 1:5) # subgraph only contains vertices 1 to 5 
k <- snafun::create_manual_graph(4-6, 4-7, 5-6, 6-7) # create a new graph "k"
h_k <- make_union(h,k) # combine "h" and "k" graphs 
#> Error in eval(kall[[1]]): object 'h' not found
if (FALSE) { # \dontrun{
  # Plotting the graphs 
  opar <- par()
  par(mfrow=c(1, 3), mar = c(0,0,0,1))
  set.seed(689)
  snafun::plot(h)
  snafun::plot(k)
  snafun::plot(h_k)
  par(opar)
} # }

# more than two graphs
# Note: the vertices end up in different orders, but the graphs are the same
net3 <- snafun::create_manual_graph(H-I:K, I-E, X-Q-Z)
snafun::make_union(net1, net2, net3)
#> Error in eval(kall[[1]]): object 'net1' not found
snafun::make_union(net1, net3, net2)
#> Error in eval(kall[[1]]): object 'net1' not found
snafun::make_union(net3, net2, net1)
#> Error in eval(kall[[1]]): object 'net3' not found