Generating multiple network graphs with attributes from csv with ggraph, tidygraph in R

82 views Asked by At

I have three csv files which looks like this:

node <- data.frame(
object = c("2-1887", "2-1887", "2-1887", "2-1887", "2-1887", "2-1887", "2-1887", "4-1889", "4-1889", "4-1889", "4-1889", "4-1889", "4-1889", "4-1889"),
id = c(1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7),
function = c("f", "r", "r", "c", "s", "r", "k", "f", "r", "s", "r", "r", "r", "t"))

edge <- data.frame(
object = c("2-1887", "2-1887", "2-1887", "2-1887", "2-1887", "2-1887", "2-1887", "4-1889", "4-1889", "4-1889", "4-1889", "4-1889", "4-1889", "4-1889"),
from = c(1, 2, 3, 3, 4, 5, 6, 1, 2, 2, 3, 4, 5, 6),
to = c(2, 3, 4, 6, 6, 6, 7, 2, 3, 4, 4, 5, 6, 7))

meta <- data.frame(
object = c("2-1887", "4-1889"),
year = c(2000, 2022))

Actually this data set contains two different graphs grouped by object.

Is anyone who can advice how to generate two graphs grouped by object ("2-1887" and "4-1889") using tbl_graph in ggraph/tidygraph? It is nice if I can keep these object names in graph attributes. I suppose I can do it with purrr or apply (any methods are welcomed) but I do not know appropriate data structure (list?) for further graph analyses.

I also need to add meta information (graph attributes) to each graph from an object meta. Currently I can add graph attributes manually using such as graph$year <- 2000 but I need to automate it.

Thank you in advance for your kind support.

1

There are 1 answers

1
Onyambu On BEST ANSWER
edge %>%
  group_by(object) %>%
  summarise(graph = list(igraph::graph_from_data_frame(cur_data()))) %>%
  left_join(meta)

# A tibble: 2 x 3
  object graph     year
  <chr>  <list>   <dbl>
1 2-1887 <igraph>  2000
2 4-1889 <igraph>  2022