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

#Set up options and libraries
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(igraph)
library(networkD3)
library(tidyverse)
library(visNetwork)
library(png)

Part 1 - Creating an Undirected Graph Object

setwd("C:/Users/bwegr/Desktop/MBA/Courses/STRAT 490R - Advanced Strategy Analytics/People Analytics/")
li <- read.csv("Connections.csv")
li <- li %>% mutate("Name" = paste(First.Name,Last.Name),
                    "Me" = "Brandon Wegrowski")
li$Connected.On <- as.Date(li$Connected.On, "%d-%b-%y")
li <- li[,c(9,8,4:7)] %>% filter(!is.na(Weight))

li_edges <- li %>% select(Me, Name)
li_edges2 <- li_edges
colnames(li_edges2) <- c("from", "to")

li_graph <- graph_from_data_frame(li_edges2, directed = F)
li_graph
## IGRAPH 79f29db UN-- 51 50 -- 
## + attr: name (v/c)
## + edges from 79f29db (vertex names):
##  [1] Brandon Wegrowski--Gabriel San Martin Brandon Wegrowski--Justin Crandall   
##  [3] Brandon Wegrowski--Tanner Poulton     Brandon Wegrowski--Joshua Spackman   
##  [5] Brandon Wegrowski--Shruti Choubey     Brandon Wegrowski--Jenna Eagleson    
##  [7] Brandon Wegrowski--Adam Wright        Brandon Wegrowski--Brookston Jeppson 
##  [9] Brandon Wegrowski--Sandro Morales     Brandon Wegrowski--Matt Drohan       
## [11] Brandon Wegrowski--Sean Crow          Brandon Wegrowski--Fernanda Sayavedra
## [13] Brandon Wegrowski--Jared Dyer         Brandon Wegrowski--Coltin Romney     
## [15] Brandon Wegrowski--Paul Hardcastle    Brandon Wegrowski--Pushpa Veni Manti 
## + ... omitted several edges
com <- c("Amazon", li$Company)

E(li_graph)$date <- li$Connected.On
E(li_graph)$weight <- li$Weight
V(li_graph)$company <- com

plot(li_graph)

Re-do the layout using the Kamada-Kawaii layout

Set a seed of 123 to control randomness

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

Remove all labels except for ‘Brandon Wegrowski’ and re-plot

# Remove all labels except 'Brandon Wegrowski'
V(li_graph)$label <- ifelse(V(li_graph)$name == 'Brandon Wegrowski', V(li_graph)$name,"")
plot(li_graph)

Part 2 - Interactive Network Visualization Using networkD3 (EXTENSION)

Convert the li_graph for the purposes of using networkD3

Use your new group in the conversion

V(li_graph)$group <- ifelse(V(li_graph)$name == "Brandon Wegrowski", 1, 0)
li_d3 <- igraph_to_networkD3(li_graph, group = V(li_graph)$group)
# Run a force network visualization of the karate graph in networkD3 
networkD3::forceNetwork(Links = li_d3$links, 
                        Nodes = li_d3$nodes, 
                        NodeID = "name",   
                        Source = "source",
                        Target = "target", 
                        Group = "group",
                        opacity = 1)

Part 3 - Bonus Visualizations!

bmw <- "https://raw.githubusercontent.com/bwegr/bwegr.github.io/3ea357e0ee8a7e249f1fc3a2a7dfbaa809daea55/images/bmw.jpg"
per <- "https://raw.githubusercontent.com/bwegr/bwegr.github.io/3ea357e0ee8a7e249f1fc3a2a7dfbaa809daea55/images/Person1.png"
#img1 <- readPNG("Person1.png")
#img2 <- readPNG("Person2.png")

nodes <- as.data.frame(li_d3$nodes)
nodes <- cbind(data.frame(id = 0:50), nodes, shape = "image", 
               data.frame(c(bmw,rep(per,50))))
colnames(nodes) <- c("id","label","group","shape","img")
links <- data.frame(li_d3$links)
colnames(links) <- c("from","to")

ipath <- "https://raw.githubusercontent.com/bwegr/bwegr.github.io/3ea357e0ee8a7e249f1fc3a2a7dfbaa809daea55/images/"
imgcol = c(paste0(ipath,"bmw.jpg"), rep(paste0(ipath,"Person1.png"),50))

visNetwork(nodes, links, width="100%", height="400px",
           main="Interactive LinkedIn Network Graph!", submain="The Most Recent 50 Connections with Brandon Wegrowski!", footer= "Source: www.linkedin.com/in/bwegrowski") %>% 
  visNodes(shapeProperties = list(useBorderWithImage = F)) %>%
  visLayout(randomSeed = 2)
ipath <- "https://raw.githubusercontent.com/bwegr/bwegr.github.io/3ea357e0ee8a7e249f1fc3a2a7dfbaa809daea55/images/"
imgcol = c(paste0(ipath,"bmw.jpg"), rep(paste0(ipath,"Person1.png"),50))
 
nodes <- data.frame(id = 1:4, 
                    shape = "image",
                    image = c(paste0(ipath,"bmw.jpg"), rep(paste0(ipath,"Person1.png"),3)),
                    label = "I'm an image")
  
edges <- data.frame(from = c(2,4,3,3), to = c(1,2,4,2))

visNetwork(nodes, edges, width = "100%") %>% 
  visNodes(shapeProperties = list(useBorderWithImage = F)) %>%
  visLayout(randomSeed = 2)
#images/Person1.png
#https://github.com/bwegr/bwegr.github.io/blob/3ea357e0ee8a7e249f1fc3a2a7dfbaa809daea55/images/Person1.png