My friends and my friends’ friends on Twitter. Can you guess which cluster is associated with the National Science Foundation (@NSF)?

# Social Network Analysis

# How to plot a network subgraph on a network graph using R

Here is an example of how to highlight the members of a subgraph on a plot of a network graph.

## Load R libraries library(igraph) # Set adjacency matrix g <- matrix(c(0,1,1,1, 1,0,1,0, 1,1,0,1, 0,0,1,0),nrow=4,ncol=4,byrow=TRUE) # Set adjacency matrix to graph object g <- graph.adjacency(g,mode="directed") # Add node attribute label and name values V(g)$name <- c("n1","n2","n3","n4") # Set subgraph members c <- c("n1","n2","n3") # Add edge attribute id values E(g)$id <- seq(ecount(g)) # Extract supgraph ccsg <- induced.subgraph(graph=g,vids=c) # Extract edge attribute id values of subgraph ccsgId <- E(ccsg)$id # Set graph and subgraph edge and node colors and sizes E(g)$color="grey" E(g)$width=2 E(g)$arrow.size=1 E(g)$arrow.width=1 E(g)[ccsgId]$color <- "#DC143C" # Crimson E(g)[ccsgId]$width <- 2 V(g)$size <- 4 V(g)$color="#00FFFF" # Cyan V(g)$label.color="#00FFFF" # Cyan V(g)$label.cex <-1.5 V(g)[c]$label.color <- "#DC143C" # Crimson V(g)[c]$color <- "#DC143C" # Crimson # Set seed value set.seed(40041) # Set layout options l <- layout.fruchterman.reingold(g) # Plot graph and subgraph plot.igraph(x=g,layout=l)

Simple, no?

# Import *.csv Adjacency Matrix with Row and Column Names into R

Here is some quick and dirty code for entering an adjacency matrix into R. These data were originally manipulated in Excel 2010 and saved as a comma delimitated *.csv file. The original file had sorted actor names down the first column and the same names along the first row. No value was present in the upper lefthand cell of the original data matrix.

# Load network data # Expected format: adjacency matrix with corresponding row and column names # Expected file type: *.csv year1<-read.csv("networkData.csv",header=FALSE,stringsAsFactors=FALSE,sep=",") rNames<-year1[-1,1] # Get row names cNames<-as.vector(as.character(year1[1,-1])) # Get column names year1<-apply(as.matrix(year1[-1,-1]),2,as.numeric) # Get network matrix year1[is.na(year1)]<-0 # Set missing ties to 0 row.names(year1)<-rNames # Give row names colnames(year1)<-cNames # Give column names

# CONvergence of iterated CORrelations (CONCOR) R function

KNOKE BUREAUCRACIES network data used here were obtained through UCINET software.

For an excellent method tutorial on blockmodeling with UCINET see the file Blockmodels.doc written by David Knoke and hosted on his SOC 8412 homepage.

# Stack matricies and matrix transposes KNOKI <- read.table("KNOKI.txt",header=FALSE,sep="") KNOKM <- read.table("KNOKM.txt",header=FALSE,sep="") KNOKIT <- t(KNOKI) KNOKMT <- t(KNOKM) KNOK <- rbind(KNOKI,KNOKIT,KNOKM,KNOKMT) # CONCOR function # CONvergence of iterated CORrelations # Creates a square matrix of correlations of the column pairs of a matrix CONCOR <- function(mat){ colN <- ncol(mat) X <- matrix(rep(0,times=colN*colN),nrow=colN,ncol=colN) for(i in 1:colN){ for(j in i:colN){ X[i,j] <- cor(mat[,i],mat[,j],method=c("pearson")) } } X <- X+(t(X)-diag(diag((X)))) return(X) } KNOKSIM <- CONCOR(KNOK)