##### # Initializing rm(list = ls()) cat("\014") if (!require("pacman")) install.packages("pacman") pacman::p_load(igraph, igraphdata, threejs) # If pacman does not work, then get these packages via the following code (uncomment it first) # install.packages(c("igraph", "igraphdata", "threejs")) # library(igraph) # library(igraphdata) # library(threejs) ##### -------------------------------------------------------------------- ### Exercise 1: Generating an igraph object ### ## Run the following lines to create an igraph object "friends". # small social network (self created) elistfriends <- rbind(c('Alex', 'Bruno'), c('Alex', 'Christian'), c('Alex', 'Dana'), c('Alex', 'Erik'), c('Alex', 'Fabi'), c('Erik', 'Fabi'), c('Fabi', 'Gina')) # convert data into an igraph object friends <- graph.edgelist(elistfriends, directed = F) # view graph object friends -------------------- ### Exersise 2 ### ## Look at all vertices, edges, the number of vertices and the number of edges ## of the igraph object "friends". ## Plot the graph "friends". -------------------- ### Exercise 3 ### ## Add the vertex attribute "gender" to the graph object "friends" with ## value = c("m", "m", "m", "w", "m", "m", "w") and query all vertex ## attributes of "friends". friends <- ## Add the edge attribute "contactperweek" to the graph object "friends" with ## value = c(2, 1, 1, 1, 3, 2, 4). Then query all edge attributes of "friends". friends <- ------------------- ### Exercise 4 ### ## Consider the people who meet each other more than once a week. ## View the attributes of the first three vertices of the graph object "friends". ------------------- ### Exercise 5 ### ## Style Adjustments with plotting parameters # a) plot the social network "friends" and adjust the size of the vertices such that every name is readable. # Set the parameters directly in the igraph object. # b) make the following adjustments by setting the plotting parameters within the plot function: # vertex color: green # edge color: black # line type: dashed # set the title to "social network" ------------------- ### Exercise 6 ### ## Layouts # Plot the graph of the "friends" network in the following layouts: # kamada.kawai # fruchterman.reingold # circle # tree ------------------- ### Exercise 7 ### ## Highlighting Graph Aspects # Mark the following group of nodes in a different color to emphasize them. # Node IDs: 1,2,3 ------------------- ### Exercise 8 ### ## Interactive plotting with tkplot # a) Plot the "friends" network with tkplot. # b) Adjust the layout and the set color of the vertices to blue. # c) Get the coordinates from the adjusted plot. ------------------- ### Exercise 9 ### ## Use the macaque dataset for the following exercises. data("macaque") ## the data resembles a simplification of the visuo-tactil monkey-rain-network. # Identify all brain areas (= vertices) that are directly connected to the area "V1". neighbors() ------------------- ### Exercise 10 ### # Check if there are vertices that receive information directly from "V1" and send it directly to "FEF" # Hint: think about the directions that you set in the mode argument! a <- b <- intersection() ------------------- ### Exercise 11 ### # a.) Calculate the out-degrees of all vertices and save the result in the new variable "macout". macout <- # b.) Apply the following function to check which areas have the highest out-degrees. which( == max()) ------------------- ### Exercise 12 ### # a.) Run the following lines to check for loops and multiple edges within the network. any(which_loop(macaque) == TRUE) any_multiple(macaque) # b.) Apply the edge density function to the macaque-network and interpret the value. edge_density() ------------------- ### Exercise 13 ### ## We will still use the macaque dataset. # How many closed triangles can be identified with V1 as a component? ------------------- ### Exercise 14 ### # a.) Save the macaque data as an undirected network (make sure that you do not create multiple edges). macaqueud <- any_multiple(macaqued) # b.) Calculate the largest cliques in the undirected network and plot the first two of them. lc <- clique1 <- induced_subgraph(macaqueud, lc[[]]) clique2 <- induced_subgraph(macaqueud, lc[[]]) par(mfrow = c(1,2)) plot( , vertex.label.color = "black", vertex.label.cex = 1.5, vertex.size = 0, main = "Largest Clique 1", layout = layout.circle(clique1)) plot( , vertex.label.color = "black", vertex.label.cex = 1.5, vertex.size = 0, main = "Largest Clique 2", layout = layout.circle(clique2)) ------------------- ### Exercise 15 ### # Is there a preference for vertices with the same "shape" attribute to associate with each other? # [shape refers to visual nodes (square) vs sensorimotor nodes (circle)] # USE THE ORIGINAL DIRECTED GRAPH macaque$shape <- as.numeric(factor(V( )$ )) assortativity_nominal() ------------------- ### BONUS Exercise 16 ### # Run the following lines and try to understand what is happening and why one would do that macaque_trans <- transitivity(macaque) manygraphs <- vector('list', 1000) for(i in 1:1000){ manygraphs[[i]] <- erdos.renyi.game(n = gorder(macaque), p.or.m = edge_density(macaque), type = "gnp", directed = TRUE) } many_trans <- unlist(lapply(manygraphs, transitivity)) par(mfrow=c(1,1)) hist(many_trans, xlim = range(c(0.1, 0.8))) abline(v = macaque_trans, col = "red", lty = 3, lwd = 2) # Reference for the macaque dataset: # Negyessy L., Nepusz T., Kocsis L., Bazso F.: Prediction of the main cortical areas and # connections involved in the tactile function of the visual cortex by network analysis. # European Journal of Neuroscience, 23(7): 1919-1930, 2006.