library(igraph)

WHAT ABOUT TWO MODE EDGELISTS?

There is a slightly different approach to bringing in Two mode network data from an Edgelist than from an Adjacency matrix. Instead of a two mode matrix, you may have edgelist data in two mode format. First, bring it in and make it a network object.

hp_tm_edgelist <- read.csv(file.choose())
head(hp_tm_edgelist)
##          character      group
## 1 Albus Dumbledore    Phoenix
## 2 Albus Dumbledore    Prefect
## 3 Albus Dumbledore Gryffindor
## 4      Remus Lupin    Phoenix
## 5      Remus Lupin    Prefect
## 6      Remus Lupin Gryffindor

Different from the adjacency matrix, this edge list has one type of node in one column and the other type in the second column.

This is the same process as any other network. Directed is set to FALSE

hp_tm_net <- graph_from_data_frame(hp_tm_edgelist, directed = FALSE)

Let’s check to see if this is actually a two mode network using bipartite_mapping. This function goes through the edgelist an ensures that the columns have distinct nodes in them (i.e. it is truly a bipartite or two mod network).

bipartite_mapping(hp_tm_net)
## $res
## [1] TRUE
## 
## $type
##       Albus Dumbledore            Remus Lupin          Molly Weasley 
##                  FALSE                  FALSE                  FALSE 
##           Siruis Black          Severus Snape          Alastor Moody 
##                  FALSE                  FALSE                  FALSE 
##     Minerva McGonagall          Rubeus Hagrid   Kingsley Shacklebolt 
##                  FALSE                  FALSE                  FALSE 
##       Nymphadora Tonks     Mundungus Fletcher         Dedalus Diggle 
##                  FALSE                  FALSE                  FALSE 
##           Elphias Dode   Aberforth Dumbledore          Arabella Figg 
##                  FALSE                  FALSE                  FALSE 
##         Emmeline Vance        Sturgis Podmore           Hestia Jones 
##                  FALSE                  FALSE                  FALSE 
##        Aurthur Weasley           Bill Weasley        Charlie Weasley 
##                  FALSE                  FALSE                  FALSE 
##       Hermione Granger           Harry Potter              Cho Chang 
##                  FALSE                  FALSE                  FALSE 
##            Ron Weasley         Lavendar Brown         George Weasley 
##                  FALSE                  FALSE                  FALSE 
##           Fred Weasley     Neville Longbottom          Colin Creevey 
##                  FALSE                  FALSE                  FALSE 
##          Luna Lovegood            Dean Thomas             Katie Bell 
##                  FALSE                  FALSE                  FALSE 
##       Angelina Johnson          Hannah Abbott             Lee Jordon 
##                  FALSE                  FALSE                  FALSE 
##      Anthony Goldstein         Ernie Macmilan Justin Finch-Fletchley 
##                  FALSE                  FALSE                  FALSE 
##            Padma Patil        Seamus Finnigan            Susan Bones 
##                  FALSE                  FALSE                  FALSE 
##     Marietta Edgecombe         Alicia Spinnet         Dennis Creevey 
##                  FALSE                  FALSE                  FALSE 
##          Ginny Weasley          Parvati Patil          Nigel Wolpert 
##                  FALSE                  FALSE                  FALSE 
##        Cormac McLaggen           Romilda Vane         Michael Corner 
##                  FALSE                  FALSE                  FALSE 
##             Terry Boot         Maisy Reynolds                 Leanne 
##                  FALSE                  FALSE                  FALSE 
##        Zacharias Smith            Luca Caruso           Alice Toplin 
##                  FALSE                  FALSE                  FALSE 
##           James Potter           Lilly Potter         Peter Petigrew 
##                  FALSE                  FALSE                  FALSE 
##         Fabian Prewett         Gideon Prewett       Frank Longbottom 
##                  FALSE                  FALSE                  FALSE 
##       Alice Longbottom            Edgar Bones          Benjy Fenwick 
##                  FALSE                  FALSE                  FALSE 
##       Caradoc Dearborn        Dorcas Meadowes       Marlene McKinnon 
##                  FALSE                  FALSE                  FALSE 
##         Fleur Delacour    Bellatrix Lestrange          Lucius Malfoy 
##                  FALSE                  FALSE                  FALSE 
##         Igor Karkaroff          Regulus Black        Barty Crouch Jr 
##                  FALSE                  FALSE                  FALSE 
##        Antonin Dolohov         Thorfinn Rowle      Augustus Rookwood 
##                  FALSE                  FALSE                  FALSE 
##            Evan Rosier         Walden Macnair          Alecto Carrow 
##                  FALSE                  FALSE                  FALSE 
##          Amycus Carrow              Avery Jnr          Corban Yaxley 
##                  FALSE                  FALSE                  FALSE 
##             Crabbe Snr           Draco Malfoy                 Gibbon 
##                  FALSE                  FALSE                  FALSE 
##              Goyle Snr                 Jugson           Mulciber Snr 
##                  FALSE                  FALSE                  FALSE 
##           Mulciber Jnr               Nott Snr     Rabastan Lestrange 
##                  FALSE                  FALSE                  FALSE 
##    Rodolphus Lestrange                 Tavers        Pansy Parkinson 
##                  FALSE                  FALSE                  FALSE 
##    Millicent Bulstrode         Vincent Crabbe          Gregory Goyle 
##                  FALSE                  FALSE                  FALSE 
##        Graham Montague     Cassius Warrington            Argus Filch 
##                  FALSE                  FALSE                  FALSE 
##       Dolores Umbridge             Jane Court         Gabriel Truman 
##                  FALSE                  FALSE                  FALSE 
##         Cedric Diggory    Constance Pickering        Natalie Kathryn 
##                  FALSE                  FALSE                  FALSE 
##             Tom Riddle           Felix Rosier           Gemma Farley 
##                  FALSE                  FALSE                  FALSE 
##       Penelope Padgett           Rodrick Lyme          Annalena Murk 
##                  FALSE                  FALSE                  FALSE 
##          Angelica Cole          Percy Weasley       Freddie Clemmons 
##                  FALSE                  FALSE                  FALSE 
##            Dani Caroll          Marcus Turner    Penelope Clearwater 
##                  FALSE                  FALSE                  FALSE 
##         Robert Hillard         Chester Davies                Phoenix 
##                  FALSE                  FALSE                   TRUE 
##                Prefect             Gryffindor           Death.Eaters 
##                   TRUE                   TRUE                   TRUE 
##              Slytherin             Hufflepuff              Ravenclaw 
##                   TRUE                   TRUE                   TRUE 
##      Dumbeldore.s.Army    Inquisitorial.Squad 
##                   TRUE                   TRUE

It recognises that there are two types of node in this object, so we can set that as a vertex characteristic.

V(hp_tm_net)$type <- bipartite_mapping(hp_tm_net)$type

Now we have changed it into a two mode network and added the characteristic “type” that we are familiar with from wokring on the adjacecny matrix version.

V(hp_tm_net)$type 
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [121] FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

You see the true and false statements, as we expect to see. Since this is the case, we will need to use the same +1 alteration to the arguments in our visualisation.

When we plot it, it looks how we expect!

shapes <- c("circle", "square")
colors <-c("green", "orange")
plot(hp_tm_net, vertex.color=colors[V(hp_tm_net)$type+1],
     vertex.shape=shapes[V(hp_tm_net)$type+1], vertex.label = NA)