Reputation: 5935
I have the following network graph:
library(tidyverse)
library(igraph)
set.seed(123)
n=5
data = tibble(d = paste(1:n))
relations = data.frame(tibble(
from = sample(data$d),
to = lead(from, default=from[1]),
))
graph = graph_from_data_frame(relations, directed=T, vertices = data)
V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")
plot(graph, layout=layout.circle, edge.arrow.size = 0.2)
I want to connect each Node to every Node on this graph - I can do this manually by redefining the "relations" data frame:
relations_1 = data.frame("from" = c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5), "to" = c(2,3,4,5,1,3,4,5,1,2,4,5,1,2,3,5,1,2,3,4))
Then, I can re-run the network graph:
graph = graph_from_data_frame(relations_1, directed=T, vertices = data)
V(graph)$color <- ifelse(data$d == relations_1$from[1], "red", "orange")
plot(graph, layout=layout.circle, edge.arrow.size = 0.2)
Thank you!
Upvotes: 3
Views: 623
Reputation: 102710
If you depart from graph
, you can try the following code using add_edges
+ get.adjacency
graph %>%
add_edges(c(t(which(`diag<-`(get.adjacency(., sparse = FALSE), 1) != 1, arr.ind = TRUE))))
which adds all missing edges while retaining the attributes from your original graph
object, and you will see
Upvotes: 1
Reputation: 3194
An solution using igraph::make_full_graph
:
relations %>%
with(union(from,to)) %>%
{
G <- make_full_graph(length(.), directed = TRUE)
V(G)$name <- .
G
}
Upvotes: 2
Reputation: 6132
You could just update relations
using complete
, and than filter out the rows where from
is equal to to
, which gives arrows from a node to itself.
relations <- relations %>%
complete(from, to) %>%
dplyr::filter(from != to)
Upvotes: 2