In this document we will practice various types of network analyses using an edgelist of dolphins

Exercise 1 - Basic graph summary metrics

# Download the dolphin dataset from the URL provided
url <- "https://ona-book.org/data/dolphins.csv"

dolphin_edgelist <- read.csv(url)

# look at the data to see what you're dealing with
# create an undirected graph
library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
dolphin_graph <- igraph::graph_from_data_frame(
  dolphin_edgelist,
  directed = FALSE
)
# View the graph object - how many vertices and edges are there?
dolphin_graph
## IGRAPH b67d85b UN-- 62 159 -- 
## + attr: name (v/c)
## + edges from b67d85b (vertex names):
##  [1] CCL       --Double   DN16      --Feather  DN21      --Feather 
##  [4] Beak      --Fish     Bumper    --Fish     DN16      --Gallatin
##  [7] DN21      --Gallatin Feather   --Gallatin Beak      --Grin    
## [10] CCL       --Grin     Beak      --Haecksel Grin      --Hook    
## [13] Beescratch--Jet      DN21      --Jet      Feather   --Jet     
## [16] Gallatin  --Jet      Haecksel  --Jonah    Beescratch--Knit    
## [19] DN63      --Knit     Double    --Kringel  Hook      --Kringel 
## [22] Jonah     --Kringel  Jonah     --MN105    Jet       --MN23    
## + ... omitted several edges
# what is the distance between Zig and Fork
distances(dolphin_graph, weights = NULL)["Zig","Fork"]
## [1] 7
# Is this a dense network?
edge_density(dolphin_graph)
## [1] 0.0840825
# How many components are in this network?
components(dolphin_graph)
## $membership
##         CCL        DN16        DN21        Beak      Bumper     Feather 
##           1           1           1           1           1           1 
##        Grin  Beescratch    Gallatin    Haecksel        DN63      Double 
##           1           1           1           1           1           1 
##        Hook       Jonah         Jet         Mus       Notch     Kringel 
##           1           1           1           1           1           1 
##        Fish       MN105        MN83        Knit       Oscar        Fork 
##           1           1           1           1           1           1 
##       Scabs   Patchback        MN60    Shmuddel       SN100         SN4 
##           1           1           1           1           1           1 
##          PL        SN63     Stripes        SN96       TR120     Topless 
##           1           1           1           1           1           1 
##       Cross        Five        TR99         SN9        SN90     Trigger 
##           1           1           1           1           1           1 
##        SN89        TR82      Upbang Ripplefluke       TSN83        MN23 
##           1           1           1           1           1           1 
##     Number1       Quasi        SMN5     Thumper        TR77        TR88 
##           1           1           1           1           1           1 
##      TSN103         Vau        Wave         Web    Whitetip         Zap 
##           1           1           1           1           1           1 
##         Zig      Zipfel 
##           1           1 
## 
## $csize
## [1] 62
## 
## $no
## [1] 1
# What does this mean for our network?
# we have a complete graph where all dolphins are somehow connected
# What is the network diameter? What does this mean?
diameter(dolphin_graph)
## [1] 8
# how large is the largest clique?
clique_num(dolphin_graph)
## [1] 5
# how many cliques are between size 3 and 5?
length(cliques(dolphin_graph, min = 3, max = 5))
## [1] 125

Exercise 2 - Centrality measures

# Continuing with our same dolphin graph, let's calculate centrality metrics.
# HINT: try using the tidygraph package
cent <-
  dolphin_graph |>
  tidygraph::as_tbl_graph() |>
  dplyr::mutate(
    DEGREE_CENT = tidygraph::centrality_degree(),
    BTWN_CENT = tidygraph::centrality_betweenness(),
    CLOSE_CENT = tidygraph::centrality_closeness()
  )
## Warning in betweenness(graph = graph, v = V(graph), directed = directed, :
## 'nobigint' is deprecated since igraph 1.3 and will be removed in igraph 1.4
#  Which dolphin is an important connector? (SN100)
cent %>%
  dplyr::arrange(
    desc(BTWN_CENT)
  )
## # A tbl_graph: 62 nodes and 159 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 62 x 4 (active)
##   name       DEGREE_CENT BTWN_CENT CLOSE_CENT
##   <chr>            <dbl>     <dbl>      <dbl>
## 1 SN100                7      454.    0.00685
## 2 Beescratch           8      390.    0.00610
## 3 SN9                  8      262.    0.00662
## 4 SN4                 11      254.    0.00654
## 5 DN63                 5      216.    0.00599
## 6 Jet                  9      209.    0.00508
## # … with 56 more rows
## #
## # Edge Data: 159 x 2
##    from    to
##   <int> <int>
## 1    31    48
## 2    32    44
## 3    26    32
## # … with 156 more rows
# Which dolphin has the most connections?  (Grin)
cent %>%
  dplyr::arrange(
    desc(DEGREE_CENT)
  )
## # A tbl_graph: 62 nodes and 159 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 62 x 4 (active)
##   name    DEGREE_CENT BTWN_CENT CLOSE_CENT
##   <chr>         <dbl>     <dbl>      <dbl>
## 1 Grin             12     113.     0.00617
## 2 SN4              11     254.     0.00654
## 3 Topless          11      74.4    0.00568
## 4 Scabs            10     105.     0.00599
## 5 Trigger          10     155.     0.00541
## 6 Jet               9     209.     0.00508
## # … with 56 more rows
## #
## # Edge Data: 159 x 2
##    from    to
##   <int> <int>
## 1    23    42
## 2    14    37
## 3    14    21
## # … with 156 more rows
# Which dolphin is the most central? (SN100)
cent %>%
  dplyr::arrange(
    desc(CLOSE_CENT)
  )
## # A tbl_graph: 62 nodes and 159 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 62 x 4 (active)
##   name       DEGREE_CENT BTWN_CENT CLOSE_CENT
##   <chr>            <dbl>     <dbl>      <dbl>
## 1 SN100                7      454.    0.00685
## 2 SN9                  8      262.    0.00662
## 3 SN4                 11      254.    0.00654
## 4 Kringel              9      188.    0.00641
## 5 Grin                12      113.    0.00617
## 6 Beescratch           8      390.    0.00610
## # … with 56 more rows
## #
## # Edge Data: 159 x 2
##    from    to
##   <int> <int>
## 1    10    37
## 2    50    56
## 3    46    50
## # … with 156 more rows
library(ggraph)
## Loading required package: ggplot2
# plot the dolphin graph and locate Grin and SN100 to see if these results make intuitive sense
V(dolphin_graph)$highcent <- ifelse(V(dolphin_graph)$name %in% c("SN100","Grin"),1,0)

ggraph::ggraph(dolphin_graph) +
  geom_edge_link(color = "grey") +
  geom_node_point(aes(color = as.factor(highcent), size = 3), show.legend = FALSE) +
  scale_color_manual(values = c("lightblue","pink")) +
  geom_node_text(aes(label = name)) +
  theme_void()
## Using `stress` as default layout

Exercise 3: Community detection

# What communities do you see in the dolphin graph? Try out a couple of methods and compare results
# e.g. cluster_louvain, cluster_leiden, cluster_fast_greedy
set.seed(123)
communities <- cluster_louvain(dolphin_graph)
V(dolphin_graph)$community <- membership(communities)

sizes(communities)
## Community sizes
##  1  2  3  4  5 
##  5 18  9 18 12
# plot your favorite option
set.seed(123)
ggraph(dolphin_graph, layout = "fr") +
  geom_edge_link(color =  "grey") +
  geom_node_point(aes(color = as.factor(community)),
                  show.legend = FALSE) +
  theme_void()

Exercise 4 - Do people tend to email with people in their same department?

# Download the email dataset from the URL provided
url <- "https://ona-book.org/data/email_edgelist.csv"

email_edgelist <- read.csv(url)

# Download the corresponding department mappings from the URL provided
url_v <- "https://ona-book.org/data/email_vertices.csv"

email_dept <- read.csv(url_v)

# look through the edges and vertices to understand the data source
# create the graph with vertice labels
email_graph <- igraph::graph_from_data_frame(
  email_edgelist,
  directed = FALSE,
  vertices = email_dept
)

# Calculate assortativity
assortativity_nominal(email_graph, factor(V(email_graph)$dept))
## [1] 0.3139728

Exercise 5 - Exploring the friends graph (EXTENSION)

# Download the friends dataset from the URL provided
url <- "https://ona-book.org/data/friends_tv_edgelist.csv"

friends_edgelist <- read.csv(url)
# which two characters interact most?
friends_edgelist |>
  dplyr::arrange(desc(weight)) |> 
  head()
##       from     to weight
## 1 Chandler Monica    938
## 2 Chandler   Joey    889
## 3   Rachel   Ross    836
## 4   Monica Phoebe    800
## 5   Monica Rachel    774
## 6   Phoebe Rachel    771
# is this a connected network?
friends_graph <-
  igraph::graph_from_data_frame(
    friends_edgelist,
    directed = FALSE
  )

components(friends_graph)
## $membership
##       a Casino Boss       a Crew Member a Disembodied Voice   a Drunken Gambler 
##                   1                   1                   1                   1 
##    a Female Student     a Male Customer           a Student           a Tourist 
##                   1                   1                   1                   1 
##            a Waiter    a Waiter in Drag             a Woman               Actor 
##                   1                   1                   1                   1 
## Adoption Agency Guy            Adrienne          Agency Guy      Air Stewardess 
##                   1                   1                   1                   1 
##    Airline Employee                Alan                Alex    Alexandra Steele 
##                   1                   1                   1                   1 
##               Alice              Alison              Amanda               Amber 
##                   1                   1                   1                   1 
##                 Amy      Amy Storms Out         Anchorwoman              Andrea 
##                   1                   1                   1                   1 
##              Angela           Annabelle           Announcer       Another Extra 
##                   1                   1                   1                   1 
## Another Man's Voice   Another Scientist  Another Tour Guide   Answering Machine 
##                   1                   1                   1                   1 
##              Arthur              Ashley           Assistant           Aunt Iris 
##                   1                   1                   1                   1 
##        Aunt Lillian           Aunt Lisa         Aunt Millie              Aurora 
##                   1                   1                   1                   1 
##          Bandleader        Bank Officer               Barry         Bass Singer 
##                   1                   1                   1                   1 
##                 Ben            Benjamin             Bernice            Best Man 
##                   1                   1                   1                   1 
##           Big Bully    Big Nosed Rachel                Bill       Billy Crystal 
##                   1                   1                   1                   1 
##         Bitter Lady        Bitter Woman    Blackjack Dealer         Blonde Girl 
##                   1                   1                   1                   1 
##                 Bob              Bonnie                 Boy     Boy in the Cape 
##                   1                   1                   1                   1 
##                Boys                Burt         C.h.e.e.s.e        C.h.e.e.s.e. 
##                   1                   1                   1                   1 
##              Cailin             Caitlin                Carl               Carol 
##                   1                   1                   1                   1 
##            Caroline               Casey             Cashier              Cassie 
##                   1                   1                   1                   1 
##   Casting Assistant Casting Director #1 Casting Director #2 Casting Director #3 
##                   1                   1                   1                   1 
##         Casting Guy             Cecilia               Celia            Chandler 
##                   1                   1                   1                   1 
##           Chandlers         Charity Guy             Charlie     Charlton Heston 
##                   1                   1                   1                   1 
##      Chase Lassiter              Cheryl                Chip               Chloe 
##                   1                   1                   1                   1 
##             Claudia               Clerk               Cliff               Clown 
##                   1                   1                   1                   1 
##             Colleen            Coma Guy              Cookie                 Cop 
##                   1                   1                   1                   1 
##             Cowgirl            Croupler            Customer           Customers 
##                   1                   1                   1                   1 
##              Cut to             Cynthia                 Dan            Danielle 
##                   1                   1                   1                   1 
##               Danny      Danny's Sister                Dave               David 
##                   1                   1                   1                   1 
##       Delivery Girl        Delivery Guy     Dennis Phillips               Devon 
##                   1                   1                   1                   1 
##          Dick Clark                Dina            Director                Dirk 
##                   1                   1                   1                   1 
##              Doctor     Doctor Connelly                 Don               Donny 
##                   1                   1                   1                   1 
##        Donny Osmond                Doug         Dr Baldhara            Dr Biely 
##                   1                   1                   1                   1 
##            Dr Burke    Dr Drake Ramoray    Dr Drake Remoray        Dr Franzblau 
##                   1                   1                   1                   1 
##        Dr Gettleman            Dr Green            Dr Harad           Dr Horton 
##                   1                   1                   1                   1 
##          Dr Johnson        Dr Ledbetter       Dr Leedbetter               Dr Li 
##                   1                   1                   1                   1 
##             Dr Long           Dr Miller         Dr Mitchell          Dr Oberman 
##                   1                   1                   1                   1 
##           Dr Remore           Dr Rhodes            Dr Roger            Dr Rosen 
##                   1                   1                   1                   1 
##           Dr Schiff  Dr Stryker Remoray    Dr Timothy Burke           Dr Wesley 
##                   1                   1                   1                   1 
##             Dr Zane                Drew           Drunk Man     Drunken Gambler 
##                   1                   1                   1                   1 
##              Duncan                Earl               Eddie               Eldad 
##                   1                   1                   1                   1 
##           Elizabeth              Emeril      Emil Alexander               Emily 
##                   1                   1                   1                   1 
##                Emma            Employee                Eric               Erica 
##                   1                   1                   1                   1 
##                Erin               Ernie             Estelle                Estl 
##                   1                   1                   1                   1 
##          Evil Bitch               Extra         Fake Monica            Fat Girl 
##                   1                   1                   1                   1 
##          Fat Monica                Fbob            Felicity        Female Clerk 
##                   1                   1                   1                   1 
##      Female Jeweler      Female Student              Fergie             Fireman 
##                   1                   1                   1                   1 
##          Fireman #1          Fireman #2       Fireman No. 1       Fireman No. 2 
##                   1                   1                   1                   1 
##       Fireman No. 3      First Dorm Guy                Fran               Frank 
##                   1                   1                   1                   1 
##           Frank Jr.           Frank Sr.             Frannie            Fredrick 
##                   1                   1                   1                   1 
##    Front Desk Clerk               Funny                Gail                Gang 
##                   1                   1                   1                   1 
##                Gary        Gary Collins        Gary's Radio          Gate Agent 
##                   1                   1                   1                   1 
##               Gavin                Gene             Gerston                Gert 
##                   1                   1                   1                   1 
##              Ginger                Girl        Girl's Voice               Girls 
##                   1                   1                   1                   1 
##   Grandma Tribbiani            Grandmom            Guest #1            Guest #2 
##                   1                   1                   1                   1 
##            Guest #3             Gunther            Guru Saj                 Guy 
##                   1                   1                   1                   1 
##              Guy #1              Guy #2                Guys        Gym Employee 
##                   1                   1                   1                   1 
##            Handyman              Hayley   Hayley's Roommate    Health Inspector 
##                   1                   1                   1                   1 
##             Heather               Helen              Helena           Henrietta 
##                   1                   1                   1                   1 
##               Hilda             Hillary          Hitchhiker          Hold Voice 
##                   1                   1                   1                   1 
##          Hombre Man       Homo Ergaster              Hooker                Hope 
##                   1                   1                   1                   1 
##               Hoshi                Host         Hotel Clerk         Housekeeper 
##                   1                   1                   1                   1 
##  Hums While He Pees       Hypnosis Tape        i Understand            Intercom 
##                   1                   1                   1                   1 
##         Interviewer            Isabella               Issac              It's 6 
##                   1                   1                   1                   1 
##              It's 9                Jack                Jade                Jake 
##                   1                   1                   1                   1 
##               Jamie                Jane              Janice      Janice's Voice 
##                   1                   1                   1                   1 
##              Janine             Janitor             Jasmine               Jason 
##                   1                   1                   1                   1 
##            Jay Leno            Jeanette             Jeannie            Jeannine 
##                   1                   1                   1                   1 
##                 Jen            Jennifer      Jessica Ashley    Jessica Lockhart 
##                   1                   1                   1                   1 
##              Jester                Jill                 Jim             Jo Lynn 
##                   1                   1                   1                   1 
##              Joanna              Joanne                Joey         Joey Laughs 
##                   1                   1                   1                   1 
##           Joey Nods          Joey on Tv      Joey's Co-Star         Joey's Date 
##                   1                   1                   1                   1 
##       Joey's Doctor     Joey's Grandmom    Joey's Hand Twin         Joey's Head 
##                   1                   1                   1                   1 
##       Joey's Sister      Joey's Sisters                Josh              Joshua 
##                   1                   1                   1                   1 
##               Judge                Judy               Julie               Julio 
##                   1                   1                   1                   1 
##                Kara               Karin                Kash                Kate 
##                   1                   1                   1                   1 
##               Kathy     Kathy's Co-Star               Katie                 Ken 
##                   1                   1                   1                   1 
##               Kevin                 Kid                Kids                Kiki 
##                   1                   1                   1                   1 
##                 Kim      Kitchen Worker                Kori              Krista 
##                   1                   1                   1                   1 
##             Kristen             Kristin                Kyle         Kyle Lowder 
##                   1                   1                   1                   1 
##                Lady               Larry               Laura              Lauren 
##                   1                   1                   1                   1 
##              Leader            Lecturer              Leslie               Lewis 
##                   1                   1                   1                   1 
##                Liam              Lipson                Lisa        Little Bully 
##                   1                   1                   1                   1 
##         Little Girl              Lizzie           Locksmith            Lorraine 
##                   1                   1                   1                   1 
##               Luisa               Lydia                 Mac             Machine 
##                   1                   1                   1                   1 
##           Maitre D'              Malcom          Male Guest        Male Jeweler 
##                   1                   1                   1                   1 
##        Male Student                 Man  Man at the Wedding           Man on Tv 
##                   1                   1                   1                   1 
##  Man with a Bow Tie         Man's Voice               Manny                Marc 
##                   1                   1                   1                   1 
##               Marge              Margha            Marjorie                Mark 
##                   1                   1                   1                   1 
##              Marsha          Mary Ellen         Mary-Angela            Matire'd 
##                   1                   1                   1                   1 
##        Matress King     Matthew Ashford                 Max                 Meg 
##                   1                   1                   1                   1 
##               Megan                 Mel             Melanie             Melissa 
##                   1                   1                   1                   1 
##             Message                Mich            Michelle                Mike 
##                   1                   1                   1                   1 
##          Mike's Dad          Mike's Mom               Mindy            Minister 
##                   1                   1                   1                   1 
##             Minster              Mischa               Missy               Molly 
##                   1                   1                   1                   1 
##                Mona         Mona's Date              Monica               Morse 
##                   1                   1                   1                   1 
##               Mover                Mr a             Mr Bing          Mr Bowmont 
##                   1                   1                   1                   1 
##            Mr Boyle           Mr Buffay           Mr Burgin         Mr Campbell 
##                   1                   1                   1                   1 
##          Mr Douglas         Mr Franklin           Mr Geller            Mr Green 
##                   1                   1                   1                   1 
##           Mr Greene          Mr Heckles           Mr Kaplan         Mr Oberblau 
##                   1                   1                   1                   1 
##           Mr Posner            Mr Simon         Mr Thompson          Mr Treeger 
##                   1                   1                   1                   1 
##        Mr Tribbiani          Mr Waltham         Mr Wineburg          Mr Zellner 
##                   1                   1                   1                   1 
##           Mr Zelner            Mrs Bing          Mrs Burgin         Mrs Burkart 
##                   1                   1                   1                   1 
##       Mrs Chatracus          Mrs Geller           Mrs Green          Mrs Greene 
##                   1                   1                   1                   1 
##           Mrs Lynch          Mrs Potter         Mrs Tedlock         Mrs Waltham 
##                   1                   1                   1                   1 
##        Mrs Wineburg           Ms Geller          Ms Lambert               Nancy 
##                   1                   1                   1                   1 
##            Narrator                Nina               Nurse            Nurse #1 
##                   1                   1                   1                   1 
##            Nurse #2           Old Woman     Older Scientist              Others 
##                   1                   1                   1                   1 
##                Oven                  Pa      Paleontologist               Paolo 
##                   1                   1                   1                   1 
##              Parker        Party Guests           Passenger        Passenger #1 
##                   1                   1                   1                   1 
##        Passenger #2        Passenger #3             Patrick                Paul 
##                   1                   1                   1                   1 
##               Paulo       Pbs Volunteer                Pete          Pete's Mom 
##                   1                   1                   1                   1 
##               Peter              Petrie                Phil              Phoebe 
##                   1                   1                   1                   1 
##           Phoebe Sr        Photographer           Pizza Guy           Policeman 
##                   1                   1                   1                   1 
##        Priest on Tv       Prof. Sherman    Professor Feesen   Professor Sherman 
##                   1                   1                   1                   1 
##  Professor Spafford    Professore Clerk             Quartet               Racel 
##                   1                   1                   1                   1 
##               Rache              Rachel               Radio              Rahcel 
##                   1                   1                   1                   1 
##             Raymond        Receptionist             Referee             Richard 
##                   1                   1                   1                   1 
##                Rita                 Rob              Robbie              Robert 
##                   1                   1                   1                   1 
##      Robin Williams               Roger                Ross          Salon Girl 
##                   1                   1                   1                   1 
##    Same Man's Voice               Sarah     Second Dorm Guy      Second Message 
##                   1                   1                   1                   1 
##      Security Guard   Sherman Whitfield Sleep Clinic Worker Soothing Male Voice 
##                   1                   1                   1                   1 
##             Stanley           Stephanie               Steve          Supervisor 
##                   1                   1                   1                   1 
##               Susan               Susie             Teacher               Terry 
##                   1                   1                   1                   1 
##         The Colonel       The Conductor The Cooking Teacher        The Director 
##                   1                   1                   1                   1 
##          The Doctor           The Girls            The Guys  The Head Librarian 
##                   1                   1                   1                   1 
##        The Hot Girl     The Interviewer     The Little Girl          The Lurker 
##                   1                   1                   1                   1 
##     The Other Woman        The Producer    The Second Guest        Ticket Agent 
##                   1                   1                   1                   1 
##             Trainer        Tv Announcer              Ursula              Waiter 
##                   1                   1                   1                   1 
##            Waitress         Woman No. 1         The Fireman        The Croupier 
##                   1                   1                   1                   1 
##               Woman          The Waiter      Waiter in Drag       Woman at Door 
##                   1                   1                   1                   1 
##           Store Guy       The Presenter          Tour Guide         Second Girl 
##                   1                   1                   1                   1 
##           Uncle Dan           The Woman         The Teacher                Owen 
##                   1                   1                   1                   1 
##               Wayne               Tommy          The Writer                Dana 
##                   1                   1                   1                   1 
##             Frankie              Lowell        Mary-Theresa          Ms Mckenna 
##                   1                   1                   1                   1 
##          Phoebe Sr.             Realtor      Richard's Date               Ronni 
##                   1                   1                   1                   1 
##                 Roy                Russ               Sandy              Santos 
##                   1                   1                   1                   1 
##               Scott           Sebastian             Shelley              Singer 
##                   1                   1                   1                   1 
##            Sister 1              Sophie             Stevens            Stripper 
##                   1                   1                   1                   1 
##                 Tag     The Chorus Line   The Cigarette Guy         The Knocker 
##                   1                   1                   1                   1 
##             The Man The Museum Official       The Paramedic    The Photographer 
##                   1                   1                   1                   1 
##           The Rabbi        The Salesman        The Stripper         The Vampire 
##                   1                   1                   1                   1 
##        The Waitress               Tilly                 Tim                 Tom 
##                   1                   1                   1                   1 
##                Tony                  Tv           Tv Doctor           Van Damme 
##                   1                   1                   1                   1 
##               Vince               Voice              Walker           Whitfield 
##                   1                   1                   1                   1 
##                will               Witch         Woman No. 2         Woman on Tv 
##                   1                   1                   1                   1 
##       Woman's Voice                Zack      Shop Assistant              Patron 
##                   1                   1                   1                   1 
##  Woman Giving Birth       Stage Manager   The "Hey Guy" Guy                 Zoe 
##                   1                   1                   1                   1 
##           Secretary                 Ray        Sick Bastard      Stage Director 
##                   1                   1                   1                   1 
##           Mackenzie             The Fan   The Porsche Owner                 Sid 
##                   1                   1                   1                   1 
##       Mrs Tribbiani      Phoebe-Estelle           Smart Kid               Sonia 
##                   1                   1                   1                   1 
##             Student                Tape             The A.d  The Acting Teacher 
##                   1                   1                   1                   1 
##      The Dr Cleaner     The Food Critic            The Grip         The Husband 
##                   1                   1                   1                   1 
##         The Old Man          The Pastor  The Security Guard     The Singing Man 
##                   1                   1                   1                   1 
##    The Woman Dealer             Waiters           We Have 8         Young Ethan 
##                   1                   1                   1                   1 
##        Ralph Lauren             Whitney     The Bass Barber         Strange Man 
##                   1                   1                   1                   1 
##            Precious              Sergei               Paula                Rtst 
##                   1                   1                   1                   1 
##         Spokeswoman                 Stu        The Cute Guy       The Professor 
##                   1                   1                   1                   1 
##       The Saleslady   The Wedding Guest               Video                Rick 
##                   1                   1                   1                   1 
##     The Housekeeper  Phoebe's Assistant            Producer                Ryan 
##                   1                   1                   1                   1 
##      Smoke Detector            Stranger       Tattoo Artist   The Smoking Woman 
##                   1                   1                   1                   1 
##       Trudie Styler             Russell     Wedding Planner            Salesman 
##                   1                   1                   1                   1 
##      The Instructor       The Librarian      The Saleswoman           Waiter #2 
##                   1                   1                   1                   1 
##      Woman on Train        Waiter No. 2 
##                   1                   1 
## 
## $csize
## [1] 650
## 
## $no
## [1] 1
# who is the most "important" character?
c_vals <-
  friends_graph |>
  tidygraph::as_tbl_graph() |>
  dplyr::mutate(
    DEGREE_CENT = tidygraph::centrality_degree(),
    BTWN_CENT = tidygraph::centrality_betweenness(),
    CLOSE_CENT = tidygraph::centrality_closeness()
  )
## Warning in betweenness(graph = graph, v = V(graph), directed = directed, :
## 'nobigint' is deprecated since igraph 1.3 and will be removed in igraph 1.4
# Ross and Joey come out on top of all metrics
c_vals |>
  dplyr::arrange(desc(DEGREE_CENT))
## # A tbl_graph: 650 nodes and 2976 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 650 x 4 (active)
##   name     DEGREE_CENT BTWN_CENT CLOSE_CENT
##   <chr>          <dbl>     <dbl>      <dbl>
## 1 Ross             359    46721.    0.00106
## 2 Joey             353    46871.    0.00106
## 3 Chandler         342    35337.    0.00105
## 4 Monica           339    33493.    0.00104
## 5 Phoebe           334    39774.    0.00104
## 6 Rachel           331    32353.    0.00103
## # … with 644 more rows
## #
## # Edge Data: 2,976 x 3
##    from    to weight
##   <int> <int>  <int>
## 1   243   244      1
## 2     3   243      1
## 3     2   243      1
## # … with 2,973 more rows
c_vals |>
  dplyr::arrange(desc(BTWN_CENT))
## # A tbl_graph: 650 nodes and 2976 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 650 x 4 (active)
##   name     DEGREE_CENT BTWN_CENT CLOSE_CENT
##   <chr>          <dbl>     <dbl>      <dbl>
## 1 Joey             353    46871.    0.00106
## 2 Ross             359    46721.    0.00106
## 3 Phoebe           334    39774.    0.00104
## 4 Chandler         342    35337.    0.00105
## 5 Monica           339    33493.    0.00104
## 6 Rachel           331    32353.    0.00103
## # … with 644 more rows
## #
## # Edge Data: 2,976 x 3
##    from    to weight
##   <int> <int>  <int>
## 1   164   171      1
## 2     4   164      1
## 3     1   164      1
## # … with 2,973 more rows
c_vals |>
  dplyr::arrange(desc(CLOSE_CENT))
## # A tbl_graph: 650 nodes and 2976 edges
## #
## # An undirected simple graph with 1 component
## #
## # Node Data: 650 x 4 (active)
##   name     DEGREE_CENT BTWN_CENT CLOSE_CENT
##   <chr>          <dbl>     <dbl>      <dbl>
## 1 Ross             359    46721.    0.00106
## 2 Joey             353    46871.    0.00106
## 3 Chandler         342    35337.    0.00105
## 4 Monica           339    33493.    0.00104
## 5 Phoebe           334    39774.    0.00104
## 6 Rachel           331    32353.    0.00103
## # … with 644 more rows
## #
## # Edge Data: 2,976 x 3
##    from    to weight
##   <int> <int>  <int>
## 1   219   220      1
## 2     3   219      1
## 3     2   219      1
## # … with 2,973 more rows
# Are the friends part of the same community?  Who else is in the community?  
# Does it make intuitive sense (if you're a friends fan :-) )

set.seed(123)
communities <- cluster_louvain(friends_graph, weights = NULL)
V(friends_graph)$community <- membership(communities)

membership(communities)[c("Joey","Monica","Chandler","Ross","Phoebe","Rachel")]
##     Joey   Monica Chandler     Ross   Phoebe   Rachel 
##        5        5        5        5        5        5
# is anyone else in cluster 5?  so many random people....
length(membership(communities)[membership(communities) == 5])
## [1] 246
# try leiden
set.seed(123)
communities <- cluster_leiden(friends_graph, weights = NULL)
V(friends_graph)$community <- membership(communities)

membership(communities)[c("Joey","Monica","Chandler","Ross","Phoebe","Rachel")]
##     Joey   Monica Chandler     Ross   Phoebe   Rachel 
##       49       49       49       49       49       49
# this list of people makes a lot more sense to a Friends fan
membership(communities)[membership(communities) == 49]
##             Ben           Carol        Chandler         Charlie       Elizabeth 
##              49              49              49              49              49 
##           Emily           Erica           Frank            Gary         Gunther 
##              49              49              49              49              49 
##             Guy          Janice          Janine            Jill            Joey 
##              49              49              49              49              49 
##           Julie           Kathy             Man            Mike            Mona 
##              49              49              49              49              49 
##          Monica       Mr Geller        Mrs Bing      Mrs Geller           Nurse 
##              49              49              49              49              49 
##            Paul            Pete          Phoebe          Rachel         Richard 
##              49              49              49              49              49 
##            Ross           Susan        The Guys The Interviewer          Waiter 
##              49              49              49              49              49 
##           Woman             Tag 
##              49              49