Skip to content

Instantly share code, notes, and snippets.

@zackbatist
Last active January 20, 2018 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackbatist/cdae6bb5f4f140b1232a to your computer and use it in GitHub Desktop.
Save zackbatist/cdae6bb5f4f140b1232a to your computer and use it in GitHub Desktop.
This is the R code used for my M.A. thesis, which is also included as Appendix B in the document itself.
### INITIATE IGRAPH ###
library(igraph)
### LOAD NETWORK DATA ###
dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=FALSE)
m=as.matrix(dat)
g=graph.adjacency(m,mode="undirected",weighted=TRUE,diag=FALSE)
g
### ADD ATTRIBUTES ###
a=read.csv("attribs.csv") ##### MODIFY THIS FILE PATH AND ATTRIBUTE NAMES IF NECESSARY
V(g)$name=as.character(a$Site[match(V(g)$name,a$SafeName)])
V(g)$site_id=as.character(a$Site.ID[match(V(g)$name,a$Site)])
V(g)$obsi_analyzed=as.character(a$Obsidian.Analyzed[match(V(g)$name,a$Site)])
V(g)$name=as.character(a$Site[match(V(g)$name,a$SafeName)])
V(g)$latitude=as.character(a$Latitude[match(V(g)$name,a$Site)])
V(g)$longitude=as.character(a$Longitude[match(V(g)$name,a$Site)])
V(g)$country=as.character(a$Country[match(V(g)$name,a$Site)])
V(g)$region=as.character(a$Region[match(V(g)$name,a$Site)])
V(g)$period=as.character(a$Period[match(V(g)$name,a$Site)])
### ANALYSIS ###
# Betweenness Centrality
bw=betweenness(g, v=V(g), directed = FALSE, nobigint = TRUE, normalized = TRUE, weights = NULL)
bw
V(g)$betweeness=bw
# Closeness Centrality
clo=closeness(g, vids=V(g), mode = c("all"), weights = NULL, normalized = TRUE)
clo
V(g)$closeness=clo
# Local Clustering Coefficient
cc=transitivity(g, type=c("barrat"), vids = NULL, weights = NULL, isolates = c("zero"))
cc
V(g)$local_cc=cc
# Global Clustering Coefficient
gcc=transitivity(g, type=c("globalundirected"), vids = NULL, weights = NULL, isolates = c("zero"))
gcc
g$global_cc=gcc
# Average Local Clustering Coefficient
acc=transitivity(g, type=c("localaverageundirected"), vids = NULL, isolates = c("zero"))
acc
g$average_local_cc=acc
# Degree
deg=degree(g, v=V(g), mode = c("all"), loops = NULL, normalized = FALSE)
deg
V(g)$degree=deg
# Weighted Degree
wdeg=graph.strength (g, vids = V(g), mode = c("all"), loops = NULL, weights = NULL)
wdeg
V(g)$weighted_degree=wdeg
# Average Degree
adeg=mean(V(g)$degree)
adeg
g$average_degree=adeg
# Average Path Length
apl=average.path.length(g, directed=FALSE, unconnected=TRUE)
apl
g$average_path_length=apl
# Edge Betweenness Communities
ebc=edge.betweenness.community (g, weights = E(g)$weight, directed = FALSE, edge.betweenness = TRUE, merges = TRUE, bridges = TRUE, modularity = TRUE, membership = TRUE)
ebc
V(g)$GN=ebc$membership
g$ebc_modularity=ebc$modularity
# Edge Betweenness
eb=edge.betweenness(g, e=E(g), directed = FALSE, weights = NULL)
eb
E(g)$edge_betweeness=eb
# Density
den=graph.density(g, loops=FALSE)
den
g$density=den
# Diameter
diam=diameter(g, directed = FALSE, unconnected = TRUE, weights = NULL)
diam
g$diameter=diam
# Eccentricity
eccen=eccentricity(g, vids=V(g), mode=c("all"))
eccen
g$eccentricity=eccen
# Radius
rad=radius(g, mode=c("all"))
rad
g$radius=rad
# Number of Triangles
tri=adjacent.triangles (g, vids = V(g))
tri
V(g)$triangles=tri
# Size of Largest Clique
lcli=clique.number(g)
lcli
g$largest_clique=lcli
### WRITE GRAPH TO FILE ###
write.graph(g, "g.gml", format=c("gml")) ##### MODIFY OUTPUT FILE NAMES
write.graph(g, "g.net", format=c("pajek"))
### EDGE-BETWEENNESS COMMUNITIES DENDROGRAM ###
V(g)$name<-V(g)$site_id
ebc=edge.betweenness.community (g, weights = E(g)$weight, directed = FALSE, edge.betweenness = TRUE, merges = TRUE, bridges = TRUE, modularity = TRUE, membership = TRUE)
dend=as.dendrogram(ebc)
png("ebc_dend.png",width=1000,height=800)
par(cex=1,font=3)
plot(dend, main="Title") ##### MODIFY TITLE
dev.off()
### DEGREE DISTRIBUTION CHART ###
degd=degree.distribution(g, cumulative = FALSE)
degd
png("deg_dist.png",width=1000,height=800)
barplot(degd,xlab="Degree",ylab="Frequency (%)", axes=TRUE, names.arg=(1:#), seq(by=5), axisnames=TRUE, axis.lty=1, cex.names=.85, main="Title") ##### MODIFY THE MAXIMUM DEGREE AND TITLE
dev.off()
### SUMMARY ###
g
### COMPLETE ###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment