#install.packages("GGally")
#install.packages("network")
#install.packages("sna")
#install.packages("intergraph")
library(intergraph)
## Warning: package 'intergraph' was built under R version 4.2.3
library(GGally)
## Warning: package 'GGally' was built under R version 4.2.3
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(network)
## Warning: package 'network' was built under R version 4.2.3
## 
## 'network' 1.18.1 (2023-01-24), part of the Statnet Project
## * 'news(package="network")' for changes since last version
## * 'citation("network")' for citation information
## * 'https://statnet.org' for help, support, and other information
library(sna)
## Warning: package 'sna' was built under R version 4.2.3
## Loading required package: statnet.common
## Warning: package 'statnet.common' was built under R version 4.2.3
## 
## Attaching package: 'statnet.common'
## The following objects are masked from 'package:base':
## 
##     attr, order
## sna: Tools for Social Network Analysis
## Version 2.7-1 created on 2023-01-24.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##  For citation information, type citation("sna").
##  Type help(package="sna") to get started.
library(readr)
library(ggplot2)
library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:sna':
## 
##     betweenness, bonpow, closeness, components, degree, dyad.census,
##     evcent, hierarchy, is.connected, neighborhood, triad.census
## The following objects are masked from 'package:network':
## 
##     %c%, %s%, add.edges, add.vertices, delete.edges, delete.vertices,
##     get.edge.attribute, get.edges, get.vertex.attribute, is.bipartite,
##     is.directed, list.edge.attributes, list.vertex.attributes,
##     set.edge.attribute, set.vertex.attribute
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union

Data Setup

Go ahead and setup the data for this assignment. In order to do social network analysis for a friend group, we will randomly create variables by randomly choosing two letter from the array of alphabets “A”, “B”, “C”, “D”, “E”. Then we will create another dataset that collects the unique variable from the dataset and distinguish their gender randomly.

# Create sample data
data <- data.frame(matrix(ncol = 2, nrow = 200))
names(data) <- c("first", "second")
for(x in 1:200) {
  a <- c("A", "B", "C", "D", "E")
  bc <- paste(sample(a,1, replace = T), sample(a,1, replace = T), sep = "")
  de <- paste(sample(a,1, replace = T), sample(a,1, replace = T), sep = "")
  ifelse(bc == de, data[x,1] <- paste(sample(a,1, replace = T), sample(a,1, replace = T), sep = ""), data[x,1] <- bc)
  data[x,2] <- paste(de, sep = "")
}

friend <- data.frame(unique(data$first))
for(x in 1:nrow(friend)) {
  g <- c("Female", "Male")
  friend[x,2] <- sample(g,1, replace = T)
}

names(friend) <- c("name", "gender")

Creating Network

Now we use graph_from_data_frame() function to create our network. We can plot the network by using the plot function. Afterwards, we will see the vertices of a graph by using the V() function then the E() function to see the edges. Then we will alter the names and degrees of the vertices of the network.

# Create Network
net <- graph_from_data_frame(d = data, vertices = friend, directed = T)
plot(net, vertex.size = 2, edge.arrow.size = 0.1, vertex.label.cex = 0.8)

V(net)
## + 25/25 vertices, named, from 80a40e2:
##  [1] BC AE BD AC EE BB EB AA AD DE BA CE CA CD DA CC EC DB EA BE CB DD AB DC ED
E(net)
## + 200/200 edges from 80a40e2 (vertex names):
##   [1] BC->AC AE->CC BD->EC AC->DC EE->ED BB->AC EB->CE AC->EE AA->BB AD->CA
##  [11] BC->CB DE->BA BA->CC CE->DE DE->CC CA->AB CD->EE DA->EB CC->AA AC->EB
##  [21] EC->AD CD->BB DB->AE BC->CA CA->BD EA->AD BA->CE CE->DE AA->AD BD->CC
##  [31] CE->EE BD->CE EA->BE BE->AA DB->AC EE->DD AC->CA AA->AB DE->CD AD->AA
##  [41] EE->BD CC->BA CB->BD AA->DD EB->BA EA->AD CE->EB DA->EC CB->AB DD->EE
##  [51] CC->BB DD->AE AB->BE CB->ED DB->CD DE->ED DE->BB BB->AC AC->CB DE->EB
##  [61] EB->DA AA->EC BD->BE DC->DD CE->CA EB->BB AC->BD DA->BE EB->EE CB->BD
##  [71] CA->DA AC->AD AA->EB CE->AD AE->BC EB->DD EB->AB CB->BB BE->EA CE->BD
##  [81] AE->CC BB->CD EC->EE AD->CE BE->BD DB->EE CD->AD EA->DD AD->EC AD->CA
##  [91] EC->BA DD->AC BA->DD CE->EC DB->DC AA->AE BC->CC CD->AC CA->AD CD->CE
## + ... omitted several edges
V(net)$label <- V(net)$name
V(net)$degree <- degree(net)

Histogram of node degree

Create a histogram using the degree of the vertices.

hist(V(net)$degree, 
     col = "tomato2", 
     main = "Histogram of Node Degree",
     ylab = "Frequency",
     xlab = "Degree of Vertices")

Color and Plot

Change the colors of the nodes regarding the gender. Do the following.

# Color 
V(net)$color <- ifelse(V(net)$gender == "Male", "lightblue", "pink")

# Network diagram
set.seed(222)
plot(net, 
     vertex.size = V(net)$degree,
     edge.arrow.size = 0.1,
     vertex.label.cex = 0.8)
legend("topright", c("Male","Female"), pch=21,
       col="#777777", pt.bg=c("lightblue","pink"), pt.cex=2, cex=.8)
title(main = "Friend Group")

Hub and authorities

Plot the hub (outgoing interactions) and authorities (ingoing interactions) side by side to see which person has the most in or out going connections.

hs <- hub_score(net)$vector
as <- authority.score(net)$vector
par(mfrow = c(1,2))
set.seed(123)
plot(net, vertex.size = hs*30,
     main = "Hubs", vertex.color = rainbow(52), edge.arrow.size = 0.1, layout = layout.kamada.kawai)
set.seed(123)
plot(net, vertex.size = as*30,
     main = "Authorities", vertex.color = rainbow(52), edge.arrow.size = 0.1, layout = layout.kamada.kawai)

Compile and Finish.

Note that the echo = FALSE parameter can be added to the code chunk to prevent printing of the R code that generated the plot.