IS 620 WEB ANALYTICS - WEEK 2 | Data Analytics
options(warn = -1)
suppressMessages(library(ggraph))
suppressMessages(library(tidygraph))options(warn = -1)
suppressMessages(library(igraph))
suppressMessages(library(dplyr))
suppressMessages(library(knitr))A glimspe and discription of dataset
g <- read_graph('polbooks.gml', format = "gml")
df <- as_tbl_graph(g)
df## # A tbl_graph: 105 nodes and 441 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 105 x 3 (active)
## id label value
## <dbl> <chr> <chr>
## 1 0 1000 Years for Revenge n
## 2 1 Bush vs. the Beltway c
## 3 2 Charlie Wilson's War c
## 4 3 Losing Bin Laden c
## 5 4 Sleeping With the Devil n
## 6 5 The Man Who Warned America c
## # ... with 99 more rows
## #
## # Edge Data: 441 x 2
## from to
## <int> <int>
## 1 1 2
## 2 1 3
## 3 1 4
## # ... with 438 more rows
The above gives a discription or structure of the data. We have 105 nodes and 441 edges.
Method 1:
df %>% activate(edges)## # A tbl_graph: 105 nodes and 441 edges
## #
## # An undirected simple graph with 1 component
## #
## # Edge Data: 441 x 2 (active)
## from to
## <int> <int>
## 1 1 2
## 2 1 3
## 3 1 4
## 4 2 4
## 5 1 5
## 6 3 5
## # ... with 435 more rows
## #
## # Node Data: 105 x 3
## id label value
## <dbl> <chr> <chr>
## 1 0 1000 Years for Revenge n
## 2 1 Bush vs. the Beltway c
## 3 2 Charlie Wilson's War c
## # ... with 102 more rows
kable(head(as_tibble(df)))| id | label | value |
|---|---|---|
| 0 | 1000 Years for Revenge | n |
| 1 | Bush vs. the Beltway | c |
| 2 | Charlie Wilson’s War | c |
| 3 | Losing Bin Laden | c |
| 4 | Sleeping With the Devil | n |
| 5 | The Man Who Warned America | c |
Method 2:
E(df)## + 441/441 edges:
## [1] 1-- 2 1-- 3 1-- 4 2-- 4 1-- 5 3-- 5 1-- 6 2-- 6 3-- 6 4-- 6
## [11] 5-- 6 1-- 7 2-- 7 5-- 7 6-- 7 3-- 8 6-- 8 7-- 8 4-- 9 4--10
## [21] 9--10 4--11 7--11 9--11 4--12 9--12 10--12 11--12 4--13 7--13
## [31] 9--13 10--13 11--13 12--13 4--14 9--14 12--14 13--14 4--15 9--15
## [41] 10--15 12--15 13--15 8--15 4--16 11--16 13--16 4--17 11--17 16--17
## [51] 4--18 12--18 13--18 14--18 4--19 7--19 13--19 4--20 11--20 4--21
## [61] 9--21 10--21 12--21 4--22 9--22 11--22 12--22 4--23 7--23 9--23
## [71] 12--23 4--24 9--24 13--24 22--24 4--25 9--25 10--25 13--25 21--25
## [81] 4--26 7--26 15--26 23--26 4--27 9--27 12--27 15--27 25--27 4--28
## [91] 9--28 10--28 12--28 24--28 5--29 5--30 7--30 12--30 14--30 5--31
## + ... omitted several edges
V(df)## + 105/105 vertices:
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
## [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
## [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
## [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
## [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
## [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
## [103] 103 104 105
We can bind the nodes or edges together (if you want!)
bind_nodes(df)## # A tbl_graph: 105 nodes and 441 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 105 x 3 (active)
## id label value
## <dbl> <chr> <chr>
## 1 0 1000 Years for Revenge n
## 2 1 Bush vs. the Beltway c
## 3 2 Charlie Wilson's War c
## 4 3 Losing Bin Laden c
## 5 4 Sleeping With the Devil n
## 6 5 The Man Who Warned America c
## # ... with 99 more rows
## #
## # Edge Data: 441 x 2
## from to
## <int> <int>
## 1 1 2
## 2 1 3
## 3 1 4
## # ... with 438 more rows
bind_edges(df)## # A tbl_graph: 105 nodes and 441 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 105 x 3 (active)
## id label value
## <dbl> <chr> <chr>
## 1 0 1000 Years for Revenge n
## 2 1 Bush vs. the Beltway c
## 3 2 Charlie Wilson's War c
## 4 3 Losing Bin Laden c
## 5 4 Sleeping With the Devil n
## 6 5 The Man Who Warned America c
## # ... with 99 more rows
## #
## # Edge Data: 441 x 2
## from to
## <int> <int>
## 1 1 2
## 2 1 3
## 3 1 4
## # ... with 438 more rows
plot(df)col <- c("brown", "blue")
plot(df, edge.arrow.size=.2, vertex.label.color="black", vertex.label.dist=1.1,
vertex.color=c( "brown", "blue")[1+(V(df)$value=="c")],layout=layout_in_circle)
legend(x=-1.1, y=-1.1, c("Label","Value"), pch=21,
col="#777777", pt.bg=col, pt.cex=2.5, bty="n", ncol=1)diameter(df, directed = TRUE, unconnected = FALSE, weights = NULL)## [1] 7
A histogram depicting the betweeness of the data.
df2 <- estimate_betweenness(df, vids = V(df), directed = TRUE, cutoff = 2, weights = NULL, nobigint = TRUE)
hist(df2, breaks = "Sturges", main = paste("Histogram of Betweenness"), col="brown",
xlab = "Betweeness",
axes = TRUE, plot = TRUE, labels = FALSE)#betweenness(df, v = V(df), directed = TRUE, weights = NULL, nobigint = TRUE, normalized = FALSE)
#edge_betweenness(df, e = E(df), directed = T, weights = NULL)shortest_paths(df, from =1, to = V(df), mode = c("out", "all", "in"),
weights = NULL, output = c("vpath", "epath", "both"),
predecessors = FALSE, inbound.edges = FALSE)## $vpath
## $vpath[[1]]
## + 1/105 vertex:
## [1] 1
##
## $vpath[[2]]
## + 2/105 vertices:
## [1] 1 2
##
## $vpath[[3]]
## + 2/105 vertices:
## [1] 1 3
##
## $vpath[[4]]
## + 2/105 vertices:
## [1] 1 4
##
## $vpath[[5]]
## + 2/105 vertices:
## [1] 1 5
##
## $vpath[[6]]
## + 2/105 vertices:
## [1] 1 6
##
## $vpath[[7]]
## + 2/105 vertices:
## [1] 1 7
##
## $vpath[[8]]
## + 3/105 vertices:
## [1] 1 3 8
##
## $vpath[[9]]
## + 3/105 vertices:
## [1] 1 4 9
##
## $vpath[[10]]
## + 3/105 vertices:
## [1] 1 4 10
##
## $vpath[[11]]
## + 3/105 vertices:
## [1] 1 4 11
##
## $vpath[[12]]
## + 3/105 vertices:
## [1] 1 4 12
##
## $vpath[[13]]
## + 3/105 vertices:
## [1] 1 4 13
##
## $vpath[[14]]
## + 3/105 vertices:
## [1] 1 4 14
##
## $vpath[[15]]
## + 3/105 vertices:
## [1] 1 4 15
##
## $vpath[[16]]
## + 3/105 vertices:
## [1] 1 4 16
##
## $vpath[[17]]
## + 3/105 vertices:
## [1] 1 4 17
##
## $vpath[[18]]
## + 3/105 vertices:
## [1] 1 4 18
##
## $vpath[[19]]
## + 3/105 vertices:
## [1] 1 4 19
##
## $vpath[[20]]
## + 3/105 vertices:
## [1] 1 4 20
##
## $vpath[[21]]
## + 3/105 vertices:
## [1] 1 4 21
##
## $vpath[[22]]
## + 3/105 vertices:
## [1] 1 4 22
##
## $vpath[[23]]
## + 3/105 vertices:
## [1] 1 4 23
##
## $vpath[[24]]
## + 3/105 vertices:
## [1] 1 4 24
##
## $vpath[[25]]
## + 3/105 vertices:
## [1] 1 4 25
##
## $vpath[[26]]
## + 3/105 vertices:
## [1] 1 4 26
##
## $vpath[[27]]
## + 3/105 vertices:
## [1] 1 4 27
##
## $vpath[[28]]
## + 3/105 vertices:
## [1] 1 4 28
##
## $vpath[[29]]
## + 3/105 vertices:
## [1] 1 5 29
##
## $vpath[[30]]
## + 3/105 vertices:
## [1] 1 5 30
##
## $vpath[[31]]
## + 3/105 vertices:
## [1] 1 5 31
##
## $vpath[[32]]
## + 3/105 vertices:
## [1] 1 5 32
##
## $vpath[[33]]
## + 4/105 vertices:
## [1] 1 4 9 33
##
## $vpath[[34]]
## + 4/105 vertices:
## [1] 1 4 9 34
##
## $vpath[[35]]
## + 5/105 vertices:
## [1] 1 4 9 36 35
##
## $vpath[[36]]
## + 4/105 vertices:
## [1] 1 4 9 36
##
## $vpath[[37]]
## + 4/105 vertices:
## [1] 1 4 13 37
##
## $vpath[[38]]
## + 4/105 vertices:
## [1] 1 4 9 38
##
## $vpath[[39]]
## + 4/105 vertices:
## [1] 1 4 11 39
##
## $vpath[[40]]
## + 4/105 vertices:
## [1] 1 4 11 40
##
## $vpath[[41]]
## + 4/105 vertices:
## [1] 1 4 9 41
##
## $vpath[[42]]
## + 4/105 vertices:
## [1] 1 4 9 42
##
## $vpath[[43]]
## + 4/105 vertices:
## [1] 1 4 9 43
##
## $vpath[[44]]
## + 4/105 vertices:
## [1] 1 4 9 44
##
## $vpath[[45]]
## + 4/105 vertices:
## [1] 1 4 9 45
##
## $vpath[[46]]
## + 4/105 vertices:
## [1] 1 4 9 46
##
## $vpath[[47]]
## + 4/105 vertices:
## [1] 1 4 9 47
##
## $vpath[[48]]
## + 4/105 vertices:
## [1] 1 4 10 48
##
## $vpath[[49]]
## + 4/105 vertices:
## [1] 1 4 10 49
##
## $vpath[[50]]
## + 4/105 vertices:
## [1] 1 4 10 50
##
## $vpath[[51]]
## + 4/105 vertices:
## [1] 1 4 10 51
##
## $vpath[[52]]
## + 4/105 vertices:
## [1] 1 4 10 52
##
## $vpath[[53]]
## + 4/105 vertices:
## [1] 1 4 10 53
##
## $vpath[[54]]
## + 4/105 vertices:
## [1] 1 4 21 54
##
## $vpath[[55]]
## + 4/105 vertices:
## [1] 1 4 13 55
##
## $vpath[[56]]
## + 4/105 vertices:
## [1] 1 4 11 56
##
## $vpath[[57]]
## + 4/105 vertices:
## [1] 1 4 12 57
##
## $vpath[[58]]
## + 4/105 vertices:
## [1] 1 4 14 58
##
## $vpath[[59]]
## + 4/105 vertices:
## [1] 1 3 8 59
##
## $vpath[[60]]
## + 5/105 vertices:
## [1] 1 5 31 100 60
##
## $vpath[[61]]
## + 5/105 vertices:
## [1] 1 5 31 85 61
##
## $vpath[[62]]
## + 5/105 vertices:
## [1] 1 5 31 87 62
##
## $vpath[[63]]
## + 5/105 vertices:
## [1] 1 5 31 85 63
##
## $vpath[[64]]
## + 5/105 vertices:
## [1] 1 5 31 100 64
##
## $vpath[[65]]
## + 5/105 vertices:
## [1] 1 3 8 59 65
##
## $vpath[[66]]
## + 5/105 vertices:
## [1] 1 3 8 59 66
##
## $vpath[[67]]
## + 4/105 vertices:
## [1] 1 5 29 67
##
## $vpath[[68]]
## + 4/105 vertices:
## [1] 1 5 31 68
##
## $vpath[[69]]
## + 5/105 vertices:
## [1] 1 3 8 59 69
##
## $vpath[[70]]
## + 5/105 vertices:
## [1] 1 3 8 59 70
##
## $vpath[[71]]
## + 4/105 vertices:
## [1] 1 5 31 71
##
## $vpath[[72]]
## + 4/105 vertices:
## [1] 1 3 8 72
##
## $vpath[[73]]
## + 4/105 vertices:
## [1] 1 5 29 73
##
## $vpath[[74]]
## + 4/105 vertices:
## [1] 1 5 31 74
##
## $vpath[[75]]
## + 4/105 vertices:
## [1] 1 5 31 75
##
## $vpath[[76]]
## + 4/105 vertices:
## [1] 1 5 31 76
##
## $vpath[[77]]
## + 4/105 vertices:
## [1] 1 5 31 77
##
## $vpath[[78]]
## + 4/105 vertices:
## [1] 1 4 20 78
##
## $vpath[[79]]
## + 4/105 vertices:
## [1] 1 5 32 79
##
## $vpath[[80]]
## + 4/105 vertices:
## [1] 1 5 31 80
##
## $vpath[[81]]
## + 4/105 vertices:
## [1] 1 5 31 81
##
## $vpath[[82]]
## + 5/105 vertices:
## [1] 1 3 8 72 82
##
## $vpath[[83]]
## + 4/105 vertices:
## [1] 1 5 31 83
##
## $vpath[[84]]
## + 4/105 vertices:
## [1] 1 5 31 84
##
## $vpath[[85]]
## + 4/105 vertices:
## [1] 1 5 31 85
##
## $vpath[[86]]
## + 4/105 vertices:
## [1] 1 3 8 86
##
## $vpath[[87]]
## + 4/105 vertices:
## [1] 1 5 31 87
##
## $vpath[[88]]
## + 5/105 vertices:
## [1] 1 5 29 73 88
##
## $vpath[[89]]
## + 5/105 vertices:
## [1] 1 5 29 67 89
##
## $vpath[[90]]
## + 5/105 vertices:
## [1] 1 5 29 67 90
##
## $vpath[[91]]
## + 5/105 vertices:
## [1] 1 5 29 67 91
##
## $vpath[[92]]
## + 4/105 vertices:
## [1] 1 5 32 92
##
## $vpath[[93]]
## + 5/105 vertices:
## [1] 1 5 29 73 93
##
## $vpath[[94]]
## + 4/105 vertices:
## [1] 1 5 31 94
##
## $vpath[[95]]
## + 5/105 vertices:
## [1] 1 5 31 74 95
##
## $vpath[[96]]
## + 5/105 vertices:
## [1] 1 5 31 74 96
##
## $vpath[[97]]
## + 5/105 vertices:
## [1] 1 5 29 67 97
##
## $vpath[[98]]
## + 5/105 vertices:
## [1] 1 5 29 67 98
##
## $vpath[[99]]
## + 5/105 vertices:
## [1] 1 5 31 74 99
##
## $vpath[[100]]
## + 4/105 vertices:
## [1] 1 5 31 100
##
## $vpath[[101]]
## + 5/105 vertices:
## [1] 1 5 29 67 101
##
## $vpath[[102]]
## + 5/105 vertices:
## [1] 1 5 31 85 102
##
## $vpath[[103]]
## + 5/105 vertices:
## [1] 1 4 9 47 103
##
## $vpath[[104]]
## + 5/105 vertices:
## [1] 1 5 31 68 104
##
## $vpath[[105]]
## + 5/105 vertices:
## [1] 1 5 31 68 105
##
##
## $epath
## NULL
##
## $predecessors
## NULL
##
## $inbound_edges
## NULL
#all_shortest_paths(graph, from, to = V(graph), mode = c("out", "all", "in"),weights = NULL)