-
Notifications
You must be signed in to change notification settings - Fork 3
/
GOT.Rmd
969 lines (636 loc) · 33.8 KB
/
GOT.Rmd
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
---
title: "Network Analysis on Game of Thrones"
output:
html_document: default
html_notebook: default
---
In this article, I will be exploring and doing network analysis of the families of major characters from Game of Thrones which is a very famous TV series.
If you are a big Game of thrones fan then you should know that surprisingly, __House Stark__ (specifically Ned and Sansa) and __House Lannister__ (especially Tyrion) are the most important family connections in Game of Thrones; they also connect many of the storylines and are central parts of the Series.
##What is a Network ?
A network in this context is a graph of interconnected nodes/vertices. Nodes can e.g. be people in a social network, genes in a co-expression network, etc. Nodes are connected via edges.
##What can network analysis tell us?
Network analysis can e.g. be used to explore relationships in social or professional networks. In such cases, we would typically ask questions like:
1)How many connections does each person have?
2)Who is the most connected (i.e."famous","Dominating","influential" or
“important”) person?
3)Are there clusters of tightly connected people?
4)Are there a few key objects that connect clusters of people?
etc.
The answers can give us a lots of information and insights of lots of things and how people interact.
-----------------------
##Implementing It in R
Loading the Required Packages
```{r,warning=FALSE,message=FALSE}
require(tidyverse)
require(ggthemes)
require(igraph)
require(statnet)
```
Now loading the Game of Thrones Datasets
```{r}
#Loading the Datasets
battle<-read.csv("battles.csv")
#Deaths on characters Dataset
deaths<-read.csv("character-deaths.csv")
#pred dataset
pred<-read.csv("character-predictions.csv")
```
###Data Preprocessing and Transformation
```{r}
pred[pred == ""] <- NA
#Converting Individual columns to Character data type
pred$name <- as.character(pred$name)
pred$house <- as.character(pred$house)
pred$mother <- as.character(pred$mother)
pred$father <- as.character(pred$father)
pred$spouse <- as.character(pred$spouse)
pred$house <- ifelse(is.na(pred$house) & grepl("Targaryen", pred$name), "House Targaryen", pred$house)
pred$house <- ifelse(is.na(pred$house) & grepl("Stark", pred$name), "House Stark", pred$house)
#gsub() to perform replacement and pattern matching
pred$name <- gsub("[[:punct:]]", "", pred$name)
```
####Creating a new Child_of Dataframe which will contain the Parents and the child's name.
```{r}
child_of <- data.frame(name = pred$name[grepl("son of | daughter of", pred$name)],
stringsAsFactors = FALSE)
child_of$child_of <- ifelse(grepl("daughter", child_of$name), paste(gsub("(.* daughter of )(.*)", "\\2", child_of$name),
gsub("(.* )(.*)( daughter of.*)", "\\2", child_of$name)), paste(gsub("(.* son of )(.*)", "\\2", child_of$name),
gsub("(.* )(.*)( son of.*)", "\\2", child_of$name)))
child_of[child_of$name == "Ulf son of Umar", "child_of"] <- "Umar"
```
Performing a Left Join between the Child_of and Pred Datasets for joining both the tables Together i.e Merging them together with a similar column.
It is similar to using *__'merge()'__* function in R
```{r}
#Joining the 2 tables by common "name" column
pred<-left_join(pred, child_of, by = "name")
#Now I am manually adding the Mother and Father names for the given children Names
#Inside the brackets the first argument is the condition and second is the the name of the column for which you want to add value
pred[pred$name == "Arya Stark", "mother"] <- "Catelyn Stark"
pred[pred$name == "Arya Stark", "father"] <- "Eddard Stark"
pred[pred$name == "Bran Stark", "mother"] <- "Catelyn Stark"
pred[pred$name == "Bran Stark", "father"] <- "Eddard Stark"
pred[pred$name == "Sansa Stark", "mother"] <- "Catelyn Stark"
pred[pred$name == "Sansa Stark", "father"] <- "Eddard Stark"
pred[pred$name == "Rickon Stark", "mother"] <- "Catelyn Stark"
pred[pred$name == "Rickon Stark", "father"] <- "Eddard Stark"
pred[pred$name == "Jon Snow", "mother"] <- "Lyanna Stark"
pred[pred$name == "Jon Snow", "father"] <- "Eddard Stark"
pred[pred$name == "Jon Snow", "child_of"] <- "Rhaegar Targaryen"
pred[pred$name == "Theon Greyjoy", "mother"] <- "Alannys Harlaw"
pred[pred$name == "Theon Greyjoy", "father"] <- "Balon Greyjoy"
pred[pred$name == "Asha Greyjoy", "mother"] <- "Alannys Harlaw"
pred[pred$name == "Asha Greyjoy", "father"] <- "Balon Greyjoy"
pred[pred$name == "Tommen Baratheon", "house"] <- "House Baratheon/Lannister"
pred[pred$name == "Balon Greyjoy", "house"] <- "House Greyjoy"
pred[pred$name == "Balon Greyjoy", "mother"] <- "Lady of House Sunderly"
pred[pred$name == "Victarion Greyjoy", "mother"] <- "Lady of House Sunderly"
pred[pred$name == "Victarion Greyjoy", "father"] <- "Quellon Greyjoy"
pred[pred$name == "Ramsay Snow", "father"] <- "Roose Bolton"
pred[pred$name == "Myrcella Baratheon", "mother"] <- "Cersei Lannister"
pred[pred$name == "Myrcella Baratheon", "father"] <- "Robert Baratheon"
pred[pred$name == "Shireen Baratheon", "mother"] <- "Selyse Florent"
pred[pred$name == "Shireen Baratheon", "father"] <- "Stannis Baratheon"
pred[pred$name == "Renly Baratheon", "mother"] <- "Cassana Baratheon"
pred[pred$name == "Renly Baratheon", "father"] <- "Steffon Baratheon"
pred[pred$name == "Stannis Baratheon", "mother"] <- "Cassana Baratheon"
#adding a row to the Pred dataset
add <- pred[1, ] #Adding a row
add[1, ] <- NA
add$name <- "Robert Baratheon"
add$mother <- "Cassana Baratheon"
add$father <- "Steffon Baratheon"
add$spouse <- "Cersei Lannister"
pred <- rbind(pred, add) #Binding Row wise to the dataset
#----------
#Adding some more values
pred[pred$name == "Catelyn Stark", "mother"] <- "Minisa Whent"
pred[pred$name == "Catelyn Stark", "father"] <- "Hoster Tully"
pred[pred$name == "Edmure Tully", "mother"] <- "Minisa Whent"
pred[pred$name == "Edmure Tully", "father"] <- "Hoster Tully"
pred[pred$name == "Lysa Arryn", "mother"] <- "Minisa Whent"
pred[pred$name == "Lysa Arryn", "father"] <- "Hoster Tully"
pred[pred$name == "Robert Arryn", "mother"] <- "Lysa Arryn"
pred[pred$name == "Robert Arryn", "father"] <- "Jon Arryn"
pred[pred$name == "Lyanna Stark", "father"] <- "Rickard Stark"
pred[pred$name == "Eddard Stark", "father"] <- "Rickard Stark"
pred[pred$name == "Brandon Stark", "father"] <- "Rickard Stark"
pred[pred$name == "Benjen Stark", "father"] <- "Rickard Stark"
pred[pred$name == "Rickard Stark", "mother"] <- "Marna Locke"
pred[pred$name == "Rickard Stark", "father"] <- "Edwyle Stark"
pred[pred$name == "Willam Stark", "mother"] <- "Lorra Royce"
pred[pred$name == "Willam Stark", "father"] <- "Beron Stark"
pred[pred$name == "predrik Stark son of Beron", "mother"] <- "Lorra Royce"
pred[pred$name == "predrik Stark son of Beron", "father"] <- "Beron Stark"
pred[pred$name == "predrik Stark son of Beron", "child_of"] <- NA
add <- pred[1, ]
add[1, ] <- NA
add$name <- "Lyarra Stark"
add$mother <- "Arya Flint"
add$father <- "predrik Stark"
add$spouse <- "Rickard Stark"
pred <- rbind(pred, add)
#------------
#Adding some more Names
pred[pred$name == "Artos Stark", "mother"] <- "Lorra Royce"
pred[pred$name == "Artos Stark", "father"] <- "Beron Stark"
pred[pred$name == "predwell Stark", "mother"] <- "Alys Karstark"
pred[pred$name == "predwell Stark", "father"] <- "Brandon Stark son of Cregan"
pred[pred$name == "Beron Stark", "mother"] <- "Alys Karstark"
pred[pred$name == "Beron Stark", "father"] <- "Brandon Stark son of Cregan"
pred[pred$name == "Brandon Stark son of Cregan", "mother"] <- "Lynara Stark"
pred[pred$name == "Brandon Stark son of Cregan", "father"] <- "Cregan Stark"
pred[pred$name == "Cregan Stark", "mother"] <- "Gilliane Glover"
pred[pred$name == "Cregan Stark", "father"] <- "Rickon Stark son of Benjen"
pred[pred$name == "Cersei Lannister", "mother"] <- "Joanna Lannister"
pred[pred$name == "Cersei Lannister", "father"] <- "Tywin Lannister"
pred[pred$name == "Jaime Lannister", "mother"] <- "Joanna Lannister"
pred[pred$name == "Jaime Lannister", "father"] <- "Tywin Lannister"
pred[pred$name == "Tyrion Lannister", "mother"] <- "Joanna Lannister"
pred[pred$name == "Tyrion Lannister", "father"] <- "Tywin Lannister"
pred[pred$name == "Joffrey Baratheon", "child_of"] <- "Jaime Lannister"
pred[pred$name == "Myrcella Baratheon", "child_of"] <- "Jaime Lannister"
pred[pred$name == "Tommen Baratheon", "child_of"] <- "Jaime Lannister"
pred[pred$name == "Jonnel Stark", "father"] <- "Cregan Stark"
pred[pred$name == "Jonnel Stark", "mother"] <- "Lynara Stark"
pred[pred$name == "Brendan Stark son of Cregan", "father"] <- "Cregan Stark"
pred[pred$name == "Brendan Stark son of Cregan", "child_of"] <- NA
pred[pred$name == "Eddard Stark", "mother"] <- "Lyarra Stark"
pred[pred$name == "Benjen Stark", "mother"] <- "Lyarra Stark"
pred[pred$name == "Brandon Stark", "mother"] <- "Lyarra Stark"
pred[pred$name == "Lyanna Stark", "mother"] <- "Lyarra Stark"
pred[pred$name == "Edwyle Stark", "mother"] <- "Melantha Blackwood"
pred[pred$name == "Edwyle Stark", "father"] <- "Willam Stark"
pred[pred$name == "Cerenna Lannister", "mother"] <- "Myranda Lefford"
pred[pred$name == "Myrielle Lannister", "mother"] <- "Myranda Lefford"
pred[pred$name == "Daven Lannister", "mother"] <- "Myranda Lefford"
pred[pred$name == "Cerenna Lannister", "father"] <- "Stafford Lannister"
pred[pred$name == "Myrielle Lannister", "father"] <- "Stafford Lannister"
pred[pred$name == "Daven Lannister", "father"] <- "Stafford Lannister"
pred[pred$name == "Joanna Lannister", "father"] <- "Jason Lannister"
pred[pred$name == "Joanna Lannister", "mother"] <- "Marla Prester"
pred[pred$name == "Stafford Lannister", "father"] <- "Jason Lannister"
pred[pred$name == "Stafford Lannister", "mother"] <- "Marla Prester"
pred[pred$name == "Tywin Lannister", "father"] <- "Tytos Lannister"
pred[pred$name == "Kevan Lannister", "father"] <- "Tytos Lannister"
pred[pred$name == "Genna Lannister", "father"] <- "Tytos Lannister"
pred[pred$name == "Lancel Lannister", "father"] <- "Kevan Lannister"
pred[pred$name == "Lancel Lannister", "mother"] <- "Dorna Swyft"
pred[pred$name == "Willem Lannister", "father"] <- "Kevan Lannister"
pred[pred$name == "Willem Lannister", "mother"] <- "Dorna Swyft"
pred[pred$name == "Brandon Stark son of Cregan", "child_of"] <- NA
pred[pred$name == "Lanna Lannister", "father"] <- "Damion Lannister"
pred[pred$name == "Lanna Lannister", "mother"] <- "Shiera Crakehall"
pred[pred$name == "Lucion Lannister", "father"] <- "Damion Lannister"
pred[pred$name == "Lucion Lannister", "mother"] <- "Shiera Crakehall"
pred[pred$name == "Damion Lannister", "father"] <- "Damon Lannister son of Jason"
pred[pred$name == "Damion Lannister", "mother"] <- "Ella Lannister"
pred[pred$name == "Damon Lannister son of Jason", "father"] <- "Jason Lannister"
pred[pred$name == "Damon Lannister son of Jason", "child_of"] <- NA
pred[pred$name == "Damon Lannister son of Jason", "mother"] <- "Alys Stackspear"
pred[pred$name == "Jason Lannister", "father"] <- "Gerold Lannister"
pred[pred$name == "Jason Lannister", "mother"] <- "Rohanne Webber"
pred[pred$name == "Damon Lannister lord", "name"] <- "Damon Lannister"
pred[pred$name == "Tybolt Lannister", "father"] <- "Damon Lannister"
pred[pred$name == "Tybolt Lannister", "mother"] <- "Cerissa Brax"
pred[pred$name == "Gerold Lannister", "father"] <- "Damon Lannister"
pred[pred$name == "Gerold Lannister", "mother"] <- "Cerissa Brax"
pred[pred$name == "Tygett Lannister", "father"] <- "Tytos Lannister"
pred[pred$name == "Tygett Lannister", "mother"] <- "Jeyne Marbrand"
pred[pred$name == "Tywin Lannister", "mother"] <- "Jeyne Marbrand"
pred[pred$name == "Kevan Lannister", "mother"] <- "Jeyne Marbrand"
pred[pred$name == "Genna Lannister", "mother"] <- "Jeyne Marbrand"
pred[pred$name == "Gerion Lannister", "father"] <- "Tytos Lannister"
pred[pred$name == "Gerion Lannister", "mother"] <- "Jeyne Marbrand"
pred[pred$name == "Tyrek Lannister", "father"] <- "Tygett Lannister"
pred[pred$name == "Tyrek Lannister", "mother"] <- "Darlessa Marbrand"
pred[pred$name == "Steffon Baratheon", "father"] <- "Ormund Baratheon"
pred[pred$name == "Steffon Baratheon", "mother"] <- "Rhaelle Targaryen"
pred[pred$name == "Daenerys Targaryen", "father"] <- "Aerys II Targaryen"
pred[pred$name == "Daenerys Targaryen", "mother"] <- "Rhaella Targaryen"
pred[pred$name == "Viserys Targaryen", "father"] <- "Aerys II Targaryen"
pred[pred$name == "Viserys Targaryen", "mother"] <- "Rhaella Targaryen"
pred[pred$name == "Rhaegar Targaryen", "father"] <- "Aerys II Targaryen"
pred[pred$name == "Rhaegar Targaryen", "mother"] <- "Rhaella Targaryen"
pred[pred$name == "Rhaella Targaryen", "father"] <- "Jaehaerys II Targaryen"
pred[pred$name == "Rhaella Targaryen", "mother"] <- "Shaera Targaryen"
pred[pred$name == "Rhaenys Targaryen daughter of Rhaegar", "father"] <- "Rhaegar Targaryen"
pred[pred$name == "Rhaenys Targaryen daughter of Rhaegar", "mother"] <- "Elia Martell"
pred[pred$name == "Rhaenys Targaryen daughter of Rhaegar", "child_of"] <- NA
pred[pred$name == "Aegon Targaryen daughter of Rhaegar", "father"] <- "Rhaegar Targaryen"
pred[pred$name == "Aegon Targaryen daughter of Rhaegar", "mother"] <- "Elia Martell"
pred[pred$name == "Aegon Targaryen daughter of Rhaegar", "child_of"] <- NA
pred[pred$name == "Aegon IV Targaryen", "father"] <- "Aerys II Targaryen"
pred[pred$name == "Aegon IV Targaryen", "mother"] <- "Rhaella Targaryen"
add <- pred[pred$name == "Jaehaerys II Targaryen", ]
add[, "name"] <- "Shaera Targaryen"
pred <- rbind(pred, add)
#------------
pred[pred$name == "Duncan Targaryen", "father"] <- "Aegon V Targaryen"
pred[pred$name == "Duncan Targaryen", "mother"] <- "Betha Blackwood"
pred[pred$name == "Rhaelle Targaryen", "father"] <- "Aegon V Targaryen"
pred[pred$name == "Rhaelle Targaryen", "mother"] <- "Betha Blackwood"
pred[pred$name == "Theodore Tyrell", "father"] <- "Luthor Tyrell son of Moryn"
pred[pred$name == "Theodore Tyrell", "mother"] <- "Elyn Norridge"
pred[pred$name == "Mace Tyrell", "father"] <- "Luthor Tyrell"
pred[pred$name == "Mace Tyrell", "mother"] <- "Olenna Redwyne"
pred[pred$name == "Luthor Tyrell", "father"] <- "Unknown father Tyrell"
pred[pred$name == "Luthor Tyrell", "mother"] <- "Unknown mother Tyrell"
pred[pred$name == "Moryn Tyrell", "father"] <- "Unknown father Tyrell"
pred[pred$name == "Moryn Tyrell", "mother"] <- "Unknown mother Tyrell"
pred[pred$name == "Luthor Tyrell son of Moryn", "father"] <- "Moryn Tyrell"
pred[pred$name == "Luthor Tyrell son of Moryn", "child_of"] <- NA
pred[pred$name == "Garlan Tyrell", "father"] <- "Mace Tyrell"
pred[pred$name == "Garlan Tyrell", "mother"] <- "Alerie Hightower"
pred[pred$name == "Willas Tyrell", "father"] <- "Mace Tyrell"
pred[pred$name == "Willas Tyrell", "mother"] <- "Alerie Hightower"
pred[pred$name == "Loras Tyrell", "father"] <- "Mace Tyrell"
pred[pred$name == "Loras Tyrell", "mother"] <- "Alerie Hightower"
pred[pred$name == "Margaery Tyrell", "father"] <- "Mace Tyrell"
pred[pred$name == "Margaery Tyrell", "mother"] <- "Alerie Hightower"
pred[pred$name == "Mina Tyrell", "father"] <- "Luthor Tyrell"
pred[pred$name == "Mina Tyrell", "mother"] <- "Olenna Redwyne"
pred[pred$name == "Hobber Redwyne", "mother"] <- "Mina Tyrell"
pred[pred$name == "Hobber Redwyne", "father"] <- "Paxter Redwyne"
pred[pred$name == "Horas Redwyne", "mother"] <- "Mina Tyrell"
pred[pred$name == "Horas Redwyne", "father"] <- "Paxter Redwyne"
pred[pred$name == "Olene Tyrell", "mother"] <- "Elyn Norridge"
pred[pred$name == "Olene Tyrell", "father"] <- "Luthor Tyrell son of Moryn"
pred[pred$name == "Medwick Tyrell", "mother"] <- "Elyn Norridge"
pred[pred$name == "Medwick Tyrell", "father"] <- "Luthor Tyrell son of Moryn"
pred[pred$name == "Elinor Tyrell", "mother"] <- "Lia Serry"
pred[pred$name == "Elinor Tyrell", "father"] <- "Theodore Tyrell"
pred[pred$name == "Luthor Tyrell son of Theodore", "mother"] <- "Lia Serry"
pred[pred$name == "Luthor Tyrell son of Theodore", "father"] <- "Theodore Tyrell"
pred[pred$name == "Luthor Tyrell son of Theodore", "child_of"] <- NA
pred[pred$name == "Oberyn Martell", "mother"] <- "Princess of Dorne"
pred[pred$name == "Doran Martell", "mother"] <- "Princess of Dorne"
pred[pred$name == "Mors Martell brother of Doran", "mother"] <- "Princess of Dorne"
pred[pred$name == "Elia Martell", "mother"] <- "Princess of Dorne"
pred[pred$name == "Doran Martell", "house"] <- "House Martell"
pred[pred$name == "Obara Sand", "father"] <- "Oberyn Martell"
pred[pred$name == "Nymeria Sand", "father"] <- "Oberyn Martell"
pred[pred$name == "Tyene Sand", "father"] <- "Oberyn Martell"
pred[pred$name == "Sarella Sand", "father"] <- "Oberyn Martell"
pred[pred$name == "Elia Sand", "father"] <- "Oberyn Martell"
pred[pred$name == "Elia Sand", "mother"] <- "Ellaria Sand"
pred[pred$name == "Obella Sand", "father"] <- "Oberyn Martell"
pred[pred$name == "Obella Sand", "mother"] <- "Ellaria Sand"
pred[pred$name == "Dorea Sand", "father"] <- "Oberyn Martell"
pred[pred$name == "Obella Sand", "mother"] <- "Ellaria Sand"
pred[pred$name == "Loreza Sand", "father"] <- "Oberyn Martell"
pred[pred$name == "Loreza Sand", "mother"] <- "Ellaria Sand"
pred[pred$name == "Aegon Targaryen son of Rhaegar", "father"] <- "Rhaegar Targaryen"
pred[pred$name == "Aegon Targaryen son of Rhaegar", "mother"] <- "Elia Martell"
pred[pred$name == "Aegon Targaryen son of Rhaegar", "child_of"] <- NA
pred[pred$name == "Jon Arryn", "father"] <- "Jasper Arryn"
pred[pred$name == "Alys Arryn", "father"] <- "Jasper Arryn"
pred[pred$name == "Stevron Frey", "father"] <- "Walder Frey"
pred[pred$name == "Stevron Frey", "mother"] <- "Perra Royce"
pred[pred$name == "Emmon Frey", "father"] <- "Walder Frey"
pred[pred$name == "Emmon Frey", "mother"] <- "Perra Royce"
pred[pred$name == "Aenys Frey", "father"] <- "Walder Frey"
pred[pred$name == "Aenys Frey", "mother"] <- "Perra Royce"
pred[pred$name == "Ryman Frey", "father"] <- "Stevron Frey"
pred[pred$name == "Ryman Arryn", "mother"] <- "Corenna Swan"
pred[pred$name == "Edwyn Frey", "father"] <- "Ryman Frey"
pred[pred$name == "Petyr Frey", "father"] <- "Ryman Frey"
pred[pred$name == "Walda Frey", "father"] <- "Edwyn Frey"
pred[pred$name == "Walda Frey", "mother"] <- "Janyce Hunter"
pred[pred$name == "Perra Frey", "father"] <- "Petyr Frey"
pred[pred$name == "Perra Frey", "mother"] <- "Mylenda Caron"
pred[pred$name == "Maegelle Frey", "father"] <- "Stevron Frey"
pred[pred$name == "Maegelle Frey", "mother"] <- "Jeyne Lydden"
pred[pred$name == "Marianne Vance", "father"] <- "Dafyn Vance"
pred[pred$name == "Marianne Vance", "mother"] <- "Maegelle Frey"
pred[pred$name == "Walder Vance", "father"] <- "Dafyn Vance"
pred[pred$name == "Walder Vance", "mother"] <- "Maegelle Frey"
pred[pred$name == "Patrek Vance", "father"] <- "Dafyn Vance"
pred[pred$name == "Patrek Vance", "mother"] <- "Maegelle Frey"
pred[pred$name == "Walton Frey", "father"] <- "Stevron Frey"
pred[pred$name == "Walton Frey", "mother"] <- "Marsella Weynwood"
pred[pred$name == "Steffon Frey", "father"] <- "Walton Frey"
pred[pred$name == "Steffon Frey", "mother"] <- "Deana Hardyng"
pred[pred$name == "Bryan Frey", "father"] <- "Walton Frey"
pred[pred$name == "Bryan Frey", "mother"] <- "Deana Hardyng"
pred[pred$name == "Walda Frey daughter of Walton", "father"] <- "Walton Frey"
pred[pred$name == "Walda Frey daughter of Walton", "mother"] <- "Deana Hardyng"
pred[pred$name == "Harlon Greyjoy", "father"] <- "Quellon Greyjoy"
pred[pred$name == "Harlon Greyjoy", "mother"] <- "Lady of House Stonetree"
pred[pred$name == "Quenton Greyjoy", "father"] <- "Quellon Greyjoy"
pred[pred$name == "Quenton Greyjoy", "mother"] <- "Lady of House Stonetree"
pred[pred$name == "Donel Greyjoy", "father"] <- "Quellon Greyjoy"
pred[pred$name == "Donel Greyjoy", "mother"] <- "Lady of House Stonetree"
pred[pred$name == "predrik Greyjoy", "father"] <- "Balon Greyjoy"
pred[pred$name == "predrik Greyjoy", "mother"] <- "Alannys Harlaw"
#-------
pred[pred$name == "Maron Greyjoy", "father"] <- "Balon Greyjoy"
pred[pred$name == "Maron Greyjoy", "mother"] <- "Alannys Harlaw"
pred[pred$name == "Urrigon Greyjoy", "father"] <- "Quellon Greyjoy"
pred[pred$name == "Urrigon Greyjoy", "mother"] <- "Lady of House Sunderly"
pred[pred$name == "Aeron Greyjoy", "father"] <- "Quellon Greyjoy"
pred[pred$name == "Aeron Greyjoy", "mother"] <- "Lady of House Sunderly"
pred[pred$name == "Robin Greyjoy", "father"] <- "Quellon Greyjoy"
pred[pred$name == "Robin Greyjoy", "mother"] <- "Lady of House Piper"
pred[pred$name == "Ryman Frey", "mother"] <- "Corenna Swann"
pred[pred$name == "Walda Frey daughter of Edwyn", "father"] <- "Edwyn Frey"
pred[pred$name == "Walda Frey daughter of Edwyn", "mother"] <- "Janyce Hunter"
pred[pred$name == "Aegon Frey son of Stevron", "father"] <- "Stevron Frey"
pred[pred$name == "Aegon Frey son of Stevron", "mother"] <- "Jeyne Lydden"
pred[pred$name == "Cleos Frey", "father"] <- "Emmon Frey"
pred[pred$name == "Cleos Frey", "mother"] <- "Genna Lannister"
pred[pred$name == "Tywin Frey", "father"] <- "Cleos Frey"
pred[pred$name == "Tywin Frey", "mother"] <- "Jeyne Darry"
pred[pred$name == "Willem Frey", "father"] <- "Cleos Frey"
pred[pred$name == "Willem Frey", "mother"] <- "Jeyne Darry"
pred[pred$name == "Lyonel Frey", "father"] <- "Emmon Frey"
pred[pred$name == "Lyonel Frey", "mother"] <- "Genna Lannister"
pred[pred$name == "Tion Frey", "father"] <- "Emmon Frey"
pred[pred$name == "Tion Frey", "mother"] <- "Genna Lannister"
pred[pred$name == "Walder Frey son of Emmon", "father"] <- "Emmon Frey"
pred[pred$name == "Walder Frey son of Emmon", "mother"] <- "Genna Lannister"
pred[pred$name == "Amerei Frey", "father"] <- "Merrett Frey"
pred[pred$name == "Amerei Frey", "mother"] <- "Mariya Darry"
pred[pred$name == "Walda Frey daughter of Merrett", "father"] <- "Merrett Frey"
pred[pred$name == "Walda Frey daughter of Merrett", "mother"] <- "Mariya Darry"
pred[pred$name == "Walder Frey son of Merrett", "father"] <- "Merrett Frey"
pred[pred$name == "Walder Frey son of Merrett", "mother"] <- "Mariya Darry"
pred[pred$name == "Marissa Frey", "father"] <- "Merrett Frey"
pred[pred$name == "Marissa Frey", "mother"] <- "Mariya Darry"
pred[pred$name == "Merrett Frey", "father"] <- "Walder Frey"
pred[pred$name == "Merrett Frey", "mother"] <- "Amarei Crakehall"
pred[pred$name == "Roslin Frey", "father"] <- "Walder Frey"
pred[pred$name == "Roslin Frey", "mother"] <- "Bethany Rosby"
pred[pred$name == "Olyvar Frey", "father"] <- "Walder Frey"
pred[pred$name == "Olyvar Frey", "mother"] <- "Bethany Rosby"
pred[pred$name == "Willamen Frey", "father"] <- "Walder Frey"
pred[pred$name == "Willamen Frey", "mother"] <- "Bethany Rosby"
pred[pred$name == "Benfrey Frey", "father"] <- "Walder Frey"
pred[pred$name == "Benfrey Frey", "mother"] <- "Bethany Rosby"
pred[pred$name == "Perwyn Frey", "father"] <- "Walder Frey"
pred[pred$name == "Perwyn Frey", "mother"] <- "Bethany Rosby"
pred[, c(6, 12:13, 15, 34)] %>% arrange(name)
pred[grep("Perwyn", pred$name), c(6, 12:13, 15, 34)] %>% arrange(name)
add <- pred[pred$name == "Willam Stark", ]
add[, "spouse"] <- "Lyanna Glover"
pred <- rbind(pred, add)
add <- pred[pred$name == "Sansa Stark", ]
add[, "spouse"] <- "Ramsay Snow"
pred <- rbind(pred, add)
add <- pred[pred$name == "Cregan Stark", ]
add[, "spouse"] <- "Lynara Stark"
pred <- rbind(pred, add)
pred[pred$name == "Steffon Baratheon", "spouse"] <- "Cassana Baratheon"
pred[pred$name == "Tommen Baratheon", "spouse"] <- "Margaery Tyrell"
pred[pred$name == "Joffrey Baratheon", "spouse"] <- "Margaery Tyrell"
pred[pred$name == "Jason Lannister", "spouse"] <- "Alys Stackspear"
add <- pred[pred$name == "Jason Lannister", ]
add[, "spouse"] <- "Marla Prester"
pred <- rbind(pred, add)
add <- pred[pred$name == "Gerold Lannister", ]
add[, "spouse"] <- "Rohanne Webber"
pred <- rbind(pred, add)
pred[pred$name == "Aerys II Targaryen", "spouse"] <- "Rhaella Targaryen"
add <- pred[pred$name == "Daenerys Targaryen", ]
add[, "spouse"] <- "Hizdahr zo Loraq"
pred <- rbind(pred, add)
pred[pred$name == "Hizdahr zo Loraq", "spouse"] <- "Daenerys Targaryen"
add <- pred[pred$name == "Margaery Tyrell", ]
add[, "spouse"] <- "Joffrey Baratheon"
pred <- rbind(pred, add)
add <- pred[pred$name == "Margaery Tyrell", ]
add[, "spouse"] <- "Tommen Baratheon"
pred <- rbind(pred, add)
pred[pred$name == "Elyn Norridge", "spouse"] <- "Luthor Tyrell son of Moryn"
```
Adding More Rows
```{r}
add <- pred[pred$name == "Jon Arryn", ]
add[, "spouse"] <- "Lysa Arryn"
pred <- rbind(pred, add)
add <- pred[pred$name == "Jon Arryn", ]
add[, "spouse"] <- "Rowena Arryn"
pred <- rbind(pred, add)
add <- pred[pred$name == "Stevron Frey", ]
add[, "spouse"] <- "Marsella Waynwood"
pred <- rbind(pred, add)
add <- pred[pred$name == "Stevron Frey", ]
add[, "spouse"] <- "Jeyne Lydden"
pred <- rbind(pred, add)
add <- pred[pred$name == "Lysa Arryn", ]
add[, "spouse"] <- "Petyr Baelish"
pred <- rbind(pred, add)
pred[pred$name == "Quellon Greyjoy", "spouse"] <- "Lady of House Stonetree"
add <- pred[pred$name == "Quellon Greyjoy", ]
add[, "spouse"] <- "Lady of House Sunderly"
pred <- rbind(pred, add)
add <- pred[pred$name == "Quellon Greyjoy", ]
add[, "spouse"] <- "Lady of House Piper"
pred <- rbind(pred, add)
pred[, c(6, 15:16)] %>% arrange(name)
pred[grep("Bethany", pred$name), c(6, 15:16)] %>% arrange(name)
```
####Generating more New Dataframes
```{r}
#contains Mothers
pred_2a <- pred[, c(6, 12)] %>%
na.omit()
#Contains Fathers
pred_2b <- pred[, c(6, 13)] %>%
na.omit()
#Child of
pred_3 <- pred[, c(6, 34)] %>%
na.omit()
#Spouse Dataset
pred_4 <- pred[, c(6, 16)] %>%
na.omit()
```
####Generating the first Network Dataset
```{r}
net1 <- data.frame(source = c(pred_2a$mother, pred_2b$father, pred_3$child_of, pred_4$name),
target = c(pred_2a$name, pred_2b$name, pred_3$name, pred_4$spouse),
type = c(rep("mother", nrow(pred_2a)), rep("father", nrow(pred_2b)), rep("father/mother", nrow(pred_3)), rep("spouse", nrow(pred_4))))
head(net1)
```
-----------------
##Scraping GOT Data
```{r,message=FALSE,warning=FALSE}
require(rvest)
#Scraping all the main characters fro wikipedia
main.page <- read_html("https://en.wikipedia.org/wiki/List_of_Game_of_Thrones_characters")
names <- main.page %>% # feed `main.page` to the next step
html_nodes(xpath='//*[@id="mw-content-text"]/div/table[2]') %>%
.[[1]] %>%
html_table(fill=T)
#removing irrelevant columns
for(i in 1:7)
{
names$Appearances<-NULL
}
#renaming the columns
names(names)<-c("actor","name")
names$actor<-NULL
names<-names[names$name=="Character",]#removing a irrelevant row
#final data frame with major character names
df_final_major <- data.frame(names)
```
###Scraping the list of Minor and Supporting characters
```{r}
main.page <- read_html("https://en.wikipedia.org/wiki/List_of_Game_of_Thrones_characters")
#List of Minor actors
names <- main.page %>%
html_nodes(xpath='//*[@id="mw-content-text"]/div/table[3]') %>%
.[[1]] %>%
html_table(fill=T)
for(i in 1:7)
{
names$Appearances<-NULL
}
#renaming the columns
names(names)<-c("actor","name")
names$actor<-NULL
names<-names[!names$name=="Character",]#removing a irrelevant row
df_final_minor <-data.frame(names)
```
###Final Data frame
```{r}
df_final <- rbind(df_final_major,
df_final_minor)
names(df_final)<-c("name")
```
This data frame consists of all the characters in GOT i.e all supporting and Major characters.
Saving the data frame.
```{r}
save(df_final, file = "df_final.RData")
load("df_final.RData")
```
```{r}
net <- rbind(net1)
net$source <- as.character(net$source)
net$target <- as.character(net$target)
net$type <- as.character(net$type)
net[net == "Cassana Estermont"] <- "Cassana Baratheon"
net[net == "Catelyn Tully"] <- "Catelyn Stark"
net[net == "Ramsay Snow"] <- "Ramsay Bolton"
net <- net[!duplicated(net), ]
net <- rbind(net, c("Talisa Stark", "Robb Stark", "spouse"))
net[net$target == "Aegon IV Targaryen" & net$type == "mother", "source"] <- "Viserys II Targaryen"
net[net$target == "Aegon IV Targaryen" & net$type == "father", "source"] <- "Larra Rogare"
net[net$target == "Maron Martell", "target"] <- "Maron Martell I"
#----------
pred$name <- as.character(pred$name)
characters <- data.frame(name = unique(c(as.character(net$source), as.character(net$target)))) %>%
mutate(name = as.character(name)) %>%
left_join(pred[, c(6, 8, 9, 15, 32:34)], by = "name")
characters[characters == ""] <- NA
#assigning 0 as NA value
characters$popularity[is.na(characters$popularity)] <- 0
#removing duplicate characters
characters <- characters[!duplicated(characters), ]
```
```{r}
characters[characters$name == "Tya Lannister", "male"] <- 0
characters[grep("Aegon", characters$name), "male"] <- 1
characters[grep("Aerys", characters$name), "male"] <- 1
characters[grep("Baelon", characters$name), "male"] <- 1
for (i in 1:nrow(characters)) {
name <- characters[i, "name"]
value <- ifelse(name %in% net$source & any(net[net$source == name, "type"] == "mother"), 0,
ifelse(name %in% net$source & any(net[net$source == name, "type"] == "father"), 1, characters[i, "male"]))
characters[i, "male"] <- value
}
#making a Boolean column with male as 1 otherwise 0
characters[characters$name == "Ramsay Bolton", "male"] <- 1
characters[characters$name == "Arya Stark", "male"] <- 0
characters[characters$name == "Sansa Stark", "male"] <- 0
characters[characters$name == "Robb Stark", "male"] <- 1
characters[characters$name == "Rickon Stark", "male"] <- 1
characters[characters$name == "Bran Stark", "male"] <- 1
characters[characters$name == "Jon Snow", "male"] <- 1
for (i in 1:nrow(characters)) {
value <- characters[i, "male"]
if (is.na(value)) {
name <- characters[i, "name"]
type <- net[which(net$source == name | net$target == name), "type"]
if (any(type == "spouse")) {
net2 <- net[which(net$source == name | net$target == name), ] %>%
filter(type == "spouse")
if (nrow(net2) > 1) {
sp <- c(net2[, "source"], net2[, "target"])
sp <- sp[which(sp != name)]
for (j in length(sp)) {
spouse <- sp[j]
value[j] <- ifelse(characters[characters$name == spouse, "male"] == 1, 0,
ifelse(characters[characters$name == spouse, "male"] == 0, 1, NA))
}
value <- na.omit(value)[1]
} else {
sp1 <- net2[, "source"]
sp2 <- net2[, "target"]
spouse <- ifelse(sp1 == name, sp2, sp1)
value <- ifelse(characters[characters$name == spouse, "male"] == 1, 0,
ifelse(characters[characters$name == spouse, "male"] == 0, 1, NA))
}
characters[i, "male"] <- value
}
}
}
#Adding houses columns to the characters dataframe
#------------
for (i in 1:nrow(characters)) {
if (!is.na(characters[i, "male"])) {
if (characters[i, "male"] == 1 & is.na(characters[i, "house"])) {
name <- gsub(" son of .*", "", characters[i, "name"])
characters[i, "house"] <- paste("House", gsub("(.* )(.*$)", "\\2", name))
}
if (characters[i, "male"] == 0) {
name <- characters[i, "name"]
father <- net[which(net$target == name & net$type == "father"), ]
if (nrow(father) > 0) {
characters[i, "house"] <- paste("House", gsub("(.* )(.*$)", "\\2", father$source))
} else {
characters[i, "house"] <- paste("House", gsub("(.* )(.*$)", "\\2", characters[i, "name"]))
}
}
} else {
characters[i, "house"] <- paste("House", gsub("(.* )(.*$)", "\\2", characters[i, "name"]))
}
}
characters[grep("Baratheon", characters$house), "house"] <- "House Baratheon"
characters[grep("Bolton", characters$house), "house"] <- "House Bolton"
characters[grep("Fossoway", characters$house), "house"] <- "House Fossoway"
characters[grep("Frey", characters$house), "house"] <- "House Frey"
characters[grep("Lannister", characters$house), "house"] <- "House Lannister"
characters[grep("Tyrell", characters$house), "house"] <- "House Tyrell"
characters[grep("Stark", characters$house), "house"] <- "House Stark"
characters[grep("Targaryen", characters$house), "house"] <- "House Targaryen"
characters[characters$name == "Rickon Stark son of Benjen", "house"] <- "House Stark"
characters[characters$name == "Drogo", "house"] <- "Dothraki"
characters$house2 <- ifelse(characters$house %in% c("House Targaryen", "House Stark", "House Tully", "House Lannister", "House Baratheon", "House Tyrell", "House Martell", "House Frey", "House Greyjoy"), characters$house, NA)
```
```{r}
net[net$source == "Rhaegar Targaryen" & net$target == "Aegon Targaryen son of Rhaegar", "type"] <- "father"
characters[characters$name == "Aegon II", "name"] <- "Aegon II Targaryen"
characters[characters$name == "Aegon II Targaryen", "house"] <- "House Targaryen"
characters[characters$name == "Aegon Targaryen son of Jaehaerys I", "house"] <- "House Targaryen"
characters[characters$name == "Marsella Weynwood", "name"] <- "Marsella Waynwood"
#adding color to the Network edges
library(RColorBrewer)
#adding colors to the types i.e father,mother,spouse i.e relationships
colors_edges <- with(net,
data.frame(type = levels(as.factor(type)),
color = I(brewer.pal(nlevels(as.factor(type)), name = 'Dark2'))))
#Joining net and color_edges df by type
net <- left_join(net, colors_edges, by = "type")
#------------
#coloring the nodes on the basis of house
color_vertices <- with(characters,
data.frame(house2 = levels(as.factor(house2)),
color = I(brewer.pal(nlevels(as.factor(house2)), name = 'Set1'))))
#joining the colored vertices with characters dataframe
characters <- left_join(characters, color_vertices, by = "house2")
#Shapes of the Gender
characters$shape <- ifelse(characters$male == 1, "square",
ifelse(characters$male == 0, "circle", "triangle"))
characters$shape[is.na(characters$shape)] <- "none"
```
#####Now we are done with Data preparation to be used to plot Graphs .