library(linkcomm)
## Loading required package: igraph
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
## Loading required package: RColorBrewer
##
## Welcome to linkcomm version 1.0-11
##
## For a step-by-step guide to using linkcomm functions:
## > vignette(topic = "linkcomm", package = "linkcomm")
## To run an interactive demo:
## > demo(topic = "linkcomm", package = "linkcomm")
## To cite, see:
## > citation("linkcomm")
## NOTE: To use linkcomm, you require read and write permissions in the current directory (see: help("getwd"), help("setwd"))
lesmiserables <-lesmiserables
library(igraph)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:igraph':
##
## as_data_frame, groups, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(intergraph)
library(networkD3)
library(data.table)
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
##
## between, first, last
set.seed(123)
simpleNetwork(lesmiserables, linkDistance = 200, fontSize = 6, opacity = 0.9, zoom = TRUE)
the graph illustrate the nodes (characters) and the links between them indicates if they appeared in the same chapter. the graph look connected and most characters seems to appear together mostly, except few charcters that are only connected to other charcter in one chapter.
lc <- graph_from_data_frame(lesmiserables, directed = FALSE)
lc
## IGRAPH b0f681c UN-- 77 254 --
## + attr: name (v/c)
## + edges from b0f681c (vertex names):
## [1] Napoleon --Myriel MlleBaptistine--Myriel
## [3] MmeMagloire --Myriel MlleBaptistine--MmeMagloire
## [5] CountessDeLo --Myriel Geborand --Myriel
## [7] Champtercier --Myriel Cravatte --Myriel
## [9] Count --Myriel OldMan --Myriel
## [11] Valjean --Labarre MmeMagloire --Valjean
## [13] MlleBaptistine--Valjean Valjean --Myriel
## [15] Valjean --Marguerite Valjean --MmeDeR
## + ... omitted several edges
head(V(lc))
## + 6/77 vertices, named, from b0f681c:
## [1] Napoleon MlleBaptistine MmeMagloire CountessDeLo
## [5] Geborand Champtercier
edge_density(lc)
## [1] 0.08680793
the density of the ntwork is 0.0868. A network’s density is the number of connections(edges) divided by the number of possible connections.that is, describes the portion of the potential connections in a network that are actual connections a density of 8% indicate that there are not many possible connections between charcters.
centr_degree(lc)$res
## [1] 1 3 3 1 1 1 1 1 1 36 2 1 1 1 7 7 7 7 7 7 15 11 16
## [24] 11 17 4 8 2 4 1 2 6 6 6 6 6 3 1 11 3 3 2 1 2 22 7
## [47] 2 7 2 1 4 19 2 11 15 11 9 11 13 12 13 12 10 1 10 10 10 9 3
## [70] 2 2 7 7 10 1 9 1
V(lc)$degree <- centr_degree(lc)$res
V(lc)$degree
## [1] 1 3 3 1 1 1 1 1 1 36 2 1 1 1 7 7 7 7 7 7 15 11 16
## [24] 11 17 4 8 2 4 1 2 6 6 6 6 6 3 1 11 3 3 2 1 2 22 7
## [47] 2 7 2 1 4 19 2 11 15 11 9 11 13 12 13 12 10 1 10 10 10 9 3
## [70] 2 2 7 7 10 1 9 1
cent <- data.table(arrange(data_frame(person=V(lc)$name, degree_centrality=V(lc)$degree), desc(degree_centrality)))
head(cent)
## person degree_centrality
## 1: Valjean 36
## 2: Gavroche 22
## 3: Marius 19
## 4: Javert 17
## 5: Thenardier 16
## 6: Fantine 15
centralitymeasures the extent to which an individual interacts with other individuals in the network. The more an individual connects to others in a network, the greater their centrality in the network. Valjean and Gavroche are the most central charcters to the network.
set.seed(123)
btw=cluster_edge_betweenness(lc, weights = E(lc)$weight, directed=F)
# Plot a hierarchical community structure
plot_dendrogram(btw, mode="hclust", main="Newman-Girvan community detection")
Betweenness centrality represents the degree of which nodes stand between each other. In other words,it measures centrality in a graph based on shortest paths. the betweenness centrality for each node is the number of the shortest paths that passes through the node. based on the dendogram above, it shows the level of the divisions based on the Betweenness centrality in which the communities at one level are completely contained within the larger communities at all higher levels. It shows a selection of different possibilities ranging from just a few large communities at the top to many small communities at the bottom.
set.seed(1234)
plot(btw, lc, layout=layout.fruchterman.reingold(lc), vertex.size=10, main="Newman-Girvan community detection")
length(btw)
## [1] 11
the Newman-Girvan community detection using betweenness centrality detected 11 communities. It can be seen in the graph by looking at the circles with differennt colors and shades. there are large communities and smaller communities with 2 or 1 nodes.