#library library(igraph) library(sand) library(igraphdata) library(sna) library(network) library(statnet) library(intergraph) ####upload the data data(aidsblog) ?aidsblog data(yeast) ?yeast #let's explore the data ?yeast ?aidsblog #first visualization plot(yeast, vertex.label=NA, vertex.size=3, layout=layout_with_kk, edge.arrow.size=0.2) plot(aidsblog, vertex.label=NA, vertex.size=3, layout=layout_with_kk,edge.arrow.size=0.2) #Let's start!! #According to the nature of the data: #1) compute and comment some global network statistics #(including size, number of edges, and diameter, please refer #for instance to the example of co-authorship in the data collection slides) #network exploration: E(yeast) # The edges of the object V(yeast) # The verteces of the object yeast[]#to visualize the matrix class(yeast) #it is possibile to convert the igraph object into a network. #Ofc it is possible to work also without converting ?asNetwork net_yeast<-asNetwork(yeast)###using the library intergraph we can convert the igraph obj into a network net_aids <- asNetwork(aidsblog) #secondo network gplot(net_yeast) ?gplot#visualization using the library sna igraph::ecount(yeast) #number of edges gsize(yeast) # same result of ecount. Infact ecount() and gsize() are aliases vcount(yeast) #number of vertices gorder(yeast) #vcount() and gorder() are aliases. #let's see some other functions: igraph::edge_density(yeast, loops=F) #The proportion of present edges from all possible edges in the network. sna::gden(net_yeast, mode="graph") ?gden# Density network::network.size(net_yeast) #network size returns the number of vertices length(yeast) #returns the number of vertices sna::isolates(net_yeast) #returns a list of the isolated nodes #let's see the degree deg_yeast <- igraph::degree(yeast) #the number of its adjacent edges. sna::degree(net_yeast) max(deg_yeast) min(deg_yeast) mean(igraph::degree(yeast)) #other way to find isolates node which(igraph::degree(yeast)==0) table(deg_yeast) plot(yeast, vertex.size=deg_yeast/5, layout=layout_with_kk, vertex.label=NA,edge.arrow.size=0.5) #we don't have any node with degree 0 igraph::diameter(yeast, directed=F) #diameter: The diameter of a graph is the length of the longest geodesic. igraph::reciprocity(yeast) #######The reciprocity of directed network reflects the proportion of edges that are symmetrical. igraph::reciprocity(aidsblog) aids_simply <- simplify(aidsblog, remove.multiple = F, remove.loops = T) plot(aids_simply, vertex.label=NA, vertex.size=3, layout=layout_with_kk,edge.arrow.size=0.2) ?transitivity igraph::transitivity(yeast) #Transitivity measures the probability that the adjacent vertices of a vertex are connected. #This is sometimes also called the clustering coefficient. sna::gtrans(net_yeast[,]) sna::betweenness(net_yeast, gmode="graph") # betweenness igraph::betweenness(yeast) ?betweenness igraph::closeness(yeast) #closeness ?centr_clo centr <- igraph::centr_clo(yeast, mode="all", normalized=T) #centralize according to the centr$centralization #The graph level centrality index.Concentration of edges hist(igraph::degree(yeast), col="blue",breaks=50, xlim=c(0, 50), xlab="Vertex Degree", ylab="Frequency", main="") #degree hist(igraph::degree(yeast)) average.path.length(yeast) diameter(yeast) ####second network ?degree deg_all <- igraph::degree(aidsblog, mode="all") # Default: total degree ideg <- igraph::degree(aidsblog, mode="out") odeg <- igraph::degree(aidsblog, mode="in") deg_all2 <- sna::degree(net_yeast, gmode="graph") # Default: total degree ideg2 <- sna::degree(net_yeast, cmode="indegree") odeg3 <- sna::degree(net_yeast, cmode="outdegree") diameter(aidsblog, directed=T) #diameter #2) if needed decompose the network into distinct components #Component subgraphs (or simply components) are portions of the network that are disconnected from each other. V(yeast)$name #3) compute some centrality scores for the individual vertices #ev <- sna::evcent(net_yeast) # eigenvector centrality #ev #evdf <- data.frame(net_yeast %v% "name",ev) #evdf #evdf[order(evdf$ev, decreasing = TRUE), c(1,2)] ?centralization centralization(net_yeast, degree) # Concentration of edges centralization(net_yeast, sna::betweenness) sna::dyad.census(net_yeast) #sna::triad.census(net_yeast) #triad.census(netgot, mode="graph") tc<-sna::triad.census(netgot) # directed triad census barplot(tc, names.arg=colnames(tc), las=2) barplot(tc[-1]/sum(triad.census(netgot)[-1]), names.arg=colnames(tc)[-1], las=2) edge_density(yeast) ##### comp_y <- sna::components(net_yeast)# Strong component count comp_a <- sna::components(net_aids) comp_y2 <- components(net_yeast, connected="weak") # Weak component count comp_a2 <- components(net_aids, connected="weak") # Weak component count ##get the largest component cl_y <- component.largest(net_yeast, connected="weak") cl_y cl_y2 <- component.largest(net_yeast) cl_y2 #plot gplot(net_yeast[cl_y,cl_y], boxed.lab=FALSE, label.cex=0.5,label.col=4,label=network.vertex.names(net_yeast)[cl_y]) ?decompose comps_y <- decompose.graph(yeast) table(sapply(comps_y, vcount)) yeast.gc <- decompose.graph(yeast)[[1]] plot(yeast.gc, vertex.label="", vertex.size=5) #4) extract the ego-network the most central vertex according to at least 2 centrality score (e.g., degree and closeness) and compute and comment the local clustering coefficient select <- which(V(yeast)==max(deg_yeast)) nodes_of_interest <- c("YDL136W") #V(yeast)$name #selnodes <- V(yeast)[name %in% nodes_of_interest] #selegoV <- ego(yeast, order=1, nodes = selnodes, mode = "all", mindist = 0) #plot(selegoV[[2]], vertex.label=NA) #class(selegoV) # turn the returned list of igraph.vs objects into a graph #selegoG <- induced_subgraph(yeast,unlist(selegoV)) # plot the subgraph plot(selegoG, vertex.size=5, vertex.label=NA, edge.size=0.2) ego.instr_y <- induced_subgraph(yeast, degree) ego.instr <- induced_subgraph(yeast, igraph::neighborhood(yeast, order= 1, n= "YDL136W" )[[1]])