# How to add an edge between 2 graphs in igraph R

**URL:** <https://igraph.discourse.group/t/how-to-add-an-edge-between-2-graphs-in-igraph-r/683>\
**Category:** Usage\
**Tags:** R\
**Created:** [27 March 2021 01:48 UTC](https://igraph.discourse.group/t/how-to-add-an-edge-between-2-graphs-in-igraph-r/683 "2021-03-27T01:48:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![akib62](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@akib62](https://igraph.discourse.group/u/akib62)\
**Post date:** [27 March 2021 01:48 UTC](https://igraph.discourse.group/t/how-to-add-an-edge-between-2-graphs-in-igraph-r/683/1 "2021-03-27T01:48:19Z")

</div>

I have a graph contains `4 components`. Now, I want to add an edge between components of the graph.

```
graph1 <- graph_from_data_frame(g, directed = FALSE)
E(graph1)$weight <- g$new_ssp
plot(graph1, vertex.label = V(graph1)$name)
cl <- components(graph1) # you will gate 4 components

```

 ![2021-03-26_21-26](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/dd80d940194e8890ec9c744c3179368b722c60e7.png)

I am decomposing the graph  
`dg <- decompose.graph(graph1)`

Now, if I want to add an edge between `component-1 and component-2`. After adding an edge between `component-1 and component-2`, the new graph will contain `3 component 1st (component-1 and component-2 as a 1 component because we added 1 edge), 2nd (component-3 from the main graph), and 3rd (component-4 from the main graph)`

What I have to do?

Reproducible Data:

```auto
g <- structure(list(query = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 4L, 
                                   5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("ID_00104", 
                                                                                                       "ID_00136", "ID_00169", "ID_00178", "ID_00180"), class = "factor"), 
               target = structure(c(16L, 19L, 20L, 1L, 9L, 9L, 6L, 11L, 
                                    13L, 15L, 4L, 8L, 10L, 14L, 2L, 3L, 5L, 7L, 12L, 17L, 18L
               ), .Label = c("ID_00169", "ID_00288", "ID_00324", "ID_00394", 
                             "ID_00663", "ID_00790", "ID_00846", "ID_00860", "ID_00910", "ID_00959", 
                             "ID_01013", "ID_01047", "ID_01130", "ID_01222", "ID_01260", "ID_06663", 
                             "ID_06781", "ID_06786", "ID_06791", "ID_09099"), class = "factor"), 
               new_ssp = c(0.654172560113154, 0.919096895578551, 0.925821596244131, 
                           0.860406091370558, 0.746376811594203, 0.767195767195767, 
                           0.830379746835443, 0.661577608142494, 0.707520891364902, 
                           0.908193484698914, 0.657118786857624, 0.687664041994751, 
                           0.68586387434555, 0.874513618677043, 0.836646499567848, 0.618361836183618, 
                           0.684163701067616, 0.914728682170543, 0.876297577854671, 
                           0.732707087959009, 0.773116438356164)), row.names = c(NA, 
                                                                                 -21L), class = "data.frame")

```

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [29 March 2021 08:56 UTC](https://igraph.discourse.group/t/how-to-add-an-edge-between-2-graphs-in-igraph-r/683/2 "2021-03-29T08:56:04Z")

</div>

It is not possible to add an edge between two different graph objects. Edges can only be added to a single graph.

You can combine multiple graphs using `disjoint_union`, but in your case it would make more sense not to break up the graph into multiple graph objects in the first place.

---

<div class="post-metadata">

**Author:** ![akib62](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@akib62](https://igraph.discourse.group/u/akib62)\
**Post date:** [3 April 2021 01:49 UTC](https://igraph.discourse.group/t/how-to-add-an-edge-between-2-graphs-in-igraph-r/683/3 "2021-04-03T01:49:24Z")

</div>

[Solution](https://stackoverflow.com/questions/66826023/how-to-add-a-edges-between-component-of-a-graph-in-igraph-r)
