I would like to draw network plots using tidygraph
and ggraph
.
I have a larger tibble with items connected via from
and to
.
Some of the trees are connected (a0
and b0
in the example).
I would like to:
- Count the number of independent trees
- Calculate the average maximum edges=connections per independent tree. The average maximum edges should be calculated "downstreams", i.e. from
a0
tok2
ora4
nota0
tob0
in the example data.
Example:
library(tidygraph)
library(igraph)
library(ggraph)
library(tidyverse)
# make edges
edges<- tibble(from = c("a0","a1","a2","a3","b0","b1","c0","c1","a2","k1"),
to = c("a1","a2","a3","a4","b1","a3","c1","c2","k1","k2"))
# makenodes
nodes <- unique(c(edges$from,edges$to))
tibble(node=nodes,
label=nodes) -> nodes
# make correct dataframe
routes_igraph <- graph_from_data_frame(d = edges,
vertices = nodes,
directed = TRUE)
routes_tidy <- as_tbl_graph(routes_igraph)
#plot network
ggraph(routes_tidy, layout = "tree") +
geom_edge_link() +
geom_node_point() +
theme_graph() +
geom_node_text(aes(label = label), repel = TRUE)
Created on 2023-04-16 with reprex v2.0.2
Desired output
Number of independent trees of the given
edges
andnodes
:2
Average maximum edges per independen trees:
3.5, 2