Skip to content

Instantly share code, notes, and snippets.

@aurora-mareviv
Last active December 17, 2015 06:58
Show Gist options
  • Save aurora-mareviv/5568957 to your computer and use it in GitHub Desktop.
Save aurora-mareviv/5568957 to your computer and use it in GitHub Desktop.
Network graphs with igraph!
###########################################
### Network graphs using package igraph ###
###########################################
# Author: Aurora-Mareviv
# Under GNU General Public License
# Script not intended for running using source()
# Simple script that draws a network
library(igraph)
load("my_directory/my_file.rda")
# An example of valid data structure:
MTM <- c(0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1)
FI <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
MCLI <- c(0,0,1,0,0,1,1,1,0,0,0,0,1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1)
matx <- data.frame(MTM,FI,MCLI)
# Now the instructions:
# First, you can eliminate the columns without any links.
# This command plots the graph directly:
G <- graph.incidence(as.matrix(matx),weighted=TRUE,directed=FALSE)
summary(G)
plot.igraph(G, vertex.color="yellow")
# To edit graph appearance, the best option -maybe- is to use the GUI tkplot
tkplot(G) # GUI
# We modify any element we want (using right-button clicks). We save in "Export>>postscript"
# WARNING: the tkplot() command "Export>>postscript", stores a file in EPS format.
# To convert .EPS to .PDF, read: https://gist.github.com/aurora-mareviv/ae2b1a767915bdff26df
# The coordinates of the plot (positions of nodes and edges) are stored in:
tkplot.getcoords(1) # Where the number is the tk.id, "1" in this case.
coords <- tkplot.getcoords(1) # Do not close tkplot window before storing the coordinates.
# To use the new coordinates, we can use this recipe modified from http://www.stanford.edu/~messing/Affiliation%20Data.html
G$layout = coords
V(G)$label = V(G)$name
V(G)$label.color = rgb(0,0,.2,.6)
V(G)$size = 6
V(G)$frame.color = NA
V(G)$color = rgb(0,0,1,.5)
# Set edge attributes
E(G)$arrow.size = .3
# Set edge gamma according to edge weight
egam = (E(G)$weight+.1)/max(E(G)$weight+.1)
E(G)$color = rgb(.5,.5,0,egam)
# csize <- clusters(G)$csize
# V(G)$csize <- csize
# V(G)$label.cex = V(G)$csize/(max(V(G)$csize)/2)+ .3
# #note, unfortunately one must play with the formula above to get the ratio just right.
# pdf("networkIDIS.pdf")
plot(G)
# dev.off()
###########################################
### Network graphs from adjacency matrix ##
###########################################
# To show only the ANEP fields interconnected, we must construct an adjacency matrix from matx:
matx <- as.matrix(matx)
adj_mat = t(matx) %*% (matx)
adj_mat
# Adj_mat shows the number or connections between each node MTM, FI or MCLI.
# The connections are reciprocal, so the matrix is symmetrical.
# Now we will plot this matrix.
# Two ways to show edge weights:
# png("igraphs.png", width=10, height=5, units="in", res=200)
# par(mfrow=c(1, 2))
g1 = graph.adjacency(adj_mat, mode="undirected", diag=FALSE, weighted=TRUE)
plot(g1, edge.width=E(g1)$weight, vertex.size=50)
g2 = graph.adjacency(adj_mat, mode="undirected", diag=FALSE)
plot(g2, vertex.size=50)
# dev.off()
# Now, we have a cleaner plot than if every project was shown.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment