Service as a Freelancer

If you want hire me as a freelancer please use the following links.

USairports Data

First of all add the data from igraph library. To utilize the functionalities of different libraries, it is important to add them first. We are going to to analyze the USairport dataset from Igraph library. So, it is also important to add Igraphdata library and attach the data to the code.

library(igraph)
library(igraphdata)
data("USairports")

After adding the libraries and USairports data into the code. Now, start analysis of that data. First off all, check the check out how the data looks and what are the attributes. To find the number of nodes(airports) and edges(traveling) following code is used.

vcount(USairports) #number of nodes
## [1] 755
ecount(USairports) #number of edges
## [1] 23473

To find number of attributes attached with the data. We can use the _attr function to access the attributes of network data. Moreover, we can see the first few or more nodes and edges using V() & E() functions.

V(USairports)[1:10] #first ten nodes(airports)
## + 10/755 vertices, named, from bf6202d:
##  [1] BGR BOS ANC JFK LAS MIA EWR BJC TEB LAX
E(USairports)[1:10] #fist ten departures
## + 10/23473 edges from bf6202d (vertex names):
##  [1] BGR->JFK BGR->JFK BOS->EWR ANC->JFK JFK->ANC LAS->LAX MIA->JFK EWR->ANC
##  [9] BJC->MIA MIA->BJC
vertex_attr_names(USairports) #Attributes attached with nodes
## [1] "name"     "City"     "Position"
edge_attr_names(USairports) #Attributes attached with edges
## [1] "Carrier"    "Departures" "Seats"      "Passengers" "Aircraft"  
## [6] "Distance"
vertex_attr(USairports,"City",index = c(1:5)) #First 5 cities
## [1] "Bangor, ME"    "Boston, MA"    "Anchorage, AK" "New York, NY" 
## [5] "Las Vegas, NV"
edge_attr(USairports, "Carrier", index = c(1:5)) # First 5 carriers in the traveling historry
## [1] "British Airways Plc" "British Airways Plc" "British Airways Plc"
## [4] "China Airlines Ltd." "China Airlines Ltd."
V(USairports)[[1:5]] #Access all attributes of first 5 airports
## + 5/755 vertices, named, from bf6202d:
##   name          City         Position
## 1  BGR    Bangor, ME N444827 W0684941
## 2  BOS    Boston, MA N422152 W0710019
## 3  ANC Anchorage, AK N611028 W1495947
## 4  JFK  New York, NY N403823 W0734644
## 5  LAS Las Vegas, NV N360449 W1150908
E(USairports)[[1:5]] #Acess all attributes of first 5 edges
## + 5/23473 edges from bf6202d (vertex names):
##   tail head tid hid             Carrier Departures Seats Passengers Aircraft
## 1  BGR  JFK   1   4 British Airways Plc          1   226        193      627
## 2  BGR  JFK   1   4 British Airways Plc          1   299        253      819
## 3  BOS  EWR   2   7 British Airways Plc          1   216        141      627
## 4  ANC  JFK   3   4 China Airlines Ltd.         13  5161       3135      819
## 5  JFK  ANC   4   3 China Airlines Ltd.         13  5161       4097      819
##   Distance
## 1      382
## 2      382
## 3      200
## 4     3386
## 5     3386

So, we have seen that nodes have 3 attributes (i.e., name, City and Position) and edges have 5 attributes (i.e., Carrier, Departures, Seats, Passengers, Aircraft and Distance). Moreover, we also seen the first 5 citeis and traveling aircrafts. We can also see the complete history between 2 airports by specifying airport names in the accessing edges function as follows.

E(USairports)["SFO" %--% "LAX"] #Acess to complete travel history between these two airports
## + 35/23473 edges from bf6202d (vertex names):
##  [1] LAX->SFO SFO->LAX LAX->SFO LAX->SFO LAX->SFO SFO->LAX SFO->LAX SFO->LAX
##  [9] LAX->SFO SFO->LAX SFO->LAX LAX->SFO LAX->SFO LAX->SFO SFO->LAX SFO->LAX
## [17] SFO->LAX LAX->SFO SFO->LAX SFO->LAX SFO->LAX LAX->SFO LAX->SFO LAX->SFO
## [25] LAX->SFO LAX->SFO SFO->LAX SFO->LAX SFO->LAX SFO->LAX SFO->LAX LAX->SFO
## [33] LAX->SFO SFO->LAX SFO->LAX
E(USairports)["SFO" %--% "LAX"]$Carrier #See all the aircrafts traveled between SFO and LAX
##  [1] "Cathay Pacific Airways Ltd." "London Air Services Limited"
##  [3] "American Airlines Inc."      "American Airlines Inc."     
##  [5] "American Airlines Inc."      "American Airlines Inc."     
##  [7] "American Airlines Inc."      "American Airlines Inc."     
##  [9] "Delta Air Lines Inc."        "Delta Air Lines Inc."       
## [11] "Delta Air Lines Inc."        "Southwest Airlines Co."     
## [13] "Southwest Airlines Co."      "Southwest Airlines Co."     
## [15] "Southwest Airlines Co."      "Southwest Airlines Co."     
## [17] "Southwest Airlines Co."      "SkyWest Airlines Inc."      
## [19] "SkyWest Airlines Inc."       "SkyWest Airlines Inc."      
## [21] "SkyWest Airlines Inc."       "United Air Lines Inc."      
## [23] "United Air Lines Inc."       "United Air Lines Inc."      
## [25] "United Air Lines Inc."       "United Air Lines Inc."      
## [27] "United Air Lines Inc."       "United Air Lines Inc."      
## [29] "United Air Lines Inc."       "United Air Lines Inc."      
## [31] "United Air Lines Inc."       "Virgin America"             
## [33] "Virgin America"              "Virgin America"             
## [35] "Virgin America"

Above, we seen the complete histroy between two airports. It is also possible to see the complete travel history between two states. The following code will show us the history between New Yor and California. First, we will group the edges from both states and then will present them.

# Grep the state code from the city
Cal = grepl("CA$", V(USairports)$City)
Ny = grepl("NY$", V(USairports)$City)
#Edges from California to New York
E(USairports)[V(USairports)[Cal]%--%V(USairports)[Ny]]
## + 72/23473 edges from bf6202d (vertex names):
##  [1] JFK->LAX LAX->JFK JFK->LAX JFK->LAX JFK->LAX JFK->SAN JFK->SFO JFK->SFO
##  [9] JFK->SFO LAX->JFK LAX->JFK LAX->JFK SAN->JFK SFO->JFK SFO->JFK SFO->JFK
## [17] BUR->JFK JFK->BUR JFK->LAX JFK->LGB JFK->OAK JFK->SAN JFK->SFO JFK->SJC
## [25] JFK->SMF LAX->JFK LGB->JFK OAK->JFK SAN->JFK SFO->JFK SJC->JFK SMF->JFK
## [33] JFK->LAX JFK->LAX JFK->LAX JFK->LAX JFK->SAN JFK->SAN JFK->SFO JFK->SFO
## [41] JFK->SNA JFK->SNA LAX->JFK LAX->JFK LAX->JFK SAN->JFK SAN->JFK SFO->JFK
## [49] SFO->JFK SNA->JFK LAX->ALB JFK->LAX JFK->LAX JFK->SFO JFK->SFO JFK->SFO
## [57] LAX->JFK LAX->JFK SFO->JFK SFO->JFK SFO->JFK BUR->FRG FRG->BUR HPN->BUR
## [65] BUF->LAX JFK->LAX JFK->SFO JFK->SFO LAX->JFK LAX->JFK SFO->JFK SFO->JFK

So, we have seen that there are 72 traveling records between California and New York. Now, we can further make a subgraph of specified nodes and edges.

Partition the Network

Here, we will get the graph of only that that nodes that belongs to the aricraft British Airways Plc.

