Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(igraph)
data = subset(data4, select=c("X","X.1","X.2","X.3","X.4","X.5","X.6","X.7"))
data1_1yr=subset(data4, data4$year=="1")
data_1yr=subset(data1_1yr,select=c("X","X.1","X.2","X.3","X.4","X.5","X.6","X.7") )
data1_2yr=subset(data4, data4$year=="2")
data_2yr=subset(data1_2yr,select=c("X","X.1","X.2","X.3","X.4","X.5","X.6","X.7") )
data1_3yr=subset(data4, data4$year=="3")
data_3yr=subset(data1_3yr,select=c("X","X.1","X.2","X.3","X.4","X.5","X.6","X.7") )
data1_4yr=subset(data4, data4$year=="4")
data_4yr=subset(data1_4yr,select=c("X","X.1","X.2","X.3","X.4","X.5","X.6","X.7") )
df <- data.frame(Roll=character(),No=character())
df$No=as.character(df$No)
df$Roll=as.character(df$Roll)
# A for loop with a paste function could make the coder faster
data$X.7=as.character(data$X.7)
data$X.6=as.character(data$X.6)
data$X.5=as.character(data$X.5)
data$X.4=as.character(data$X.4)
data$X.3=as.character(data$X.3)
data$X.2=as.character(data$X.2)
data$X.1=as.character(data$X.1)
data$X=as.character(data$X)
for(j in 1:nrow(data)){
for(i in 1:7){
df[(j-1)*7+i,1]=data[j,1]
i=1
}
}
for(j in 1:nrow(data)){
for(i in 1:7){
df[(j-1)*7+i,2]=data[j,i+1]
i=1
}
}
df[df == 0] <- NA
df1=subset(df,df$No!="NA")
g1=graph.data.frame(df3, directed=FALSE, vertices=NULL)
plot.igraph(g1, vertex.label=NA, layout=layout.auto(g, dim=2),
vertex.size = degree(g1)*.75,
vertex.frame.color= "black",
vertex.label.cex=1,
edge.width=1)
close=closeness(g1)
deg=degree(g1)
average.path.length(g1, directed=FALSE)
diameter(g1, directed=FALSE)
assortativity.degree(g1, directed = FALSE)
# A similar graph could be plotted for the other variables like branch
caste=subset(data4,select=c("X","year"))
caste=unique(caste)
caste$year=as.character(caste$year)
data2<-graph.data.frame(df_yr3, directed=FALSE)
g<-simplify(data2)
ordered.vertices <-get.data.frame(g, what="vertices")
g<-simplify(data2)
V(g)$color <- ifelse(caste[match(caste[,1],ordered.vertices$name), 2] == "1", "red", ifelse(caste[match(caste[,1],ordered.vertices$name), 2] == "2", "yellow", ifelse(caste[match(caste[,1],ordered.vertices$name), 2] == "3", "green", "blue")))
plot.igraph(g, vertex.label=NA,
vertex.size = degree(g1)*.75,
vertex.frame.color= V(g)$color,
vertex.label.cex=1,
edge.width=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment