I am trying out tidygraph on networks coming from different experimental treatments and am mostly interested to get out graph-wide metrics and potentially also node-level metrics. I can't seem to be able to get my head around the way tidygraph works.
I am using R v3.5 and tidygraph v1.1.
My data are structured in that way:
dat <- data.frame(Treatment = rep(c("A","B"),each = 2),
from = c("sp1","sp2","sp1","sp2"),
to = c("sp2","sp3","sp3","sp3"),
weight = runif(4))
If I want to get per treatment a graph-wide metric like the diameter I would be tempted to do:
dat %>%
as_tbl_graph() %>%
activate(edges) %>%
group_by(Treatment) %>%
mutate(Diameter = graph_diameter(weights = weight))
But I am unsure about the result as the diameters are then given for each edges while I would be expecting one measure per treatment (per graph).
Similarly if I want to derive some metrics like the connectivity of each node for each treatment this appears to be not so straightforward since the treatment variables is dropped from the nodes table. I have been trying various hacks like pasting the treatment IDs to the from and to columns before calling as_tbl_graph() along these lines:
dat %>%
mutate(from = paste(from, Treatment, sep = "_"),
to = paste(to, Treatment, sep = "_")) %>%
as_tbl_graph() %>%
mutate(Treatment = substr(name, 5, 5), name = substr(name, 1, 3)) %>%
group_by(Treatment) %>%
mutate(Centrality = centrality_betweenness())
But I got errors that the resulting vectors were of the wrong size (6 instead of 3 or 1).
Is there a way with tidygraph to derive group-level graph-wide and node-level metrics?