# create subgraph of departure data from British Airways Plc
g = subgraph.edges(USairports,E(USairports)[Carrier=="Spirit Air Lines"]) 
g
## IGRAPH 557f6f6 DN-- 19 107 -- US airports
## + attr: name (g/c), name (v/c), City (v/c), Position (v/c), Carrier
## | (e/c), Departures (e/n), Seats (e/n), Passengers (e/n), Aircraft
## | (e/n), Distance (e/n)
## + edges from 557f6f6 (vertex names):
##  [1] ACY->BOS ACY->FLL ACY->FLL ACY->MCO ACY->MCO ACY->MYR ACY->PBI ACY->RSW
##  [9] ACY->TPA ATL->FLL BDL->LGA BOS->ACY BOS->FLL BOS->MYR BQN->FLL BQN->FLL
## [17] DCA->FLL DCA->FLL DTW->BDL DTW->FLL DTW->FLL DTW->FLL DTW->LAS DTW->LAS
## [25] DTW->LAS DTW->LAX DTW->LGA DTW->MCO DTW->MCO DTW->PBI DTW->RSW DTW->TPA
## [33] DTW->TPA FLL->ACY FLL->ACY FLL->ATL FLL->BOS FLL->BQN FLL->BQN FLL->DCA
## [41] FLL->DCA FLL->DTW FLL->DTW FLL->DTW FLL->LAS FLL->LAX FLL->LGA FLL->LGA
## + ... omitted several edges

Now, checking the nodes and edges attributes in subgraph.

vertex_attr(g) #Access all the information about nodes
## $name
##  [1] "BOS" "LAS" "LAX" "PBI" "BDL" "DCA" "DTW" "LGA" "MYR" "FLL" "MCO" "TPA"
## [13] "ORD" "ATL" "RSW" "ACY" "SJU" "STT" "BQN"
## 
## $City
##  [1] "Boston, MA"                     "Las Vegas, NV"                 
##  [3] "Los Angeles, CA"                "West Palm Beach/Palm Beach, FL"
##  [5] "Hartford, CT"                   "Washington, DC"                
##  [7] "Detroit, MI"                    "New York, NY"                  
##  [9] "Myrtle Beach, SC"               "Fort Lauderdale, FL"           
## [11] "Orlando, FL"                    "Tampa, FL"                     
## [13] "Chicago, IL"                    "Atlanta, GA"                   
## [15] "Fort Myers, FL"                 "Atlantic City, NJ"             
## [17] "San Juan, PR"                   "Charlotte Amalie, VI"          
## [19] "Aguadilla, PR"                 
## 
## $Position
##  [1] "N422152 W0710019" "N360449 W1150908" "N335633 W1182429" "N264059 W0800544"
##  [5] "N415620 W0724060" "N385108 W0770216" "N421245 W0832112" "N404638 W0735221"
##  [9] "N334047 W0785542" "N260421 W0800910" "N282544 W0811858" "N275832 W0823160"
## [13] "N415847 W0875415" "N333826 W0842537" "N263210 W0814519" "N392727 W0743438"
## [17] "N182622 W0660007" "N182014 W0645824" "N182942 W0670746"
edge_attr(g) #Access all the information about edges
## $Carrier
##   [1] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##   [4] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##   [7] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [10] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [13] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [16] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [19] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [22] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [25] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [28] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [31] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [34] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [37] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [40] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [43] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [46] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [49] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [52] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [55] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [58] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [61] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [64] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [67] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [70] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [73] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [76] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [79] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [82] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [85] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [88] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [91] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [94] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
##  [97] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
## [100] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
## [103] "Spirit Air Lines" "Spirit Air Lines" "Spirit Air Lines"
## [106] "Spirit Air Lines" "Spirit Air Lines"
## 
## $Departures
##   [1]  29  42  44  27  58  30  29  55  29  89   1  29  84  30  23   3  26  59
##  [19]   1   8  57  23  11  16  19  31  55  30  32  31  87  15  32  42  43  89
##  [37]  84  23   3  26  59   8  56  23  62  31   3 135  28  61  31  20  63   6
##  [55]  30  72   3  34   1  92  11  16  19  61  30  31  31  56   3 133   1  56
##  [73]  28  58  30  32  28  61  29   2  29  30   2  31  56  20  63   6  30  67
##  [91]  29  30   1  55  87  67  28  75   3  31  33   1  30  14  32   1  92
## 
## $Seats
##   [1]  4205  7476  6380  4806  8410  4350  4205  7975  4205 12905   145  4205
##  [13] 12180  4350  4094   435  4628  8555   145  1424  8265  5014  1958  2320
##  [25]  4142  4495  7975  5340  4640  4495 12615  2670  4640  7476  6235 12905
##  [37] 12180  4094   435  4628  8555  1424  8120  5014  8990  4495   534 19575
##  [49]  4984  8845  4495  3560  9135  1308  5340 10440   654  4930   178 13340
##  [61]  1958  2320  4142  8845  4350  4495  4495  8120   534 19285   145  8120
##  [73]  4984  8410  5340  4640  4984  8845  5162   290  4205  4350   290  4495
##  [85]  8120  3560  9135  1308  4350  9715  4205  4350   145  7975 12615  9715
##  [97]  4984 10875   654  5518  4785   145  4350  2492  4640   178 13340
## 
## $Passengers
##   [1]  2154  6172  5661  3695  7138  2966  3480  6156  3613 11478   135  2994
##  [13] 10645  3489  3613   423  4144  7564   135  1383  7655  4287  1490  1876
##  [25]  3481  4082  6768  4835  3956  3353 10094  2611  4179  5562  5354 10300
##  [37]  8775  3819   411  3396  6624  1206  5965  4211  7480  3958   477 16897
##  [49]  4274  7279  3609  3105  6546   964  5183  9951   595  4678   102  6809
##  [61]  1878  2028  3146  6858  3934  4130  4298  7177   482 18313   143  7420
##  [73]  3743  7029  4036  4063  4029  7128  4967   281  2999  3365     5  4216
##  [85]  7421  2856  8704  1214  4210  7800  3367  2692    29  6189  9095  6407
##  [97]  4639 10039   580  5252  4229   143  4001  1836  3926   100  8405
## 
## $Aircraft
##   [1] 698 694 698 694 698 698 698 698 698 698 698 698 698 698 694 698 694 698
##  [19] 698 694 698 699 694 698 699 698 698 694 698 698 698 694 698 694 698 698
##  [37] 698 694 698 694 698 694 698 699 698 698 694 698 694 698 698 694 698 699
##  [55] 694 698 699 698 694 698 694 698 699 698 698 698 698 698 694 698 698 698
##  [73] 694 698 694 698 694 698 694 698 698 698 698 698 698 694 698 699 698 698
##  [91] 698 698 698 698 698 698 694 698 699 694 698 698 698 694 698 694 698
## 
## $Distance
##   [1]  274  977  977  852  852  466  936  982  914  581  101  274 1237  738  983
##  [16]  983  899  899  548 1127 1127 1127 1750 1750 1750 1979  501  957  957 1087
##  [31] 1085  983  983  977  977  581 1237  983  983  899  899 1127 1127 1127 2174
##  [46] 2342 1076 1076  178  178  529 1182 1182 1182 1046 1046 1046 1108  197  197
##  [61] 1750 1750 1750 2174 1515 1979 2342  501 1076 1076  950  563  852  852  957
##  [76]  957  178  178 1189 1189  466  738  636  529  563 1182 1182 1182 1515 1120
##  [91]  936 1087   42  982 1085 1120 1046 1046 1046 1189 1108   68  914  983  983
## [106]  197  197

