We will continue to use the data file from yesterday, WorkshopNetwork.rds.
library(tidyverse) #tools for cleaning data library(igraph) #package for doing network analysislibrary(tidygraph) #tools for doing tidy networkslibrary(here) #tools for project-based workflowlibrary(ggraph) #plotting tools for networkslibrary(boot) #to do resampling
gr <- readRDS(here("data", "WorkshopNetwork.rds"))
While we are at it we might as well set a layout.
```rGrLayout <- create_layout(gr, layout = "kk")ggraph(GrLayout) + geom_edge_link() + geom_node_point() + theme(legend.position="bottom")```
These are fairly easy to calculate
These require a bit more work
This is a list of many common graph metrics used in tidygraph https://rdrr.io/cran/tidygraph/man/graph_measures.html
Here is density
edge_density(gr)
## [1] 0.02853107
Here is diameter, note that with tidygraph you have to use the with_graph() function, and specify the graph and function graph_diameter()
with_graph(gr, graph_diameter())
## [1] 6
Here is density
edge_density(gr)
## [1] 0.02853107
Here is diameter, note that with tidygraph you have to use the with_graph() function, and specify the graph and function graph_diameter()
with_graph(gr, graph_diameter())
## [1] 6
So?
with_graph(gr, graph_reciprocity()) #Reciprocity
## [1] 0.2142857
transitivity(gr) #Transitivity
## [1] 0.2669492
with_graph(gr, graph_mean_dist()) #Average Distance
## [1] 1.88125
GrFunction <- function(gr){ Giant = max(clusters(gr)$csize) AveDeg = gr %>% activate(nodes) %>% mutate(Deg = centrality_degree( mode = 'total') ) %>% select(Deg) %>% as_tibble() %>% summarise(AveDeg = mean(Deg)) df = tibble(Giant, AveDeg ) return(df)}
GrFunction(gr)
## # A tibble: 1 × 2## Giant AveDeg## <dbl> <dbl>## 1 30 3.37
GrFunction(gr)
## # A tibble: 1 × 2## Giant AveDeg## <dbl> <dbl>## 1 30 3.37
Giant = 30, there are 30 people connected in the largest component.
AveDeg, the average person has 3.37 incoming or outgoing edges
Whole networks sometimes have regions which are different.
Perhaps you want to know, are there groups that have some common characteristic?
So we need a way to look for groups within our network.
```r#First generate a grouping based on random assignment.gr %>% activate(nodes) %>% mutate(RandGroup = sample(1:2,60, replace = TRUE)) -> gr```
```rGrLayout <- create_layout(gr, layout = "kk")ggraph(GrLayout) + geom_edge_link() + geom_node_point(aes(color = factor(RandGroup))) + theme(legend.position="bottom")```
Modularity is a metric that tells us whether there is a way to break up our network into chunks which have more connections within the chunk than from the chunk to other parts of the network.
with_graph(gr, graph_modularity(group = RandGroup)) #Modularity
## [1] 0.01470444
So the modularity of our network with nodes randomly assigned to groups is .12
This means that based on our grouping, there is a very slightly greater proportion of links between members of the group than across groups.
We'll use Walktrap, in which a random surfer can jump node to node based on the presence of edges. Then the walktrap algorithm establishes community structure by finding the propensity to end up in a cluster of nodes.
There are two ways to do this...
#First generate a grouping based on the walktrap algorithm and add it to the graph.gr %>% activate(nodes) %>% mutate(WTGroup = group_walktrap()) -> gr#The other way is to just call the walktrap algorithm. wtg <- with_graph(gr, group_walktrap())wtg
## [1] 9 3 5 1 2 7 2 4 1 1 4 3 1 5 6 3 8 2 10 1 1 4 5 3 2## [26] 4 4 6 1 4 4 4 1 7 3 2 2 2 2 2 4 1 1 1 1 1 3 3 3 3## [51] 3 1 1 5 5 6 6 8 2 1
Now we can check the modularity with the walktrap defined communities
with_graph(gr, graph_modularity(group = WTGroup)) #Modularity
## [1] 0.7393393
So the modularity of our network with nodes assigned to groups via the walktrap algorithm is .74
This means that based on our grouping, there is a much greater proportion of links between members of the group than across groups.
This is the hard way to plot these.
```rGrLayout <- create_layout(gr, layout = "kk")ggraph(GrLayout) + geom_edge_link() + geom_node_point(aes(color = factor(WTGroup))) + theme(legend.position="bottom")```
This is the easy way to plot these (though not tidy).
```rwtg = cluster_walktrap(gr)modularity(wtg)plot(wtg, gr)```
## [1] 0.6267523
Here are the community detection algorithms available to you in tidygraph. https://tidygraph.data-imaginist.com/reference/index.html#community-detection
We will continue to use the data file from yesterday, WorkshopNetwork.rds.
library(tidyverse) #tools for cleaning data library(igraph) #package for doing network analysislibrary(tidygraph) #tools for doing tidy networkslibrary(here) #tools for project-based workflowlibrary(ggraph) #plotting tools for networkslibrary(boot) #to do resampling
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |