In this document we will practice creating and examining graph objects in R, and visualizing those graphs using common visualization packages.

Exercise 1 - Creating an undirected graph object

# Download the karate dataset from the URL provided
LI_edgelist <- read.csv("Connections real.csv")
LI <- LI_edgelist |>na.omit(LI)
LI_edges <- LI |>
            dplyr::select(my.name, Name)
LI_graph <- igraph::graph_from_data_frame(
  LI_edges,
  directed = FALSE
)

LI_graph
## IGRAPH 5c3b507 UN-- 50 49 -- 
## + attr: name (v/c)
## + edges from 5c3b507 (vertex names):
##  [1] Anne Simonsen--Lynsey Mella      Anne Simonsen--Nate Akers       
##  [3] Anne Simonsen--Parker Robbins    Anne Simonsen--Tansy Hale       
##  [5] Anne Simonsen--Katie George      Anne Simonsen--Cameron Archibald
##  [7] Anne Simonsen--Ryan Parco        Anne Simonsen--Bruce Hansen     
##  [9] Anne Simonsen--Mark Ball         Anne Simonsen--Nicole Earnshaw  
## [11] Anne Simonsen--Lance Bunch       Anne Simonsen--J.J. Litster     
## [13] Anne Simonsen--Gina Maret        Anne Simonsen--Alexis Norden    
## [15] Anne Simonsen--Becca Bettinger   Anne Simonsen--Sarah Bowen      
## + ... omitted several edges
library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
V(LI_graph)
## + 50/50 vertices, named, from 5c3b507:
##  [1] Anne Simonsen           Lynsey Mella            Nate Akers             
##  [4] Parker Robbins          Tansy Hale              Katie George           
##  [7] Cameron Archibald       Ryan Parco              Bruce Hansen           
## [10] Mark Ball               Nicole Earnshaw         Lance Bunch            
## [13] J.J. Litster            Gina Maret              Alexis Norden          
## [16] Becca Bettinger         Sarah Bowen             Seth Doyle             
## [19] Jonathan Staheli        Annie Gold              Ella Wright            
## [22] Mackinzie Hamilton      Annabelle Peterson      Evan White             
## [25] Adam Nissen             Emily Clinger           Jaxson Myers           
## [28] Grant Baldwin           Jacob White             Michaela Udlock        
## + ... omitted several vertices
E(LI_graph)
## + 49/49 edges from 5c3b507 (vertex names):
##  [1] Anne Simonsen--Lynsey Mella       Anne Simonsen--Nate Akers        
##  [3] Anne Simonsen--Parker Robbins     Anne Simonsen--Tansy Hale        
##  [5] Anne Simonsen--Katie George       Anne Simonsen--Cameron Archibald 
##  [7] Anne Simonsen--Ryan Parco         Anne Simonsen--Bruce Hansen      
##  [9] Anne Simonsen--Mark Ball          Anne Simonsen--Nicole Earnshaw   
## [11] Anne Simonsen--Lance Bunch        Anne Simonsen--J.J. Litster      
## [13] Anne Simonsen--Gina Maret         Anne Simonsen--Alexis Norden     
## [15] Anne Simonsen--Becca Bettinger    Anne Simonsen--Sarah Bowen       
## [17] Anne Simonsen--Seth Doyle         Anne Simonsen--Jonathan Staheli  
## [19] Anne Simonsen--Annie Gold         Anne Simonsen--Ella Wright       
## + ... omitted several edges
plot(LI_graph)

V(LI_graph)$label <- ifelse(V(LI_graph)$name %in% c('Anne Simonsen'),
                                V(LI_graph)$name,
                                "")

plot(LI_graph)

set.seed(123)
LI_graph$layout <- igraph::layout_with_kk(LI_graph)
plot(LI_graph)

V(LI_graph)$size <- 5
plot(LI_graph)

library(ggraph)
## Loading required package: ggplot2

set.seed(123)
ggraph(LI_graph, layout = "kk") +
  geom_edge_link(color = "grey") +
  geom_node_point(size = 3) +
  theme_void()

V(LI_graph)$group <- ifelse(V(LI_graph)$name %in% c("Anne Simonsen"), 1, 0)

library(networkD3)
LI_d3 <- networkD3::igraph_to_networkD3(LI_graph, 
                                            group = V(LI_graph)$group)

networkD3::forceNetwork(Links = LI_d3$links, 
                        Nodes = LI_d3$nodes, 
                        NodeID = "name",   
                        Source = "source",
                        Target = "target", 
                        Group = "group",
                        opacity = 1)
E(LI_graph)$date <- LI$Connected.On
E(LI_graph)$weight <- LI$weight
V(LI_graph)$company <- LI$Company
## Warning in vattrs[[name]][index] <- value: number of items to replace is not a
## multiple of replacement length