Now, we will see how the graph looks like. To display ethe graph, plot() is used with set of vertex and edeges parameters.

#simple visualization of graph
plot(g, edge.arrow.size=.4,vertex.label=V(g)$name, vertex.color="yellow", layout=layout_with_kk)

Now, calculating and displaying the betweenness centrality of each node. To find the betweenness, igraph provides a function. Igraph library helps us to find the betweenness of nodes as well as edges. The betweenness() function used for the nodes, while, the edge.betweenness() function works for the edges to compute betweenness centrality.

#finding betweenness of nodes
BetN = betweenness(g,V(g),directed = TRUE) 
BetN
##         BOS         LAS         LAX         PBI         BDL         DCA 
##   0.0000000   0.8423077   0.0000000   0.2047619   0.0000000   0.0000000 
##         DTW         LGA         MYR         FLL         MCO         TPA 
##  57.4637688  17.3928571   2.1190476 199.4569729   3.8151883   0.4095238 
##         ORD         ATL         RSW         ACY         SJU         STT 
##   4.8526786   0.0000000   0.8212454  19.2883145   0.3333333   0.0000000 
##         BQN 
##   0.0000000
#finding betweeness of edges
BetE = edge.betweenness(g,E(g),directed = TRUE)
BetE
##   [1]  3.333333  5.747321  5.747321  1.938690  1.938690  3.666667  6.737719
##   [8]  5.645238  2.533333 18.000000 18.000000  3.833333 12.333333  1.833333
##  [15]  9.000000  9.000000  9.000000  9.000000 18.000000  3.573146  3.573146
##  [22]  3.573146  2.310256  2.310256  2.310256  5.416667  4.183333  2.243727
##  [29]  2.243727 11.467043  9.332875  2.463095  2.463095  7.129323  7.129323
##  [36] 18.000000 13.000000  9.000000  9.000000  9.000000  9.000000  7.147031
##  [43]  7.147031  7.147031 10.050000 12.583333  5.766667  5.766667  3.967857
##  [50]  3.967857 11.416667  5.915293  5.915293  5.915293  4.847436  4.847436
##  [57]  4.847436 18.000000  5.475000  5.475000  2.403846  2.403846  2.403846
##  [64] 10.150000  1.480769  6.083333 11.916667  9.533333  9.721429  9.721429
##  [71]  3.214286  3.202381  2.241917  2.241917  3.028497  3.028497  4.408333
##  [78]  4.408333  1.228846  1.228846  2.771429  1.666667  5.569048  8.435714
##  [85]  1.676190  5.716003  5.716003  5.716003  1.861538  3.843132  3.104762
##  [92]  4.950000 10.150000  6.160119  9.035096  3.626030  5.344327  5.344327
##  [99]  5.344327  2.300353 16.666667  1.333333  2.676190  2.791667  2.791667
## [106]  5.075000  5.075000

Now, give some styles to the network. There are many ploting properties exist that can also be set as attributes of the nodes and edges.

# As graph is small, so we add 45 to the size of each node
V(g)$size = 15
# Color the nodes, where each node will get distinct color
V(g)$color= degree(g,v=V(g),mode = "all")
#make the size of each edge according to the betweenness
#As betweeness is too low, so add 20 to each edge size
E(g)$size = edge.betweenness(g,E(g),directed = TRUE)
#assign the color of edge edge according to the betweenness
E(g)$width = edge.betweenness(g,E(g),directed = TRUE)*0.2
#Assign different colors of specific edges and nodes
E(g)[Departures>100]$color = "red"
E(g)[Departures<100 & Departures>50]$color = "green"
E(g)[Departures<50 & Departures>20]$color = "blue"
E(g)[Departures<21]$color = "black"
#ploting graph without labels
plot(g, edge.arrow.size=.4,vertex.label=NA)

#ploting graph with labels. 
plot(g, edge.arrow.size=.2, 
     vertex.frame.color="#ffffff",
     vertex.label=V(g)$name, 
     vertex.label.color="black",
     layout=layout_with_kk)
legend(x=1.5, y=1.3, 
       degree(g,v=V(g),mode = "all"), 
       pch=21, col="#777777", 
       pt.bg=degree(g,v=V(g),mode = "all"), 
       pt.cex=2, cex=.8, 
       bty="n", ncol=1)

The above two plots are of the same graph. The first one represents the nodes without labels, while, the second one represents the nodes with labels. nodes having same color belongs to the same degree centrality, while, edges having same color represents their departure count. where, green color shows that the departure count between 100 and 50, while, blue color shows the count between 20 and 50. Similarly, red color shows the departure count more than 100 and black color shows the departure count less than 20. Moreover, the width of the edge is the betweenness value.

Some another findings

What are the most important airport the aircraft "Spirit Air Lines" traveled to?

#Top 3 airports 
sort(degree(g,mode = "in"),decreasing = TRUE)[1:3]
## FLL DTW ACY 
##  28  15   9

What are the most important airport according to their centrality?

#Top 3 airports 
sort(betweenness(g,directed = TRUE),decreasing = TRUE)[1:3]
##       FLL       DTW       ACY 
## 199.45697  57.46377  19.28831

Interactive graph

library(visNetwork)
fc = spinglass.community(g)
V(g)$community = fc$membership

nodes <- data.frame(id = V(g)$name, title = V(g)$name, group = V(g)$community)
nodes <- nodes[order(nodes$id, decreasing = F),]
edges <- get.data.frame(g, what="edges")[1:2]

visNetwork(nodes, edges) %>%
   visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)