This demo will provide
- a brief introduction to web scraping using Rvest package
- a brief introduction to the concept of graph and network analysis using R igraph package
- a brief introduction on the metrics used to perform network analysis (such as degree, betweeness, closeness and eigenvector centrality)
- investigation into which MRT station is an important node in the Singapore MRT network
Installing the rvest package to scrape the list of MRT stations from wikipedia
install.packages("rvest")
library(rvest)
Installing package into '/home/nbcommon/R'
(as 'lib' is unspecified)
also installing the dependencies 'xml2', 'selectr'
Loading required package: xml2
Before we can scrape the MRT stations data, we would need to ascertain the correct XPath to use.
- navigate to the wikipedia page and fire up the developer tool (F12 for windows or right-click > Inspect)
- copy the table XPath
Now we are ready to do some real and dirty work, scraping and cleaning the data!
url <- "https://en.wikipedia.org/wiki/List_of_Singapore_MRT_stations"
mrt_stn <- url %>%
read_html() %>%
#paste the copied xpath: html_nodes(xpath here)
html_nodes(xpath='//*[@id="mw-content-text"]/div/table[2]') %>%
html_table(fill = TRUE)
mrt <- mrt_stn[[1]]
mrt <- mrt[,c(1:2,5,7:8)]
names(mrt) <- c("Code","Name","Opening","Status","Location")
mrt <- subset(mrt,Code != Name)
mrt <- mrt[2:nrow(mrt),]
mrt$Code <- substr(mrt$Code, 1, 4)
mrt$Code <- iconv(mrt$Code, "ASCII", "UTF-8", sub="")
mrt$Name <- gsub('\\[.\\]',"",mrt$Name)
mrt <- mrt[mrt$Name != 'Reserved Station',]
mrt <- mrt[mrt$Name != 'Punggol Coast',]
mrt <- mrt[mrt$Status != 'TBA',]
i. preparing the North-South Line (NSL) edgelist
ns_df <- mrt[substr(mrt$Code,1,2) == 'NS',]
sourceList <- ""
targetList <- ""
for (i in 1:nrow(ns_df)-1) {
sourceList[i] <- ns_df$Name[i]
targetList[i] <- ns_df$Name[i+1]
}
ns_edgelist <- data.frame(sourceList, targetList, "NSL")
names(ns_edgelist) <- c("source", "target", "network")
ii. preparing the East-West Line (EWL) edgelist
ew_df <- mrt[substr(mrt$Code,1,2) == 'EW',]
sourceList <- ""
targetList <- ""
for (i in 1:nrow(ew_df)-1) {
sourceList[i] <- ew_df$Name[i]
targetList[i] <- ew_df$Name[i+1]
}
ew_edgelist <- data.frame(sourceList, targetList, "EWL")
names(ew_edgelist) <- c("source", "target", "network")
iii. preparing the Changi Airport Branch Line (CAL) edgelist
cg_df <- mrt[substr(mrt$Code,1,2) == 'CG',]
sourceList <- ""
targetList <- ""
for (i in 1:nrow(cg_df)-1) {
sourceList[i] <- cg_df$Name[i]
targetList[i] <- cg_df$Name[i+1]
}
cg_edgelist <- data.frame(sourceList, targetList, "CAL")
names(cg_edgelist) <- c("source", "target", "network")
iv. preparing the North-East Line (NEL) edgelist
ne_df <- mrt[substr(mrt$Code,1,2) == 'NE',]
sourceList <- ""
targetList <- ""
for (i in 1:nrow(ne_df)-1) {
sourceList[i] <- ne_df$Name[i]
targetList[i] <- ne_df$Name[i+1]
}
ne_edgelist <- data.frame(sourceList, targetList, "NEL")
names(ne_edgelist) <- c("source", "target", "network")
v. preparing the Circle Line (CCL) edgelist
cc_df <- mrt[substr(mrt$Code,1,2) == 'CC',]
sourceList <- ""
targetList <- ""
for (i in 1:nrow(cc_df)-1) {
sourceList[i] <- cc_df$Name[i]
targetList[i] <- cc_df$Name[i+1]
}
cc_edgelist <- data.frame(sourceList, targetList, "CCL")
names(cc_edgelist) <- c("source", "target", "network")
vi. preparing the Downtown Line (DTL) edgelist
dt_df <- mrt[substr(mrt$Code,1,2) == 'DT',]
sourceList <- ""
targetList <- ""
for (i in 1:nrow(dt_df)-1) {
sourceList[i] <- dt_df$Name[i]
targetList[i] <- dt_df$Name[i+1]
}
dt_edgelist <- data.frame(sourceList, targetList, "DTL")
names(dt_edgelist) <- c("source", "target", "network")
mrt_edgelist <- rbind(ns_edgelist,ew_edgelist,cg_edgelist,ne_edgelist,cc_edgelist,dt_edgelist)
mrt_edgelist
source | target | network |
---|---|---|
Jurong East | Bukit Batok | NSL |
Bukit Batok | Bukit Gombak | NSL |
Bukit Gombak | Choa Chu Kang | NSL |
Choa Chu Kang | Yew Tee | NSL |
Yew Tee | Kranji | NSL |
Kranji | Marsiling | NSL |
Marsiling | Woodlands | NSL |
Woodlands | Admiralty | NSL |
Admiralty | Sembawang | NSL |
Sembawang | Yishun | NSL |
Yishun | Khatib | NSL |
Khatib | Yio Chu Kang | NSL |
Yio Chu Kang | Ang Mo Kio | NSL |
Ang Mo Kio | Bishan | NSL |
Bishan | Braddell | NSL |
Braddell | Toa Payoh | NSL |
Toa Payoh | Novena | NSL |
Novena | Newton | NSL |
Newton | Orchard | NSL |
Orchard | Somerset | NSL |
Somerset | Dhoby Ghaut | NSL |
Dhoby Ghaut | City Hall | NSL |
City Hall | Raffles Place | NSL |
Raffles Place | Marina Bay | NSL |
Marina Bay | Marina South Pier | NSL |
Pasir Ris | Tampines | EWL |
Tampines | Simei | EWL |
Simei | Tanah Merah | EWL |
Tanah Merah | Bedok | EWL |
Bedok | Kembangan | EWL |
... | ... | ... |
Beauty World | King Albert Park | DTL |
King Albert Park | Sixth Avenue | DTL |
Sixth Avenue | Tan Kah Kee | DTL |
Tan Kah Kee | Botanic Gardens | DTL |
Botanic Gardens | Stevens | DTL |
Stevens | Newton | DTL |
Newton | Little India | DTL |
Little India | Rochor | DTL |
Rochor | Bugis | DTL |
Bugis | Promenade | DTL |
Promenade | Bayfront | DTL |
Bayfront | Downtown | DTL |
Downtown | Telok Ayer | DTL |
Telok Ayer | Chinatown | DTL |
Chinatown | Fort Canning | DTL |
Fort Canning | Bencoolen | DTL |
Bencoolen | Jalan Besar | DTL |
Jalan Besar | Bendemeer | DTL |
Bendemeer | Geylang Bahru | DTL |
Geylang Bahru | Mattar | DTL |
Mattar | MacPherson | DTL |
MacPherson | Ubi | DTL |
Ubi | Kaki Bukit | DTL |
Kaki Bukit | Bedok North | DTL |
Bedok North | Bedok Reservoir | DTL |
Bedok Reservoir | Tampines West | DTL |
Tampines West | Tampines | DTL |
Tampines | Tampines East | DTL |
Tampines East | Upper Changi | DTL |
Upper Changi | Expo | DTL |
mrt_edgelist$target <- as.character(mrt_edgelist$target)
mrt_edgelist$source <- as.character(mrt_edgelist$source)
mrt_edgelist$network <- as.character(mrt_edgelist$network)
Closing the loop for the graph network
mrt_edgelist[nrow(mrt_edgelist)+1,] <- c("Bayfront","Marina Bay","CEL")
mrt_edgelist[nrow(mrt_edgelist)+1,] <- c("Bayfront","Promenade","CCL")
mrt_edgelist[nrow(mrt_edgelist)+1,] <- c("Tanah Merah","Expo","CAL")
Specifying this network to be a undirected graph network
mrt_edgelist$type <- "undirected"
Exporting the mrt edgelist to csv
write.csv(mrt_edgelist, file="mrt_edgelist.csv", row.names=F)
mrt_node <- mrt[substr(mrt$Code,1,2) != 'TE',]
names(mrt_node)[2] <- "id"
mrt_node$label <- mrt_node$id
# removing duplicated mrt names/nodes (nodes in a network should be unique)
mrt_nodes <- unique(mrt_node)
mrt_nodes <- mrt_nodes[!duplicated(mrt_nodes$id),]
mrt_nodes$Code <- substr(mrt_nodes$Code, 1, 2)
Exporting the mrt nodes to csv
write.csv(mrt_nodes, file="mrt_nodes.csv", row.names=F)
Installing the igraph package to do graph network analysis and plotting
install.packages("igraph")
library(igraph)
Installing package into '/home/nbcommon/R'
(as 'lib' is unspecified)
Attaching package: 'igraph'
The following objects are masked from 'package:stats':
decompose, spectrum
The following object is masked from 'package:base':
union
# renaming for igraph edgelist format
names(mrt_edgelist) <- c("from","to","network","type")
# rearranging for igraph nodelist format
mrt_nodes <- mrt_nodes[c(2,6,1,3,4,5)]
Setting up the graph network
g = graph.data.frame(mrt_edgelist, mrt_nodes, directed=F)
# Removing self loops
# g = simplify(g, remove.loops = T)
Checking if multiple edges exists in the graph network
any_multiple(g)
which_multiple(g)
#count_multiple(g)
#which_multiple(simplify(g))
#all(count_multiple(simplify(g)) == 1)
E(g)[38]
E(g)[135]
TRUE
+ 1/136 edge from d7df987 (vertex names):
[1] City Hall--Raffles Place
+ 1/136 edge from d7df987 (vertex names):
[1] Promenade--Bayfront
Removing multiple edges to create a simplified graph
simple_g <- g
simple_g <- delete_edges(simple_g,c(38,135))
any_multiple(simple_g)
FALSE
Displaying descriptive statistics of the graph network
V(simple_g)
+ 119/119 vertices, named, from 24e1b48:
[1] Jurong East Bukit Batok Bukit Gombak Choa Chu Kang
[5] Yew Tee Kranji Marsiling Woodlands
[9] Admiralty Sembawang Yishun Khatib
[13] Yio Chu Kang Ang Mo Kio Bishan Braddell
[17] Toa Payoh Novena Newton Orchard
[21] Somerset Dhoby Ghaut City Hall Raffles Place
[25] Marina Bay Marina South Pier Pasir Ris Tampines
[29] Simei Tanah Merah Bedok Kembangan
[33] Eunos Paya Lebar Aljunied Kallang
[37] Lavender Bugis Tanjong Pagar Outram Park
+ ... omitted several vertices
E(simple_g)
+ 134/134 edges from 24e1b48 (vertex names):
[1] Jurong East --Bukit Batok Bukit Batok --Bukit Gombak
[3] Bukit Gombak --Choa Chu Kang Choa Chu Kang--Yew Tee
[5] Yew Tee --Kranji Kranji --Marsiling
[7] Marsiling --Woodlands Woodlands --Admiralty
[9] Admiralty --Sembawang Sembawang --Yishun
[11] Yishun --Khatib Khatib --Yio Chu Kang
[13] Yio Chu Kang --Ang Mo Kio Ang Mo Kio --Bishan
[15] Bishan --Braddell Braddell --Toa Payoh
[17] Toa Payoh --Novena Novena --Newton
[19] Newton --Orchard Orchard --Somerset
+ ... omitted several edges
# Network Size (num of nodes and edges)
summary(simple_g)
# Network Density
graph.density(simple_g,loop=FALSE)
# greatest distance between any pair of vertices
diameter(simple_g)
# Average Path Length
mean_distance(simple_g, directed=F)
IGRAPH 24e1b48 UN-- 119 134 --
+ attr: name (v/c), label (v/c), Code (v/c), Opening (v/c), Status
| (v/c), Location (v/c), network (e/c), type (e/c)
0.0190856003418316
30
10.0371741917106
# Length of all paths in the graph
distances(simple_g)
Jurong East | Bukit Batok | Bukit Gombak | Choa Chu Kang | Yew Tee | Kranji | Marsiling | Woodlands | Admiralty | Sembawang | ... | Bendemeer | Geylang Bahru | Mattar | Ubi | Kaki Bukit | Bedok North | Bedok Reservoir | Tampines West | Tampines East | Upper Changi | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Jurong East | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ... | 13 | 14 | 15 | 15 | 16 | 17 | 18 | 19 | 21 | 21 |
Bukit Batok | 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ... | 14 | 15 | 16 | 16 | 17 | 18 | 19 | 20 | 22 | 22 |
Bukit Gombak | 2 | 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | ... | 15 | 16 | 17 | 17 | 18 | 19 | 20 | 21 | 23 | 23 |
Choa Chu Kang | 3 | 2 | 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | ... | 16 | 17 | 17 | 17 | 18 | 19 | 20 | 21 | 23 | 23 |
Yew Tee | 4 | 3 | 2 | 1 | 0 | 1 | 2 | 3 | 4 | 5 | ... | 17 | 17 | 16 | 16 | 17 | 18 | 19 | 20 | 22 | 22 |
Kranji | 5 | 4 | 3 | 2 | 1 | 0 | 1 | 2 | 3 | 4 | ... | 17 | 16 | 15 | 15 | 16 | 17 | 18 | 19 | 21 | 21 |
Marsiling | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 1 | 2 | 3 | ... | 16 | 15 | 14 | 14 | 15 | 16 | 17 | 18 | 20 | 20 |
Woodlands | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 1 | 2 | ... | 15 | 14 | 13 | 13 | 14 | 15 | 16 | 17 | 19 | 19 |
Admiralty | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 1 | ... | 14 | 13 | 12 | 12 | 13 | 14 | 15 | 16 | 18 | 18 |
Sembawang | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ... | 13 | 12 | 11 | 11 | 12 | 13 | 14 | 15 | 17 | 17 |
Yishun | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | ... | 12 | 11 | 10 | 10 | 11 | 12 | 13 | 14 | 16 | 16 |
Khatib | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | ... | 11 | 10 | 9 | 9 | 10 | 11 | 12 | 13 | 15 | 15 |
Yio Chu Kang | 11 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | ... | 10 | 9 | 8 | 8 | 9 | 10 | 11 | 12 | 14 | 14 |
Ang Mo Kio | 10 | 11 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | ... | 9 | 8 | 7 | 7 | 8 | 9 | 10 | 11 | 13 | 13 |
Bishan | 9 | 10 | 11 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | ... | 8 | 7 | 6 | 6 | 7 | 8 | 9 | 10 | 12 | 12 |
Braddell | 10 | 11 | 12 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | ... | 9 | 8 | 7 | 7 | 8 | 9 | 10 | 11 | 13 | 13 |
Toa Payoh | 10 | 11 | 12 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | ... | 10 | 9 | 8 | 8 | 9 | 10 | 11 | 12 | 14 | 14 |
Novena | 9 | 10 | 11 | 12 | 13 | 12 | 11 | 10 | 9 | 8 | ... | 9 | 10 | 9 | 9 | 10 | 11 | 12 | 13 | 15 | 14 |
Newton | 8 | 9 | 10 | 11 | 12 | 13 | 12 | 11 | 10 | 9 | ... | 8 | 9 | 9 | 9 | 10 | 11 | 12 | 13 | 14 | 13 |
Orchard | 9 | 10 | 11 | 12 | 13 | 14 | 13 | 12 | 11 | 10 | ... | 8 | 9 | 10 | 10 | 11 | 12 | 13 | 14 | 15 | 14 |
Somerset | 10 | 11 | 12 | 13 | 14 | 15 | 14 | 13 | 12 | 11 | ... | 7 | 8 | 9 | 9 | 10 | 11 | 12 | 13 | 14 | 13 |
Dhoby Ghaut | 10 | 11 | 12 | 13 | 14 | 15 | 14 | 13 | 12 | 11 | ... | 6 | 7 | 8 | 8 | 9 | 10 | 11 | 12 | 13 | 12 |
City Hall | 11 | 12 | 13 | 14 | 15 | 16 | 15 | 14 | 13 | 12 | ... | 7 | 8 | 7 | 7 | 8 | 9 | 10 | 11 | 12 | 11 |
Raffles Place | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 15 | 14 | 13 | ... | 7 | 8 | 8 | 8 | 9 | 10 | 11 | 12 | 13 | 12 |
Marina Bay | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 16 | 15 | 14 | ... | 8 | 9 | 9 | 9 | 10 | 11 | 12 | 13 | 14 | 13 |
Marina South Pier | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 17 | 16 | 15 | ... | 9 | 10 | 10 | 10 | 11 | 12 | 13 | 14 | 15 | 14 |
Pasir Ris | 21 | 22 | 23 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | ... | 10 | 9 | 8 | 6 | 5 | 4 | 3 | 2 | 2 | 3 |
Tampines | 20 | 21 | 22 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | ... | 9 | 8 | 7 | 5 | 4 | 3 | 2 | 1 | 1 | 2 |
Simei | 20 | 21 | 22 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | ... | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 2 | 3 |
Tanah Merah | 19 | 20 | 21 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | ... | 8 | 7 | 6 | 6 | 6 | 5 | 4 | 3 | 3 | 2 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | |
Kent Ridge | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 13 | ... | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 20 | 20 |
Haw Par Villa | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 14 | ... | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 20 | 19 |
Pasir Panjang | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 15 | ... | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 19 | 18 |
Labrador Park | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 16 | ... | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 18 | 17 |
Telok Blangah | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 16 | ... | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 17 | 16 |
Bayfront | 12 | 13 | 14 | 15 | 16 | 17 | 17 | 16 | 15 | 14 | ... | 7 | 8 | 8 | 8 | 9 | 10 | 11 | 12 | 13 | 12 |
Bukit Panjang | 13 | 14 | 15 | 16 | 17 | 18 | 18 | 17 | 16 | 15 | ... | 17 | 17 | 16 | 16 | 17 | 18 | 19 | 20 | 22 | 22 |
Cashew | 12 | 13 | 14 | 15 | 16 | 17 | 17 | 16 | 15 | 14 | ... | 16 | 16 | 15 | 15 | 16 | 17 | 18 | 19 | 21 | 21 |
Hillview | 11 | 12 | 13 | 14 | 15 | 16 | 16 | 15 | 14 | 13 | ... | 15 | 15 | 14 | 14 | 15 | 16 | 17 | 18 | 20 | 20 |
Beauty World | 10 | 11 | 12 | 13 | 14 | 15 | 15 | 14 | 13 | 12 | ... | 14 | 14 | 13 | 13 | 14 | 15 | 16 | 17 | 19 | 19 |
King Albert Park | 9 | 10 | 11 | 12 | 13 | 14 | 14 | 13 | 12 | 11 | ... | 13 | 13 | 12 | 12 | 13 | 14 | 15 | 16 | 18 | 18 |
Sixth Avenue | 8 | 9 | 10 | 11 | 12 | 13 | 13 | 12 | 11 | 10 | ... | 12 | 12 | 11 | 11 | 12 | 13 | 14 | 15 | 17 | 17 |
Tan Kah Kee | 7 | 8 | 9 | 10 | 11 | 12 | 12 | 11 | 10 | 9 | ... | 11 | 11 | 10 | 10 | 11 | 12 | 13 | 14 | 16 | 16 |
Stevens | 7 | 8 | 9 | 10 | 11 | 12 | 12 | 11 | 10 | 9 | ... | 9 | 10 | 10 | 10 | 11 | 12 | 13 | 14 | 15 | 14 |
Rochor | 10 | 11 | 12 | 13 | 14 | 15 | 14 | 13 | 12 | 11 | ... | 8 | 8 | 7 | 7 | 8 | 9 | 10 | 11 | 12 | 11 |
Downtown | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 17 | 16 | 15 | ... | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 13 |
Telok Ayer | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 16 | 15 | 14 | ... | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 15 | 14 |
Fort Canning | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 16 | 15 | 14 | ... | 3 | 4 | 5 | 7 | 8 | 9 | 10 | 11 | 13 | 13 |
Bencoolen | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 17 | 16 | 15 | ... | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 12 | 12 |
Jalan Besar | 12 | 13 | 14 | 15 | 16 | 17 | 17 | 16 | 15 | 14 | ... | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 11 | 11 |
Bendemeer | 13 | 14 | 15 | 16 | 17 | 17 | 16 | 15 | 14 | 13 | ... | 0 | 1 | 2 | 4 | 5 | 6 | 7 | 8 | 10 | 10 |
Geylang Bahru | 14 | 15 | 16 | 17 | 17 | 16 | 15 | 14 | 13 | 12 | ... | 1 | 0 | 1 | 3 | 4 | 5 | 6 | 7 | 9 | 9 |
Mattar | 15 | 16 | 17 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | ... | 2 | 1 | 0 | 2 | 3 | 4 | 5 | 6 | 8 | 8 |
Ubi | 15 | 16 | 17 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | ... | 4 | 3 | 2 | 0 | 1 | 2 | 3 | 4 | 6 | 7 |
Kaki Bukit | 16 | 17 | 18 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | ... | 5 | 4 | 3 | 1 | 0 | 1 | 2 | 3 | 5 | 6 |
Bedok North | 17 | 18 | 19 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | ... | 6 | 5 | 4 | 2 | 1 | 0 | 1 | 2 | 4 | 5 |
Bedok Reservoir | 18 | 19 | 20 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | ... | 7 | 6 | 5 | 3 | 2 | 1 | 0 | 1 | 3 | 4 |
Tampines West | 19 | 20 | 21 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | ... | 8 | 7 | 6 | 4 | 3 | 2 | 1 | 0 | 2 | 3 |
Tampines East | 21 | 22 | 23 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | ... | 10 | 9 | 8 | 6 | 5 | 4 | 3 | 2 | 0 | 1 |
Upper Changi | 21 | 22 | 23 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | ... | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 1 | 0 |
In graph theory and network analysis, indicators of centrality identify the most important vertices within a graph. Applications include identifying the most influential person(s) in a social network, key infrastructure nodes in the Internet or urban networks, and super-spreaders of disease. In this scenario, we could identify the important stations in the MRT network using different centrality measures.
Generation of graph attributes: Degree Centrality (in-degree, out-degree and total degree)
Since this is an undirected network, we will use the total degree centrality measure. [in-degree and out-degree will be relevant if it is a directed network graph]
Degree centrality measures how connected an entity is by counting the number of direct links each entity has to others in the network.
V(simple_g)$degree=degree(simple_g, mode="all")
Betweenness centrality measures the number of paths that pass through each entity.
V(simple_g)$betweenness=betweenness(simple_g,normalized=T)
Generation of graph attributes: Closeness Centrality
Closeness centrality (or closeness) of a node is a measure of centrality in a network, calculated as the sum of the length of the shortest paths between the node and all other nodes in the graph. Thus the more central a node is, the closer it is to all other nodes
Closeness centrality measures the proximity of an entity to the other entities in the social network.
V(simple_g)$closeness=closeness(simple_g, normalized=T)
Generation of graph attributes: Eigenvector
Eigenvector measures how connected an entity is and how much direct influence it might have over other connected entities in the network.
V(simple_g)$eigen=evcent(simple_g)$vector
Coreness is a measure that can help identify tightly interlinked groups within a network. A k-core is a maximal group of entities, all of which are connected to at least k other entities in the group.
V(simple_g)$coreness=coreness(simple_g)
Specify graph layout to use
glay = layout_with_lgl(simple_g)
Installing the plyr package for data mapping / transformation
install.packages("plyr")
library(plyr)
Installing package into '/home/nbcommon/R'
(as 'lib' is unspecified)
# Generate node colors based on network:
E(simple_g)$color <- mapvalues(E(simple_g)$network, c("NSL","EWL","CAL","NEL","CCL","DTL","CEL"), c("#D42E12","#009645","#009645","#9900AA","#FA9E0D","#FA9E0D","#005EC4"))
# plot degree graph
plot(simple_g, layout=glay, edge.color=E(simple_g)$color, edge.width=3, edge.curve=1,
vertex.label.cex=.7, vertex.color="white", vertex.frame.color="black",
vertex.label.font=1.5, vertex.label=V(simple_g)$label, vertex.label.color="grey40",
vertex.size=V(simple_g)$degree*3.5)
V(simple_g)$name[degree(simple_g)==max(degree(simple_g))]
'Dhoby Ghaut'
# plot closeness graph
plot(simple_g, layout=glay, edge.color=E(simple_g)$color, edge.width=3, edge.curve=1,
vertex.label.cex=.7, vertex.color="white", vertex.frame.color="black",
vertex.label.font=.7, vertex.label=V(simple_g)$label, vertex.label.color="grey40",
vertex.size=V(simple_g)$closeness*90)
V(simple_g)$name[closeness(simple_g)==max(closeness(simple_g))]
'Little India'
# plot betweenness graph
plot(simple_g, layout=glay, edge.color=E(simple_g)$color, edge.width=3, edge.curve=1,
vertex.label.cex=.7, vertex.color="white", vertex.frame.color="black",
vertex.label.font=1, vertex.label=V(simple_g)$label, vertex.label.color="grey40",
vertex.size=V(simple_g)$betweenness*60)
V(simple_g)$name[betweenness(simple_g)==max(betweenness(simple_g))]
'Botanic Gardens'
# plot eigenvector graph
plot(simple_g, layout=glay, edge.color=E(simple_g)$color, edge.width=3, edge.curve=1,
vertex.label.cex=.7, vertex.color="white", vertex.frame.color="black",
vertex.label.font=1, vertex.label=V(simple_g)$label, vertex.label.color="grey40",
vertex.size=V(simple_g)$eigen*20)
V(simple_g)$name[which.max(V(simple_g)$eigen)]
'Dhoby Ghaut'
attr = data.frame(row.names=V(simple_g)$name,degree=V(simple_g)$degree,
coreness=V(simple_g)$coreness,betweenness=V(simple_g)$betweenness,
closeness=V(simple_g)$closeness,eigen=V(simple_g)$eigen)
table(attr$degree)
table(attr$coreness)
table(attr$betweenness)
table(attr$closeness)
table(attr$eigen)
1 2 3 4 5
6 92 7 13 1
1 2
24 95
0 0.00195567144719687 0.00402964894490318 0.0047008547008547
6 1 1 1
0.00582114056690328 0.0068810662031001 0.0123293530073191 0.0127891255009899
1 1 1 1
0.0160534067313728 0.0169491525423729 0.0175975938687803 0.0226816498002939
1 3 1 1
0.0227057938922346 0.0232110952449936 0.0251684912701862 0.0258203817525851
1 1 1 1
0.0267399267399267 0.0270655270655271 0.0278960838282872 0.0288625373371136
1 1 1 1
0.0309630733359547 0.0317977690859047 0.033391279153991 0.0336085759814573
1 1 1 3
0.0344398225754158 0.0345308802935922 0.0345767540682795 0.0346226278429668
1 1 1 1
0.0353928243758752 0.0373026220483848 0.0386443437290895 0.0401395528514173
1 1 1 1
0.0412863972186006 0.0418948283355063 0.0421676565744362 0.0422087015307354
1 1 1 1
0.0424798051916696 0.0436163020908784 0.0445023902651021 0.0465739533536144
1 1 1 1
0.0471775556521319 0.0475807619875416 0.0482295482295482 0.0484378772514366
1 1 1 1
0.0486262011685741 0.0490656236418948 0.0493988121106765 0.0499782703172534
1 1 1 3
0.0505456564778599 0.052923849534019 0.056188130764402 0.0593220338983051
1 1 1 1
0.0604809503114588 0.062344874209281 0.066058235549761 0.066597338631237
1 1 3 1
0.0671326476411222 0.0689813952525817 0.0691848954560819 0.0705852527886426
1 1 1 1
0.0706214689265537 0.0744724515910957 0.0762518711671254 0.0766843953284631
2 1 1 1
0.0799845477811579 0.0818484716789802 0.0819209039548023 0.0904485972282583
1 2 1 1
0.0926601960500266 0.0941285017556204 0.0945241199478488 0.096830225643785
1 1 1 1
0.0973489787049109 0.0997275166766692 0.105159937363327 0.106567193007871
2 1 1 1
0.108431116905693 0.112559756627553 0.120474189965715 0.124850479087767
1 1 1 1
0.127480805446907 0.132674199623352 0.137474734932362 0.142346667770397
1 1 1 1
0.144993894993895 0.147746321475135 0.15777267302691 0.168727537371605
1 1 1 1
0.171709332726282 0.189225871429261 0.190974248601367 0.19911632623497
1 1 1 1
0.202158481819499 0.202221601374144 0.205202361982023 0.210415761263219
1 1 1 1
0.211503969978546 0.222513330987907 0.226112180349468 0.249501596959224
1 1 1 1
0.259219593965357 0.277175900057256 0.314647530749226
1 1 1
0.053393665158371 0.0563784042044912 0.0596562184024267 0.0632707774798928
1 1 1 1
0.0672748004561003 0.0708708708708709 0.0711271850512357 0.0717325227963526
1 1 1 1
0.0718198417528911 0.0721271393643032 0.0727945712523134 0.0762273901808786
1 1 1 1
0.0767230169050715 0.077124183006536 0.0773263433813893 0.0776826859776168
1 1 1 1
0.077990746860542 0.0781456953642384 0.0784053156146179 0.0793544048419637
1 1 1 1
0.0794078061911171 0.0798376184032476 0.0809327846364883 0.0823447313328681
1 1 1 1
0.0830401125967628 0.0836286321757619 0.0838068181818182 0.0841654778887304
1 1 1 1
0.085383502170767 0.0861313868613139 0.0885221305326332 0.0887218045112782
1 1 1 1
0.0890566037735849 0.0899390243902439 0.0909090909090909 0.0917573872472784
1 1 1 1
0.0940239043824701 0.0960130187144019 0.0970394736842105 0.0972794723825227
2 1 1 1
0.0973597359735974 0.0988274706867672 0.0990764063811923 0.0991596638655462
1 1 1 1
0.0994102780117944 0.100254885301614 0.101636520241171 0.101811906816221
1 1 1 1
0.10251954821894 0.103327495621716 0.105357142857143 0.105545617173524
1 1 1 1
0.106594399277326 0.107175295186194 0.1073703366697 0.107468123861566
1 1 1 1
0.108058608058608 0.108256880733945 0.10865561694291 0.109158186864015
1 1 1 1
0.109461966604824 0.109767441860465 0.109869646182495 0.109972041006524
1 1 1 1
0.110383536014967 0.111848341232227 0.112060778727445 0.112595419847328
1 1 1 2
0.112702960840497 0.113026819923372 0.113461538461538 0.114451988360815
1 1 1 1
0.115234375 0.11545988258317 0.115572967678746 0.116370808678501
1 2 1 1
0.117647058823529 0.118592964824121 0.118712273641851 0.119071644803229
1 1 2 1
0.119312436804853 0.119433198380567 0.119918699186992 0.121025641025641
1 2 1 1
0.121274409044193 0.121524201853759 0.121900826446281 0.122026887280248
1 2 1 1
0.122279792746114 0.123044838373306 0.123430962343096 0.124341412012645
1 1 1 1
0.124604012671595 0.124867724867725 0.12566560170394 0.125933831376734
1 1 1 1
0.126473740621651 0.127292340884574 0.127843986998917 0.127982646420824
1 1 1 2
0.128121606948969 0.12910284463895 0.130099228224917 0.131403118040089
1 1 1 2
0.131843575418994 0.133182844243792 0.134090909090909 0.135476463834673
1 1 1 1
0.136258660508083 0.138173302107728 0.140979689366786
1 1 1
2.51858362053298e-07 7.39028698759823e-07 1.91667562290336e-06
1 1 1
4.88507796089859e-06 1.24176223479958e-05 2.10200818492998e-05
1 1 1
2.67431016136541e-05 3.15519861450127e-05 3.493618354964e-05
1 1 1
5.74522762701692e-05 8.01654591480061e-05 8.14932595885658e-05
1 1 1
0.000141839273885244 0.000167809921265858 0.000177714640335611
1 1 1
0.000185105863472294 0.000203677745616489 0.000204189716771217
1 1 1
0.000223321313009527 0.000287174882385132 0.000321189495444625
1 1 1
0.000358747103161553 0.000417817517967914 0.000492405122926692
1 1 1
0.000517486721433531 0.000517661199389077 0.000521468567890903
1 1 1
0.000543156654683785 0.000647439641568565 0.000655292355512262
1 1 1
0.000704534547681859 0.000910833357740738 0.00127705581286622
1 1 1
0.00131478499340561 0.00137831538939482 0.00140868023272799
1 1 1
0.00164950018256149 0.00231391377469838 0.00274166271824377
1 1 1
0.00282282937525379 0.00325486333280614 0.0035903377056515
1 1 1
0.00378373757664514 0.00380690651505764 0.00413559846730773
1 1 1
0.00587889032532363 0.00666655321918628 0.0069682511646331
1 1 1
0.00729572527509893 0.00738687889451853 0.007573376213864
1 1 1
0.00827369879931538 0.00912645777319788 0.010485598006559
1 1 1
0.0105653985966293 0.0116571804822862 0.0124409387540652
1 1 1
0.012822558121519 0.0133160285498551 0.0149365306059223
1 1 1
0.016820023102403 0.017624129573429 0.0178684329563004
1 1 1
0.0200011387088744 0.0208359090163346 0.021022674274045
1 1 1
0.0231894529515193 0.0234287025398688 0.0246279983079351
1 1 1
0.0256415713329211 0.0266323211910227 0.0281642727572974
1 1 1
0.0324649884818062 0.0379494133507986 0.0399540168190206
1 1 1
0.0426884877753544 0.0431176419956824 0.0450445140766606
1 1 1
0.0458668393248469 0.0520506817733822 0.053413193322856
1 1 1
0.0536443324331427 0.0546418749796778 0.0573126934639011
1 1 1
0.0581815090184332 0.0885708045661908 0.0890729109251433
1 1 1
0.105253358095607 0.114305811115643 0.114586056646666
1 1 1
0.114720227435284 0.127090890274223 0.13570780879659
1 1 1
0.14729346265851 0.188118280674737 0.19157545406345
1 1 1
0.216775918415613 0.221412875690504 0.232840042447464
1 1 1
0.249100008151929 0.282979552890921 0.290363348277626
1 1 1
0.308845224052014 0.320872474237074 0.342513847439296
1 1 1
0.347566516922269 0.36042054487175 0.374022111837471
1 1 1
0.440571504738262 0.457523962582117 0.459245894439935
1 1 1
0.46826186745292 0.52144365496715 0.54751538121932
1 1 1
0.560619511605635 0.715627468790695 0.734824970607291
1 1 1
0.814446111403723 1
1 1
attr
degree | coreness | betweenness | closeness | eigen | |
---|---|---|---|---|---|
Jurong East | 3 | 2 | 0.202158482 | 0.09601302 | 1.314785e-03 |
Bukit Batok | 2 | 2 | 0.060480950 | 0.08905660 | 5.176612e-04 |
Bukit Gombak | 2 | 2 | 0.046573953 | 0.08304011 | 2.041897e-04 |
Choa Chu Kang | 2 | 2 | 0.037302622 | 0.07940781 | 8.149326e-05 |
Yew Tee | 2 | 2 | 0.033391279 | 0.07799075 | 3.493618e-05 |
Kranji | 2 | 2 | 0.031797769 | 0.07712418 | 2.102008e-05 |
Marsiling | 2 | 2 | 0.034622628 | 0.07840532 | 2.674310e-05 |
Woodlands | 2 | 2 | 0.041286397 | 0.08093278 | 5.745228e-05 |
Admiralty | 2 | 2 | 0.049398812 | 0.08416548 | 1.418393e-04 |
Sembawang | 2 | 2 | 0.059322034 | 0.08852213 | 3.587471e-04 |
Yishun | 2 | 2 | 0.070621469 | 0.09402390 | 9.108334e-04 |
Khatib | 2 | 2 | 0.081920904 | 0.10025489 | 2.313914e-03 |
Yio Chu Kang | 2 | 2 | 0.094524120 | 0.10825688 | 5.878890e-03 |
Ang Mo Kio | 2 | 2 | 0.108431117 | 0.11871227 | 1.493653e-02 |
Bishan | 4 | 2 | 0.259219594 | 0.13140312 | 3.794941e-02 |
Braddell | 2 | 2 | 0.048626201 | 0.12434141 | 4.311764e-02 |
Toa Payoh | 2 | 2 | 0.047177556 | 0.12304484 | 8.857080e-02 |
Novena | 2 | 2 | 0.052923850 | 0.12812161 | 2.167759e-01 |
Newton | 4 | 2 | 0.202221601 | 0.13817330 | 5.475154e-01 |
Orchard | 2 | 2 | 0.006881066 | 0.12486772 | 3.425138e-01 |
Somerset | 2 | 2 | 0.005821141 | 0.12566560 | 4.575240e-01 |
Dhoby Ghaut | 5 | 2 | 0.132674200 | 0.13547646 | 1.000000e+00 |
City Hall | 3 | 2 | 0.076684395 | 0.13009923 | 7.348250e-01 |
Raffles Place | 3 | 2 | 0.068981395 | 0.12460401 | 4.405715e-01 |
Marina Bay | 3 | 2 | 0.023211095 | 0.11302682 | 3.088452e-01 |
Marina South Pier | 1 | 1 | 0.000000000 | 0.10163652 | 1.052534e-01 |
Pasir Ris | 1 | 1 | 0.000000000 | 0.07087087 | 1.777146e-04 |
Tampines | 4 | 2 | 0.034576754 | 0.07622739 | 5.214686e-04 |
Simei | 2 | 2 | 0.016053407 | 0.07814570 | 6.474396e-04 |
Tanah Merah | 3 | 2 | 0.079984548 | 0.08380682 | 1.378315e-03 |
... | ... | ... | ... | ... | ... |
Kent Ridge | 2 | 2 | 0.027065527 | 0.09907641 | 0.0037837376 |
Haw Par Villa | 2 | 2 | 0.022681650 | 0.09727947 | 0.0038069065 |
Pasir Panjang | 2 | 2 | 0.022705794 | 0.09735974 | 0.0073868789 |
Labrador Park | 2 | 2 | 0.025168491 | 0.09882747 | 0.0178684330 |
Telok Blangah | 2 | 2 | 0.030963073 | 0.10181191 | 0.0450445141 |
Bayfront | 3 | 2 | 0.026739927 | 0.11346154 | 0.3604205449 |
Bukit Panjang | 1 | 1 | 0.000000000 | 0.07181984 | 0.0001678099 |
Cashew | 2 | 1 | 0.016949153 | 0.07732634 | 0.0004924051 |
Hillview | 2 | 1 | 0.033608576 | 0.08362863 | 0.0012770558 |
Beauty World | 2 | 1 | 0.049978270 | 0.09090909 | 0.0032548633 |
King Albert Park | 2 | 1 | 0.066058236 | 0.09941028 | 0.0082736988 |
Sixth Avenue | 2 | 1 | 0.081848472 | 0.10946197 | 0.0210226743 |
Tan Kah Kee | 2 | 1 | 0.097348979 | 0.12152420 | 0.0534131933 |
Stevens | 2 | 2 | 0.142346668 | 0.13409091 | 0.2328400424 |
Rochor | 2 | 2 | 0.094128502 | 0.13318284 | 0.5214436550 |
Downtown | 2 | 2 | 0.012329353 | 0.10976744 | 0.1881182807 |
Telok Ayer | 2 | 2 | 0.017597594 | 0.11545988 | 0.1915754541 |
Fort Canning | 2 | 2 | 0.056188131 | 0.11764706 | 0.1472934627 |
Bencoolen | 2 | 2 | 0.049065624 | 0.11206078 | 0.0581815090 |
Jalan Besar | 2 | 2 | 0.044502390 | 0.10915819 | 0.0234287025 |
Bendemeer | 2 | 2 | 0.041894828 | 0.10737034 | 0.0105653986 |
Geylang Bahru | 2 | 2 | 0.042208702 | 0.10746812 | 0.0075733762 |
Mattar | 2 | 2 | 0.047580762 | 0.11038354 | 0.0116571805 |
Ubi | 2 | 2 | 0.090448597 | 0.10805861 | 0.0104855980 |
Kaki Bukit | 2 | 2 | 0.076251871 | 0.09915966 | 0.0041355985 |
Bedok North | 2 | 2 | 0.062344874 | 0.09175739 | 0.0016495002 |
Bedok Reservoir | 2 | 2 | 0.048437877 | 0.08538350 | 0.0007045345 |
Tampines West | 2 | 2 | 0.034530880 | 0.07983762 | 0.0004178175 |
Tampines East | 2 | 2 | 0.001955671 | 0.07112719 | 0.0002871749 |
Upper Changi | 2 | 2 | 0.004029649 | 0.07279457 | 0.0003211895 |
From the results obtained from the different centrality measures applied to the MRT network, we could often see that the station 'Dhoby Ghaut' frequently appears as one of the significant nodes suggesting that it could likely be the most important node in the MRT network. The consequences of removing such a node might breaks the network into different smaller networks and reduce connectivity among the network, seriously impairing the connectivity and coverage of the MRT services.