Skip to content

Instantly share code, notes, and snippets.

@marsimaria
Created April 1, 2015 13:55
Show Gist options
  • Select an option

  • Save marsimaria/760f9fb92da3a5c060f4 to your computer and use it in GitHub Desktop.

Select an option

Save marsimaria/760f9fb92da3a5c060f4 to your computer and use it in GitHub Desktop.
Network Analysis
library(igraph)
library(plyr)
# load files into tables
orb.edge <- read.csv("/Users/mariafang/Desktop/NYU/2_AncientData/Week9 Network/orbis_edges_0514.csv")
orb.node <- read.csv("/users/mariafang/Desktop/NYU/2_AncientData/Week9 Network/orbis_nodes_0514.csv")
# merge tables in two
orb.tb1 <- merge(orb.edge, orb.node, by.x="source", by.y="id")
orb.tb2 <- merge(orb.edge, orb.node, by.x="target", by.y="id")
# rename orb.tb2 label column
colnames(orb.tb2)[colnames(orb.tb2)=="label"] <- "label2"
# merge main table
orbs.test <- merge(orb.tb1, orb.tb2, by="row.names")
# subset labels
orb.labels <- subset(orbs.test, select=c(label, label2))
# convert to graph
orbGraph <- graph.data.frame(orb.labels, directed=F)
V(orbGraph)
E(orbGraph)
degree(orbGraph)
vertex(orbGraph)
V(orbGraph)$size<-degree(orbGraph)/10
E(orbGraph)$size<-vertex(orbGraph)*10
# plot 1
plot(orbGraph)
# plot 2
plot(orbGraph, layout=layout.fruchterman.reingold, vertex.label.dist=0.5, vertex.frame.color='black', vertex.label.color='blue', vertex.label.font=1, vertex.label=V(orbGraph)$name, vertex.label.cex=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment