-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.R
1422 lines (1166 loc) · 65.6 KB
/
analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Social Network Analysis ##
## Project II ##
## Dimitris Matsanganis FT f2822212 ##
####################################################
######################### START: Import Libraries ##############################
# Import the necessary libraries.
library(igraph)
library(ggplot2)
########################## END: Import Libraries ###############################
######################### START: Import Datasets ###############################
# The initial data origin from the provided link and is in a compressed format
# (tweets2009-07.txt.gz).
# You can find this file here:
# https://drive.google.com/file/d/1RjWUg-6KrVOjJPZHHQg-h_9gSSWZUPn-/view
# The compressed file contains the `tweets2009-07.txt` file.
# The above file was the input and through the created Python file
# `raw_data_handler.py` output the following 10 CSV files (5 for the mentions
# and 5 for the hashtags, one for each day).
# You can see more in the relevant Python file.
# The first step to parse the data in R is to import the datasets as dataframes.
# Import the user/mentions files.
df_mentions_07_01 = read.csv("2009.07.01_mentions.csv")
df_mentions_07_02 = read.csv("2009.07.02_mentions.csv")
df_mentions_07_03 = read.csv("2009.07.03_mentions.csv")
df_mentions_07_04 = read.csv("2009.07.04_mentions.csv")
df_mentions_07_05 = read.csv("2009.07.05_mentions.csv")
# Import the hashtags files.
df_hashtags_07_01 = read.csv("2009.07.01_hashtags.csv")
df_hashtags_07_02 = read.csv("2009.07.02_hashtags.csv")
df_hashtags_07_03 = read.csv("2009.07.03_hashtags.csv")
df_hashtags_07_04 = read.csv("2009.07.04_hashtags.csv")
df_hashtags_07_05 = read.csv("2009.07.05_hashtags.csv")
########################## END: Import Datasets ################################
########################## START: Data Handling ################################
# Now to make our analysis easier and to fulfill the instruction regarding
# having null/na to the user that do not contain any hashtag to their tweets
# we will merge the dataframes to create 5 based on the user and then replace
# the empty fields with the `Null/NA` notations as mentioned.
# Merge the dataframes based on the specified columns the from user and the
# user for the second file.
# 01/07/2009
df_07_01 = merge(df_mentions_07_01, df_hashtags_07_01, by.x = "from",
by.y = "user", all.x = TRUE)
# 02/07/2009
df_07_02 = merge(df_mentions_07_02, df_hashtags_07_02, by.x = "from",
by.y = "user", all.x = TRUE)
# 03/07/2009
df_07_03 = merge(df_mentions_07_03, df_hashtags_07_03, by.x = "from",
by.y = "user", all.x = TRUE)
# 04/07/2009
df_07_04 = merge(df_mentions_07_04, df_hashtags_07_04, by.x = "from",
by.y = "user", all.x = TRUE)
# 05/07/2009
df_07_05 = merge(df_mentions_07_05, df_hashtags_07_05, by.x = "from",
by.y = "user", all.x = TRUE)
# Now, since the Null/NA is not a hashtag we may remove the '#' from the
# beginning of this occurrences (no specific instruction is giver therefore,
# in case we need to maintain the '#' we ignore this commands).
df_07_01$topic_of_interest = gsub("#Null/NA", "Null/NA",
df_07_01$topic_of_interest)
df_07_02$topic_of_interest = gsub("#Null/NA", "Null/NA",
df_07_02$topic_of_interest)
df_07_03$topic_of_interest = gsub("#Null/NA", "Null/NA",
df_07_03$topic_of_interest)
df_07_04$topic_of_interest = gsub("#Null/NA", "Null/NA",
df_07_04$topic_of_interest)
df_07_05$topic_of_interest = gsub("#Null/NA", "Null/NA",
df_07_05$topic_of_interest)
# To avoid time consuming procedures and for backup purposes we export the 5
# final CSV files (Remove the comments to execute them!).
# write.csv(df_07_01, "2009.07.01.csv", row.names = FALSE)
# write.csv(df_07_02, "2009.07.02.csv", row.names = FALSE)
# write.csv(df_07_03, "2009.07.03.csv", row.names = FALSE)
# write.csv(df_07_04, "2009.07.04.csv", row.names = FALSE)
# write.csv(df_07_05, "2009.07.05.csv", row.names = FALSE)
# Re import them to skip the above procedure as a shortcut.
# df_07_01 = read.csv("2009.07.01.csv")
# df_07_02 = read.csv("2009.07.02.csv")
# df_07_03 = read.csv("2009.07.03.csv")
# df_07_04 = read.csv("2009.07.04.csv")
# df_07_05 = read.csv("2009.07.05.csv")
# Remove the initial dataframes since now are not needed, for clarity and
# optimization of memory purposes.
rm(df_mentions_07_01, df_mentions_07_02, df_mentions_07_03, df_mentions_07_04,
df_mentions_07_05, df_hashtags_07_01, df_hashtags_07_02, df_hashtags_07_03,
df_hashtags_07_04, df_hashtags_07_05)
########################### END: Data Handling #################################
###################### START: Q1: Graph Creation ###############################
# Now, we moved to the creation of the Graphs with the assistance of igraph.
# We have set the seed for re productivity purposes.
set.seed(2822212)
# Create the graphs with igraph library and the related dataframe.
graph_07_01 = graph_from_data_frame(df_07_01, directed=TRUE)
graph_07_02 = graph_from_data_frame(df_07_02, directed=TRUE)
graph_07_03 = graph_from_data_frame(df_07_03, directed=TRUE)
graph_07_04 = graph_from_data_frame(df_07_04, directed=TRUE)
graph_07_05 = graph_from_data_frame(df_07_05, directed=TRUE)
# Update graph vertices with topic_of_interest attribute - the most
# frequently used hashtag.
graph_07_01 = set_vertex_attr(graph_07_01, name = "topic_of_interest",
value = df_07_01$topic_of_interest[V(graph_07_01)])
graph_07_02 = set_vertex_attr(graph_07_02, name = "topic_of_interest",
value = df_07_02$topic_of_interest[V(graph_07_02)])
graph_07_03 = set_vertex_attr(graph_07_03, name = "topic_of_interest",
value = df_07_03$topic_of_interest[V(graph_07_03)])
graph_07_04 = set_vertex_attr(graph_07_04, name = "topic_of_interest",
value = df_07_04$topic_of_interest[V(graph_07_04)])
graph_07_05 = set_vertex_attr(graph_07_05, name = "topic_of_interest",
value = df_07_05$topic_of_interest[V(graph_07_05)])
# Validate that the above modification has taken place.
# Check if the "topic_of_interest" attribute is present in the vertices
# has_attribute = "topic_of_interest" %in% names(vertex_attr(graph_07_01))
#
# if (has_attribute)
# {
# cat("The 'topic_of_interest' attribute is present in the subgraph vertices.\n")
# }
# else
# {
# cat("The 'topic_of_interest' attribute is NOT present in the subgraph vertices.\n")
# }
####################### END: Q1: Graph Creation ################################
#################### START: Q2: Average degree over time #######################
# Number of vertices for each graph.
number_of_vertices = c(gorder(graph_07_01), gorder(graph_07_02),
gorder(graph_07_03), gorder(graph_07_04),
gorder(graph_07_05))
# Create a table with labels.
table_data = data.frame(row.names = c("July 1st", "July 2nd", "July 3rd",
"July 4th", "July 5th"),
number_of_vertices = number_of_vertices)
# Preview the table.
table_data
# Number of edges for each graph.
number_of_edges = c(gsize(graph_07_01), gsize(graph_07_02), gsize(graph_07_03),
gsize(graph_07_04), gsize(graph_07_05))
number_of_edges
# Add Number of Edges to the table.
table_data$number_of_edges = number_of_edges
# Rename the column.
colnames(table_data)[1] = "Number of Vertices"
colnames(table_data)[2] = "Number of Edges"
# Preview the updated table.
table_data
# Validation checks prior to find the diameter.
is.weighted(graph_07_01)
E(graph_07_01)$weight
# Calculate the diameter of each graph (weighted).
diameter_of_the_graph1 = diameter(graph_07_01, directed = TRUE,
weights= E(graph_07_01)$weight)
diameter_of_the_graph2 = diameter(graph_07_02, directed = TRUE,
weights= E(graph_07_02)$weight)
diameter_of_the_graph3 = diameter(graph_07_03, directed = TRUE,
weights= E(graph_07_03)$weight)
diameter_of_the_graph4 = diameter(graph_07_04, directed = TRUE,
weights= E(graph_07_04)$weight)
diameter_of_the_graph5 = diameter(graph_07_05, directed = TRUE,
weights= E(graph_07_05)$weight)
# Combine the diameter values into a vector.
diameter_of_the_graphs = c(diameter_of_the_graph1, diameter_of_the_graph2,
diameter_of_the_graph3, diameter_of_the_graph4,
diameter_of_the_graph5)
# Preview.
diameter_of_the_graphs
# Add diameter values to the table.
table_data$Diameter = c(diameter_of_the_graph1, diameter_of_the_graph2,
diameter_of_the_graph3, diameter_of_the_graph4,
diameter_of_the_graph5)
# Rename the column.
colnames(table_data)[3] = "Diameter (Weighted)"
# Preview the updated table.
table_data
# Calculate the diameter of each graph (without weights).
diameter_of_the_graph1_no_weights = diameter(graph_07_01, directed = TRUE,
weights = NA)
diameter_of_the_graph2_no_weights = diameter(graph_07_02, directed = TRUE,
weights = NA)
diameter_of_the_graph3_no_weights = diameter(graph_07_03, directed = TRUE,
weights = NA)
diameter_of_the_graph4_no_weights = diameter(graph_07_04, directed = TRUE,
weights = NA)
diameter_of_the_graph5_no_weights = diameter(graph_07_05, directed = TRUE,
weights = NA)
# Combine the diameter values into a vector.
diameter_of_the_graphs_no_weights = c(diameter_of_the_graph1_no_weights,
diameter_of_the_graph2_no_weights,
diameter_of_the_graph3_no_weights,
diameter_of_the_graph4_no_weights,
diameter_of_the_graph5_no_weights)
# Preview.
diameter_of_the_graphs_no_weights
# Add diameter values to the table.
table_data$Diameter = c(diameter_of_the_graph1_no_weights,
diameter_of_the_graph2_no_weights,
diameter_of_the_graph3_no_weights,
diameter_of_the_graph4_no_weights,
diameter_of_the_graph5_no_weights)
# Rename the column.
colnames(table_data)[4] = "Diameter (Not Weighted)"
# Preview the updated table.
table_data
# Calculate the average in-degree for each graph.
in_degree = round(sapply(1:5, function(i) {
mean(degree(get(paste0("graph_07_0", i)),
v = V(get(paste0("graph_07_0", i))),
mode = c("in"), loops = FALSE, normalized = FALSE))}), 2)
# Calculate the average out-degree for each graph.
out_degree = round(sapply(1:5, function(i) {
mean(degree(get(paste0("graph_07_0", i)),
v = V(get(paste0("graph_07_0", i))),
mode = c("out"), loops = FALSE, normalized = FALSE))}), 2)
# Add average in-degree and out-degree to the table.
table_data$Avg_In_Degree = in_degree
table_data$Avg_Out_Degree = out_degree
# Rename the columns.
colnames(table_data)[5] = "Average In-Degree"
colnames(table_data)[6] = "Average Out-Degree"
# Preview the updated and final table.
table_data
# Plots of the above findings.
# The first step we need to do is to define the above table 'table_data', as a
# dataframe for all the created plot to extract each time the wanted data.
# Create a data frame for table
df_q2_data = as.data.frame(table_data)
# Reset the index and redefine the Date column as a new column.
# (Note: It contains dates in string format as July 1st etc).
df_q2_data$Date = rownames(df_q2_data)
rownames(df_q2_data) = NULL
# Number of vertices plot.
# Barplot Analyzing the 5 Days Trend Regarding the Number of Vertices.
p_vertices_barplot = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Number of Vertices`,
fill = `Date`)) +
geom_bar(stat = "identity", color = "black", width = 0.5) +
geom_text(aes(label = `Number of Vertices`, y = `Number of Vertices`),
position = position_stack(vjust = 1.03),
size = 4, color = "black") +
labs(x = "Dates of July", y = "Number of Vertices") +
ggtitle("Barplot Analyzing the 5 Days Trend Regarding the Number of Vertices") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(0, 510000)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_vertices_barplot
# Trendline Diagram for Number of Vertices over the 5 Days.
p_vertices_trendline = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Number of Vertices`,
group = 1)) +
geom_point(size = 4, color = "dodgerblue3", shape = 18) +
geom_line() +
geom_text(aes(label = `Number of Vertices`), vjust = -1) +
labs(x = "Dates of July", y = "Number of Vertices",
title = "Trendline Diagram for Number of Vertices over the 5 Days") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3")) +
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(150000, 530000)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_vertices_trendline
# Number of edges plot.
# Barplot Analyzing the 5 Days Trend Regarding the Number of Edges.
p_edges_barplot = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Number of Edges`,
fill = `Date`)) +
geom_bar(stat = "identity", color = "black", width = 0.5) +
geom_text(aes(label = `Number of Edges`, y = `Number of Edges`),
position = position_stack(vjust = 1.04),
size = 4, color = "black") +
labs(x = "Dates of July", y = "Number of Edges") +
ggtitle("Barplot Analyzing the 5 Days Trend Regarding the Number of Edges") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(0, 580000)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_edges_barplot
# Trendline Diagram for Number of Edges over the 5 Days.
p_edges_trendline = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Number of Edges`,
group = 1)) +
geom_point(size = 4, color = "dodgerblue3", shape = 18) +
geom_line() +
geom_text(aes(label = `Number of Edges`), vjust = -1) +
labs(x = "Dates of July", y = "Number of Edges",
title = "Trendline Diagram for Number of Edges over the 5 Days") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3")) +
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(200000, 560000)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_edges_trendline
# Diameter of the graph (weighted) plot.
# Barplot Analyzing the 5 Days Trend Regarding the Diameter of the Graph (weighted).
p_diameter_barplot = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Diameter (Weighted)`,
fill = `Date`)) +
geom_bar(stat = "identity", color = "black", width = 0.5) +
geom_text(aes(label = `Diameter (Weighted)`, y = `Diameter (Weighted)`),
position = position_stack(vjust = 1.02),
size = 4, color = "black") +
labs(x = "Dates of July", y = "Diameter of the Graph (weighted)") +
ggtitle("Barplot Analyzing the 5 Days Trend Regarding the Diameter of the Graph (weighted)") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(40, 100)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_diameter_barplot
# Trendline Diagram for Diameter of the Graph over the 5 Days (weighted).
p_diameter_trendline = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Diameter (Weighted)`,
group = 1)) +
geom_point(size = 5, color = "dodgerblue3", shape = 18) +
geom_line() +
geom_text(aes(label = `Diameter (Weighted)`), vjust = -1) +
labs(x = "Dates of July", y = "Diameter of the Graph (weighted)",
title = "Trendline Diagram for Diameter of the Graph over the 5 Days (weighted)") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3")) +
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(50, 100)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_diameter_trendline
# Diameter of the graph (not weighted) plot.
# Barplot Analyzing the 5 Days Trend Regarding the Diameter of the Graph (not weighted).
p_diameterNW_barplot = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Diameter (Not Weighted)`,
fill = `Date`)) +
geom_bar(stat = "identity", color = "black", width = 0.5) +
geom_text(aes(label = `Diameter (Not Weighted)`, y = `Diameter (Not Weighted)`),
position = position_stack(vjust = 1.02),
size = 4, color = "black") +
labs(x = "Dates of July", y = "Diameter of the Graph (not weighted)") +
ggtitle("Barplot Analyzing the 5 Days Trend Regarding the Diameter of the Graph (not weighted)") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(28, 70)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_diameterNW_barplot
# Trendline Diagram for Diameter of the Graph over the 5 Days (not weighted).
p_diameterNW_trendline = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Diameter (Not Weighted)`,
group = 1)) +
geom_point(size = 5, color = "dodgerblue3", shape = 18) +
geom_line() +
geom_text(aes(label = `Diameter (Not Weighted)`), vjust = -1) +
labs(x = "Dates of July", y = "Diameter of the Graph (not weighted)",
title = "Trendline Diagram for Diameter of the Graph over the 5 Days (not weighted)") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3")) +
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(30, 65)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_diameterNW_trendline
# Average in-degree plot.
# Barplot Analyzing the 5 Days Trend Regarding the Average In-Degree.
p_indegree_barplot = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Average In-Degree`,
fill = `Date`)) +
geom_bar(stat = "identity", color = "black", width = 0.5) +
geom_text(aes(label = `Average In-Degree`, y = `Average In-Degree`),
position = position_stack(vjust = 1.01),
size = 4, color = "black") +
labs(x = "Dates of July", y = "Average In-Degree") +
ggtitle("Barplot Analyzing the 5 Days Trend Regarding the Average In-Degree") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(1, 1.34)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_indegree_barplot
# Trendline Diagram for the Average In-Degree over the 5 Days.
p_indegree_trendline = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Average In-Degree`,
group = 1)) +
geom_point(size = 5, color = "dodgerblue3", shape = 18) +
geom_line() +
geom_text(aes(label = `Average In-Degree`), vjust = -1) +
labs(x = "Dates of July", y = "Average In-Degree",
title = "Trendline Diagram for the Average In-Degree over the 5 Days") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3")) +
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(1.04, 1.34)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_indegree_trendline
# Average Out-Degree plot.
# Barplot Analyzing the 5 Days Trend Regarding the Average Out-Degree.
p_outdegree_barplot = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Average Out-Degree`,
fill = `Date`)) +
geom_bar(stat = "identity", color = "black", width = 0.5) +
geom_text(aes(label = `Average Out-Degree`, y = `Average Out-Degree`),
position = position_stack(vjust = 1.01),
size = 4, color = "black") +
labs(x = "Dates of July", y = "Average Out-Degree") +
ggtitle("Barplot Analyzing the 5 Days Trend Regarding the Average Out-Degree") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(1, 1.34)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_outdegree_barplot
# Trendline Diagram for the Average Out-Degree over the 5 Days.
p_outdegree_trendline = ggplot(data = df_q2_data,
aes(x = `Date`,
y = `Average Out-Degree`,
group = 1)) +
geom_point(size = 5, color = "dodgerblue3", shape = 18) +
geom_line() +
geom_text(aes(label = `Average Out-Degree`), vjust = -1) +
labs(x = "Dates of July", y = "Average Out-Degree",
title = "Trendline Diagram for the Average Out-Degree over the 5 Days") +
theme_minimal() +
theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.5,
family = "Arial", color = "dodgerblue3")) +
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 14, family = "Arial",
color = "dodgerblue3"),
legend.position = "none",
axis.line = element_line(color = "black", size = 0.5)) +
coord_cartesian(ylim = c(1.04, 1.34)) +
scale_y_continuous(labels = scales::comma)
# Preview the plot.
p_outdegree_trendline
##################### END: Q2: Average degree over time ########################
######################## START: Q3: Important nodes ############################
# Top-10 Twitter users with regard to In-Degree.
# Create a data frame to store the results.
result_table_in_degree = data.frame()
# July 1st 2009 In-Degree.
df_in_07_01 = data.frame(in_degree_07_01 = sort(strength(graph_07_01,
vids = V(graph_07_01),
mode = "in",
loops = FALSE,
weights = E(graph_07_01)$weight),
decreasing = TRUE))
df_in_07_01 = df_in_07_01[order(-df_in_07_01$in_degree_07_01), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_in_07_01, 10)
# Append to the result table.
result_table_in_degree = cbind(names_in0701 = rownames(head(df_in_07_01, 10)))
result_table_in_degree = cbind(result_table_in_degree, head(df_in_07_01, 10))
# July 2nd 2009 In-Degree.
df_in_07_02 = data.frame(in_degree_07_02 = sort(strength(graph_07_02,
vids = V(graph_07_02),
mode = "in",
loops = FALSE,
weights = E(graph_07_02)$weight),
decreasing = TRUE))
df_in_07_02 = df_in_07_02[order(-df_in_07_02$in_degree_07_02), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_in_07_02, 10)
# Append to the result table.
result_table_in_degree = cbind(result_table_in_degree, names_in0702 = rownames(head(df_in_07_02, 10)))
result_table_in_degree = cbind(result_table_in_degree, head(df_in_07_02, 10))
# July 3rd 2009 In-Degree.
df_in_07_03 = data.frame(in_degree_07_03 = sort(strength(graph_07_03,
vids = V(graph_07_03),
mode = "in",
loops = FALSE,
weights = E(graph_07_03)$weight),
decreasing = TRUE))
df_in_07_03 = df_in_07_03[order(-df_in_07_03$in_degree_07_03), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_in_07_03, 10)
# Append to the result table.
result_table_in_degree = cbind(result_table_in_degree, names_in0703 = rownames(head(df_in_07_03, 10)))
result_table_in_degree = cbind(result_table_in_degree, head(df_in_07_03, 10))
# July 4th 2009 In-Degree.
df_in_07_04 = data.frame(in_degree_07_04 = sort(strength(graph_07_04,
vids = V(graph_07_04),
mode = "in",
loops = FALSE,
weights = E(graph_07_04)$weight),
decreasing = TRUE))
df_in_07_04 = df_in_07_04[order(-df_in_07_04$in_degree_07_04), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_in_07_04, 10)
# Append to the result table.
result_table_in_degree = cbind(result_table_in_degree, names_in0704 = rownames(head(df_in_07_04, 10)))
result_table_in_degree = cbind(result_table_in_degree, head(df_in_07_04, 10))
# July 5th 2009 In-Degree.
df_in_07_05 = data.frame(in_degree_07_05 = sort(strength(graph_07_05,
vids = V(graph_07_05),
mode = "in",
loops = FALSE,
weights = E(graph_07_05)$weight),
decreasing = TRUE))
df_in_07_05 = df_in_07_05[order(-df_in_07_05$in_degree_07_05), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_in_07_05, 10)
# Append to the result table.
result_table_in_degree = cbind(result_table_in_degree, names_in0705 = rownames(head(df_in_07_05, 10)))
result_table_in_degree = cbind(result_table_in_degree, head(df_in_07_05, 10))
# Reset the index to 1-10.
row.names(result_table_in_degree) = 1:10
# Rename the columns with more related names.
colnames(result_table_in_degree) = c('Users July 1st', 'In-Degree July 1st',
'Users July 2nd', 'In-Degree July 2nd',
'Users July 3rd', 'In-Degree July 3rd',
'Users July 4th', 'In-Degree July 4th',
'Users July 5th', 'In-Degree July 5th')
# Preview the results for In-Degree.
result_table_in_degree
################################################################################
# Top-10 Twitter users with regard to Out-Degree.
# Create a data frame to store the results.
result_table_out_degree = data.frame()
# July 1st 2009 Out-Degree.
df_out_07_01 = data.frame(out_degree_07_01 = sort(strength(graph_07_01,
vids = V(graph_07_01),
mode = "out",
loops = FALSE,
weights = E(graph_07_01)$weight),
decreasing = TRUE))
df_out_07_01 = df_out_07_01[order(-df_out_07_01$out_degree_07_01), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_out_07_01, 10)
# Append to the result table.
result_table_out_degree = cbind(names_out0701 = rownames(head(df_out_07_01, 10)))
result_table_out_degree = cbind(result_table_out_degree, head(df_out_07_01, 10))
# July 2nd 2009 Out-Degree.
df_out_07_02 = data.frame(out_degree_07_02 = sort(strength(graph_07_02,
vids = V(graph_07_02),
mode = "out",
loops = FALSE,
weights = E(graph_07_02)$weight),
decreasing = TRUE))
df_out_07_02 = df_out_07_02[order(-df_out_07_02$out_degree_07_02), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_out_07_02, 10)
# Append to the result table.
result_table_out_degree = cbind(result_table_out_degree, names_out0702 = rownames(head(df_out_07_02, 10)))
result_table_out_degree = cbind(result_table_out_degree, head(df_out_07_02, 10))
# July 3rd 2009 Out-Degree.
df_out_07_03 = data.frame(out_degree_07_03 = sort(strength(graph_07_03,
vids = V(graph_07_03),
mode = "out",
loops = FALSE,
weights = E(graph_07_03)$weight),
decreasing = TRUE))
df_out_07_03 = df_out_07_03[order(-df_out_07_03$out_degree_07_03), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_out_07_03, 10)
# Append to the result table.
result_table_out_degree = cbind(result_table_out_degree, names_out0703 = rownames(head(df_out_07_03, 10)))
result_table_out_degree = cbind(result_table_out_degree, head(df_out_07_03, 10))
# July 4th 2009 Out-Degree.
df_out_07_04 = data.frame(out_degree_07_04 = sort(strength(graph_07_04,
vids = V(graph_07_04),
mode = "out",
loops = FALSE,
weights = E(graph_07_04)$weight),
decreasing = TRUE))
df_out_07_04 = df_out_07_04[order(-df_out_07_04$out_degree_07_04), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_out_07_04, 10)
# Append to the result table.
result_table_out_degree = cbind(result_table_out_degree, names_out0704 = rownames(head(df_out_07_04, 10)))
result_table_out_degree = cbind(result_table_out_degree, head(df_out_07_04, 10))
# July 5th 2009 Out-Degree.
df_out_07_05 = data.frame(out_degree_07_05 = sort(strength(graph_07_05,
vids = V(graph_07_05),
mode = "out",
loops = FALSE,
weights = E(graph_07_05)$weight),
decreasing = TRUE))
df_out_07_05 = df_out_07_05[order(-df_out_07_05$out_degree_07_05), , drop = FALSE]
# Preview the results (only the top 10).
# head(df_out_07_05, 10)
# Append to the result table.
result_table_out_degree = cbind(result_table_out_degree, names_out0705 = rownames(head(df_out_07_05, 10)))
result_table_out_degree = cbind(result_table_out_degree, head(df_out_07_05, 10))
# Reset the index to 1-10.
row.names(result_table_out_degree) = 1:10
# Rename the columns with more related names.
colnames(result_table_out_degree) = c('Users July 1st', 'Out-Degree July 1st',
'Users July 2nd', 'Out-Degree July 2nd',
'Users July 3rd', 'Out-Degree July 3rd',
'Users July 4th', 'Out-Degree July 4th',
'Users July 5th', 'Out-Degree July 5th')
# Preview the results for Out-Degree.
result_table_out_degree
################################################################################
# Top-10 Twitter users with regard to PageRank.
# Create a data frame to store the results.
result_table_rank = data.frame()
# July 1st 2009 PageRank.
rank1 = page_rank(graph_07_01, algo = "prpack",
vids = V(graph_07_01), directed = TRUE, damping = 0.85,
personalized = NULL, weights = E(graph_07_01)$weight)
# July 2nd 2009 PageRank.
rank2 = page_rank(graph_07_02, algo = "prpack",
vids = V(graph_07_02), directed = TRUE, damping = 0.85,
personalized = NULL, weights = E(graph_07_02)$weight)
# July 3rd 2009 PageRank.
rank3 = page_rank(graph_07_03, algo = "prpack",
vids = V(graph_07_03), directed = TRUE, damping = 0.85,
personalized = NULL, weights = E(graph_07_03)$weight)
# July 4th 2009 PageRank.
rank4 = page_rank(graph_07_04, algo = "prpack",
vids = V(graph_07_04), directed = TRUE, damping = 0.85,
personalized = NULL, weights = E(graph_07_04)$weight)
# July 5th 2009 PageRank.
rank5 = page_rank(graph_07_05, algo = "prpack",
vids = V(graph_07_05), directed = TRUE, damping = 0.85,
personalized = NULL, weights = E(graph_07_05)$weight)
# Append to the result table using cbind (exact values).
result_table_rank = cbind(Rank_day1 = row.names(data.frame(Rank_day1 = head(sort(rank1$vector, decreasing = TRUE), 10))),
Value_day1 = head(sort(rank1$vector, decreasing = TRUE), 10),
Rank_day2 = row.names(data.frame(Rank_day2 = head(sort(rank2$vector, decreasing = TRUE), 10))),
Value_day2 = head(sort(rank2$vector, decreasing = TRUE), 10),
Rank_day3 = row.names(data.frame(Rank_day3 = head(sort(rank3$vector, decreasing = TRUE), 10))),
Value_day3 = head(sort(rank3$vector, decreasing = TRUE), 10),
Rank_day4 = row.names(data.frame(Rank_day4 = head(sort(rank4$vector, decreasing = TRUE), 10))),
Value_day4 = head(sort(rank4$vector, decreasing = TRUE), 10),
Rank_day5 = row.names(data.frame(Rank_day5 = head(sort(rank5$vector, decreasing = TRUE), 10))),
Value_day5 = head(sort(rank5$vector, decreasing = TRUE), 10))
# Reset the index to 1-10.
row.names(result_table_rank) = 1:10
# Rename the columns with more related names.
colnames(result_table_rank) = c('Users July 1st', 'PageRank July 1st',
'Users July 2nd', 'PageRank July 2nd',
'Users July 3rd', 'PageRank July 3rd',
'Users July 4th', 'PageRank July 4th',
'Users July 5th', 'PageRank July 5th')
# Preview the results for PageRank.
result_table_rank
# Lets round the results for better visualization.
# Append to the result table using cbind and round the values to 5 decimal places.
result_table_rank_rounded = cbind(Rank_day1 = row.names(data.frame(Rank_day1 = head(sort(rank1$vector, decreasing = TRUE), 10))),
Value_day1 = round(head(sort(rank1$vector, decreasing = TRUE), 10), 5),
Rank_day2 = row.names(data.frame(Rank_day2 = head(sort(rank2$vector, decreasing = TRUE), 10))),
Value_day2 = round(head(sort(rank2$vector, decreasing = TRUE), 10), 5),
Rank_day3 = row.names(data.frame(Rank_day3 = head(sort(rank3$vector, decreasing = TRUE), 10))),
Value_day3 = round(head(sort(rank3$vector, decreasing = TRUE), 10), 5),
Rank_day4 = row.names(data.frame(Rank_day4 = head(sort(rank4$vector, decreasing = TRUE), 10))),
Value_day4 = round(head(sort(rank4$vector, decreasing = TRUE), 10), 5),
Rank_day5 = row.names(data.frame(Rank_day5 = head(sort(rank5$vector, decreasing = TRUE), 10))),
Value_day5 = round(head(sort(rank5$vector, decreasing = TRUE), 10), 5))
# Reset the index to 1-10.
row.names(result_table_rank_rounded) = 1:10
# Rename the columns with more related names.
colnames(result_table_rank_rounded) = c('Users July 1st', 'PageRank July 1st',
'Users July 2nd', 'PageRank July 2nd',
'Users July 3rd', 'PageRank July 3rd',
'Users July 4th', 'PageRank July 4th',
'Users July 5th', 'PageRank July 5th')
# Preview the results for PageRank.
result_table_rank_rounded
######################### END: Q3: Important nodes #############################
########################## START: Q4: Communities ##############################
# Convert mention graphs to undirected graphs.
undirected_graph_07_01 = as.undirected(graph_07_01)
undirected_graph_07_02 = as.undirected(graph_07_02)
undirected_graph_07_03 = as.undirected(graph_07_03)
undirected_graph_07_04 = as.undirected(graph_07_04)
undirected_graph_07_05 = as.undirected(graph_07_05)
# Fast Greedy Clustering.
fast_greedy_communities_07_01 = cluster_fast_greedy(undirected_graph_07_01)
fast_greedy_communities_07_02 = cluster_fast_greedy(undirected_graph_07_02)
fast_greedy_communities_07_03 = cluster_fast_greedy(undirected_graph_07_03)
fast_greedy_communities_07_04 = cluster_fast_greedy(undirected_graph_07_04)
fast_greedy_communities_07_05 = cluster_fast_greedy(undirected_graph_07_05)
# Infomap Clustering.
infomap_communities_07_01 = cluster_infomap(undirected_graph_07_01)
infomap_communities_07_02 = cluster_infomap(undirected_graph_07_02)
infomap_communities_07_03 = cluster_infomap(undirected_graph_07_03)
infomap_communities_07_04 = cluster_infomap(undirected_graph_07_04)
infomap_communities_07_05 = cluster_infomap(undirected_graph_07_05)
# Louvain Clustering.
louvain_communities_07_01 = cluster_louvain(undirected_graph_07_01)
louvain_communities_07_02 = cluster_louvain(undirected_graph_07_02)
louvain_communities_07_03 = cluster_louvain(undirected_graph_07_03)
louvain_communities_07_04 = cluster_louvain(undirected_graph_07_04)
louvain_communities_07_05 = cluster_louvain(undirected_graph_07_05)
# Comment on the performance of the algorithms.
# Although, the performance of each algorithm can be evaluated based on factors
# such as modularity, community structure, and computational efficiency.
# Fast Greedy Clustering:
# This algorithm is known for its fast execution and ability to handle large
# graphs. It often produces good community structures but may not always
# find the globally optimal solution.
# Infomap Clustering:
# Infomap is based on the idea of optimizing the compression of a random
# walker's trajectory on the graph.
# It tends to generate high-quality community structures, capturing both
# local and global patterns. However, it can be relatively slower compared
# to other algorithms.
# Louvain Clustering:
# Louvain is a popular and efficient algorithm that optimizes modularity.
# It often produces well-defined communities with relatively fast execution.
# However, we need to notice that, it can be sensitive to the order in which
# nodes are processed and may not always find the globally optimal solution.
# It's important to note that the performance of these algorithms can
# vary depending on the characteristics of the graph and the specific
# community structure within the mention graphs.
# It's recommended to evaluate the results based on your specific dataset
# and application requirements.
# Select a random user present in all graphs & continue with Louvain.
random_user = "tweetmeme"
# Populate community membership for the selected user in each graph.
membership(louvain_communities_07_01)[random_user]
membership(louvain_communities_07_02)[random_user]
membership(louvain_communities_07_03)[random_user]
membership(louvain_communities_07_04)[random_user]
membership(louvain_communities_07_05)[random_user]
# Analyze the evolution of communities for the user
# and extract important topics of interest.
community_size_07_01 = sizes(louvain_communities_07_01)
community_size_07_02 = sizes(louvain_communities_07_02)
community_size_07_03 = sizes(louvain_communities_07_03)
community_size_07_04 = sizes(louvain_communities_07_04)
community_size_07_05 = sizes(louvain_communities_07_05)
# July 1st.
day_07_01_communities = membership(louvain_communities_07_01)
mid_community_07_01 = unlist(louvain_communities_07_01[community_size_07_01 > 40 & community_size_07_01 < 80])
graph_07_01_subgraph = induced_subgraph(graph_07_01, mid_community_07_01)
V(graph_07_01_subgraph )$color = factor(day_07_01_communities[mid_community_07_01])
plot(graph_07_01_subgraph ,
vertex.label = NA,
edge.arrow.width = 0.8,
edge.arrow.size = 0.2,
layout = layout_with_fr(graph_07_01_subgraph),
margin = 0,
vertex.size = 3,
main = "July 1st, 2009 Twitter Communities")
# July 2nd.
day_07_02_communities = membership(louvain_communities_07_02)
mid_community_07_02 = unlist(louvain_communities_07_02[community_size_07_02 > 40 & community_size_07_02 < 90])
graph_07_02_subgraph = induced_subgraph(graph_07_02, mid_community_07_02)
V(graph_07_02_subgraph)$color = factor(day_07_02_communities[mid_community_07_02])
plot(graph_07_02_subgraph,
vertex.label = NA,
edge.arrow.width = 0.8,
edge.arrow.size = 0.2,
layout = layout_with_fr(graph_07_02_subgraph),
margin = 0,