Network Analysis and Visualization With R and Igraph
Network Analysis and Visualization With R and Igraph
install.packages("igraph")
1.1 Assignment
You can assign a value to an object using assign() , <- , or = .
x <- 3 # Assignment
y <- 4 # Assignment
y + 5 # Evaluation, y remains 4
z # Evaluation
z # Error!
2==2 # Equality
2!=2 # Inequality
x <= y # less than or equal: "<", ">", and ">=" also work
5/0
NaN (Not a Number) - the result of an operation that cannot be reasonably defined, such as dividing zero by
zero.
0/0
is.nan(0/0)
1.4 Vectors
Vectors can be constructed by combining their elements with the important R function c() .
Combining different types of elements in one vector will coerce the elements to the least restrictive type:
length(v1)
length(v2)
Element-wise operations:
v1 + v2 # Element-wise addition
Mathematical operations:
Logical operations:
Vector elements:
v1[2:4] # elements 2, 3, 4 of v1
Note that the indexing in R starts from 1 , a fact known to confuse and upset people used to languages that
index from 0 .
To add more elements to a vector, simply assign them values.
1.5 Factors
Factors are used to store categorical data.
eye.col.v
eye.col.f
R will identify the different levels of the factor - e.g. all distinct values. The data is stored internally as
integers - each number corresponding to a factor level.
## [1] 2 3 2 1 1 1
## [1] NA NA NA NA NA NA
as.character(eye.col.f)
as.character(eye.col.v)
## [1] 5 4
m <- matrix(1:10,10,10)
m[1,]==m[,1]
m>3
m[m>3]
t(m) # Transpose m
Arrays are used when we have more than 2 dimensions. We can create them using the array() function:
1.7 Lists
Lists are collections of objects. A single list can contain all kinds of elements - character strings, numeric
vectors, matrices, other lists, and so on. The elements of lists are often named for easier access.
l2 <- list(v1,v2,v3,"Animals!")
l3 <- list()
l4 <- NULL
l1[["boo"]] # Access boo with double brackets: this returns the numeric vector
l1$boo # Named elements can be accessed with the $ operator, as with [[]]
l4[[3]] <- c(22, 23) # add a vector as element 3 in the empty list l4.
Since we added element 3 to the list l4 above, elements 1 and 2 will be generated and empty (NULL).
l1[[5]] <- "More elements!" # The list l1 had 4 elements, we're adding a 5th here.
We added an 8th element, but not 6th and 7th to the list l1 above. Elements number 6 and 7 will be
created empty (NULL).
l1$Something <- "A thing" # Adds a ninth element - "A thing", named "Something"
Creating a dataframe:
FirstName=c("John","Jim","Jane","Jill"),
Female=c(F,F,T,T),
Age=c(22,33,44,55) )
Notice that R thinks that dfr1$FirstName is a categorical variable and so it’s treating it like a factor, not a
character vector. Let’s get rid of the factor by telling R to treat ‘FirstName’ as a vector:
Alternatively, you can tell R you don’t like factors from the start using stringsAsFactors=FALSE
dfr1[1:2,3:4] # Rows 1 and 2, columns 3 and 4 - the gender and age of John & Jim
dfr1[dfr1$Age>30,2]
## [1] "Jim" "Jane" "Jill"
mean ( dfr1[dfr1$Female==TRUE,4] )
## [1] 49.5
x <- 5; y <- 10
## [1] 2
for (i in 1:x)
## [1] 15
## [1] 120
# while (condintion) expr
You may notice that RGB here ranges from 0 to 1. While this is the R default, you can also set it for to the 0-
255 range using something like rgb(10, 100, 100, maxColorValue=255) .
We can set the opacity/transparency of an element using the parameter alpha (range 0-1):
If we have a hex color representation, we can set the transparency alpha using adjustcolor from package
grDevices . For fun, let’s also set the plot background to gray using the par() function for graphical
parameters.
par(bg="gray40")
In many cases, we need a number of contrasting colors, or multiple shades of a color. R comes with some
predefined palette function that can generate those for us. For example:
pal1 <- heat.colors(5, alpha=1) # 5 colors from the heat palette, opaque
pal2 <- rainbow(5, alpha=.5) # 5 colors from the heat palette, transparent
We can also generate our own gradients using colorRampPalette . Note that colorRampPalette returns a
function that we can use to generate as many colors from that palette as we need.
1.11 R troubleshooting
While I generate many (and often very creative) errors in R, there are three simple things that will most
often go wrong for me. Those include:
1. Capitalization. R is case sensitive - a graph vertex named “Jack” is not the same as one named “jack”.
The function rowSums won’t work if spelled as rowsums or RowSums .
2. Object class. While many functions are willing to take anything you throw at them, some will still
surprisingly require character vector or a factor instead of a numeric vector, or a matrix instead of a
data frame. Functions will also occasionally return results in an unexpected formats.
3. Package namespaces. Occasionally problems will arise when different packages contain functions
with the same name. R may warn you about this by saying something like “The following object(s) are
masked from ‘package:igraph’ as you load a package. One way to deal with this is to call functions
from a package explicitly using :: . For instance, if function blah() is present in packages A and B,
you can call A::blah and B::blah . In other cases the problem is more complicated, and you may
have to load packages in certain order, or not use them together at all. For example (and pertinent to
this workshop), igraph and Statnet packages cause some problems when loaded at the same time.
It is best to detach one before loading the other.
For more advanced troubleshooting, check out try() , tryCatch() , and debug() .
2. Networks in igraph
rm(list = ls()) # Remove all the objects we created so far.
plot(g1) # A simple plot of the network - we'll talk more about plots later
class(g1)
## [1] "igraph"
g1
## IGRAPH U--- 3 3 --
## + edges:
plot(g2)
g2
## IGRAPH D--- 10 3 --
## + edges:
g3 <- graph( c("John", "Jim", "Jim", "Jill", "Jill", "John")) # named vertices
# When the edge list has vertex names, the number of nodes is not needed
plot(g3)
g3
## IGRAPH DN-- 3 3 --
g4 <- graph( c("John", "Jim", "Jim", "Jack", "Jim", "Jack", "John", "John"),
vertex.frame.color="gray", vertex.label.color="black",
plot(graph_from_literal(a--+b, b+--c))
plot(graph_from_literal(a+-+b, b+-+c))
plot(graph_from_literal(a:b:c---c:d:e))
plot(gl)
g4[]
## 7 x 7 sparse Matrix of class "dgCMatrix"
## John 1 1 . . . . .
## Jim . . 2 . . . .
## Jack . . . . . . .
## Jesse . . . . . . .
## Janis . . . . . . .
## Jennifer . . . . . . .
## Justin . . . . . . .
g4[1,]
## 1 1 0 0 0 0 0
## [7] "Justin"
Examine attributes:
edge_attr(g4)
## $type
##
## $weight
## [1] 10 10 10 10
vertex_attr(g4)
## $name
## [7] "Justin"
##
## $gender
graph_attr(g4)
## named list()
Another way to set attributes (you can similarly use set_edge_attr() , set_vertex_attr() , etc.):
graph_attr_names(g4)
graph_attr(g4, "name")
graph_attr(g4)
## $name
##
## $something
graph_attr(g4)
## $name
The graph g4 has two edges going from Jim to Jack, and a loop from John to himself. We can simplify our
graph to remove loops & multiple edges between the same nodes. Use edge.attr.comb to indicate how
edge attributes are to be combined - possible options include sum , mean , prod (product), min , max ,
first / last (selects the first/last edge’s attribute). Option “ignore” says the attribute should be disregarded
and dropped.
edge.attr.comb=c(weight="sum", type="ignore") )
plot(g4s, vertex.label.dist=1.5)
g4s
## IGRAPH DNW- 7 3 -- Email Network
The two numbers that follow (7 5) refer to the number of nodes and edges in the graph. The description also
lists node & edge attributes, for example:
eg <- make_empty_graph(40)
Full graph
fg <- make_full_graph(40)
st <- make_star(40)
Tree graph
Ring graph
rn <- make_ring(40)
igraph can also give you some notable historical graphs. For instance:
Rewiring a graph
each_edge() is a rewiring method that changes the edge endpoints uniformly randomly with a probability
prob .
head(nodes)
head(links)
nrow(nodes); length(unique(nodes$id))
Notice that there are more links than unique from-to combinations. That means we have cases in the data
where there are multiple links between the same two nodes. We will collapse all links of the same type
between the same two nodes by summing their weights, using aggregate() by “from”, “to”, & “type”. We
don’t use simplify() here so as not to collapse different link types.
head(links2)
dim(links2)
dim(nodes2)
———————————–
d describes the edges of the network. Its first two columns are the IDs of the source and the target
node for each edge. The following columns are edge attributes (weight, type, label, or anything else).
vertices starts with a column of node IDs. Any following columns are interpreted as node attributes.
4.1 Dataset 1
library(igraph)
class(net)
## [1] "igraph"
net
## IGRAPH DNW- 17 49 --
We also have easy access to nodes, edges, and their attributes with:
Now that we have our igraph network object, let’s make a first attempt to plot it.
plot(net, edge.arrow.size=.4,vertex.label=NA)
That doesn’t look very good. Let’s start fixing things by removing the loops in the graph.
If you need them, you can extract an edge list or a matrix from igraph networks.
as_edgelist(net, names=T)
as_adjacency_matrix(net, attr="weight")
as_data_frame(net, what="edges")
as_data_frame(net, what="vertices")
4.2 Dataset 2
As we have seen above, this time the edges of the network are in a matrix format. We can read those into a
graph object using graph_from_incidence_matrix() . In igraph, bipartite networks have a node attribute
called type that is FALSE (or 0) for vertices in one mode and TRUE (or 1) for those in the other mode.
head(nodes2)
## 6 s06 CNN 2 TV 56
head(links2)
## U01 U02 U03 U04 U05 U06 U07 U08 U09 U10 U11 U12 U13 U14 U15 U16 U17
## s01 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## s02 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
## s03 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
## s04 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
## s05 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
## s06 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1
## s01 0 0 0
## s02 0 0 1
## s03 0 0 0
## s04 0 0 0
## s05 0 0 0
## s06 0 0 0
table(V(net2)$type)
##
## FALSE TRUE
## 10 20
We can also easily generate bipartite projections for the two-mode network: (co-memberships are easy to
calculate by multiplying the network matrix by its transposed matrix, or using igraph’s
bipartite.projection() function).
vertex.size=7, vertex.label=nodes2$media[!is.na(nodes2$media.type)])
We can set the node & edge options in two ways - the first one is to specify them in the plot() function, as
we are doing below.
# Replace the vertex label with the node names stored in "media"
vertex.color="orange", vertex.frame.color="#555555",
vertex.label=V(net)$media, vertex.label.color="black",
vertex.label.cex=.7)
The second way to set attributes is to add them to the igraph object. Let’s say we want to color our network
nodes based on type of media, and size them based on audience size (larger audience -> larger node). We
will also change the width of the edges based on their weight.
# Generate colors based on media type:
V(net)$label <- NA
E(net)$arrow.size <- .2
plot(net)
Sometimes, especially with semantic networks, we may be interested in plotting only the labels of the
nodes:
vertex.label.font=2, vertex.label.color="gray40",
vertex.label.cex=.7, edge.color="gray85")
Let’s color the edges of the graph based on their source node color. We can get the starting node for each
edge with the ends() igraph function.
For the purposes of exploring layouts, we will generate a slightly larger 80-node graph. We use the
sample_pa() function which generates a simple graph starting from one node and adding more nodes and
links based on a preset level of preferential attachment (Barabasi-Albert model).
net.bg <- sample_pa(80)
V(net.bg)$size <- 8
E(net.bg)$arrow.mode <- 0
plot(net.bg)
plot(net.bg, layout=layout_randomly)
l <- layout_in_circle(net.bg)
plot(net.bg, layout=l)
l is simply a matrix of x, y coordinates (N x 2) for the N nodes in the graph. You can easily generate your
own:
plot(net.bg, layout=l)
This layout is just an example and not very helpful - thankfully igraph has a number of built-in layouts,
including:
l <- layout_randomly(net.bg)
plot(net.bg, layout=l)
# Circle layout
l <- layout_in_circle(net.bg)
plot(net.bg, layout=l)
# 3D sphere layout
l <- layout_on_sphere(net.bg)
plot(net.bg, layout=l)
Fruchterman-Reingold is one of the most used force-directed layout algorithms out there.
Force-directed layouts try to get a nice-looking graph where edges are similar in length and cross each
other as little as possible. They simulate the graph as a physical system. Nodes are electrically charged
particles that repulse each other when they get too close. The edges act as springs that attract connected
nodes closer together. As a result, nodes are evenly distributed through the chart area, and the layout is
intuitive in that nodes which share more connections are closer to each other. The disadvantage of these
algorithms is that they are rather slow and therefore less often used in graphs larger than ~1000 vertices.
You can set the “weight” parameter which increases the attraction forces among nodes connected by
heavier edges.
l <- layout_with_fr(net.bg)
plot(net.bg, layout=l)
You will notice that the layout is not deterministic - different runs will result in slightly different configurations.
Saving the layout in l allows us to get the exact same result multiple times, which can be helpful if you
want to plot the time evolution of a graph, or different relationships – and want nodes to stay in the same
place in multiple plots.
plot(net.bg, layout=layout_with_fr)
plot(net.bg, layout=layout_with_fr)
plot(net.bg, layout=l)
plot(net.bg, layout=l)
dev.off()
By default, the coordinates of the plots are rescaled to the [-1,1] interval for both x and y. You can change
that with the parameter rescale=FALSE and rescale your plot manually by multiplying the coordinates by a
scalar. You can use norm_coords to normalize the plot with the boundaries you want.
l <- layout_with_fr(net.bg)
par(mfrow=c(2,2), mar=c(0,0,0,0))
dev.off()
Another popular force-directed algorithm that produces nice results for connected graphs is Kamada Kawai.
Like Fruchterman Reingold, it attempts to minimize the energy in a spring system.
l <- layout_with_kk(net.bg)
plot(net.bg, layout=l)
The LGL algorithm is meant for large, connected graphs. Here you can also specify a root: a node that will
be placed in the middle of the layout.
plot(net.bg, layout=layout_with_lgl)
par(mfrow=c(3,3), mar=c(1,1,1,1))
print(layout)
Notice that our network plot is still not too helpful. We can identify the type and size of nodes, but cannot
see much about the structure since the links we’re examining are so dense. One way to approach this is to
see if we can sparsify the network, keeping only the most important ties and discarding the rest.
hist(links$weight)
mean(links$weight)
sd(links$weight)
There are more sophisticated ways to extract the key edges, but for the purposes of this exercise we’ll only
keep ones that have weight higher than the mean for the network. In igraph, we can delete edges using
delete_edges(net, edges) :
plot(net.sp)
Another way to think about this is to plot the two tie types (hyperlink & mention) separately.
vertex.color="gray40", layout=layout.circle)
par(mfrow=c(1,2))
l <- layout_with_fr(net)
dev.off()
tkid <- tkplot(net) #tkid is the id of the tkplot that will open
tk_close(tkid, window.close = T)
plot(net, layout=l)
5.5 Other ways to represent a network
At this point it might be useful to provide a quick reminder that there are many ways to represent a network
not limited to a hairball plot.
scale="none", margins=c(10,10) )
5.6 Plotting two-mode networks with igraph
As with one-mode networks, we can modify the network object to include the visual properties that will be
used by default when plotting the network. Notice that this time we will also change the shape of the nodes -
media outlets will be squares, and their users will be circles.
V(net2)$label.cex=.4
V(net2)$label.font=2
vertex.label.color=V(net2)$color, vertex.label.font=2.5,
edge_density(net, loops=F)
## [1] 0.1764706
## [1] 0.1764706
6.2 Reciprocity
The proportion of reciprocated ties (for a directed network).
reciprocity(net)
6.3 Transitivity
global - ratio of triangles (direction disregarded) to connected triples.
local - ratio of triangles to connected triples each vertex is part of.
transitivity(net, type="local")
6.4 Diameter
A network diameter is the longest geodesic distance (length of the shortest path between two nodes) in the
network. In igraph , diameter() returns the distance, while get_diameter() returns the nodes along the
first found path of that distance.
Note that edge weights are used by default, unless set to NA.
## [1] 4
diameter(net, directed=F)
## [1] 28
diam
Note that get_diameter() returns a vertex sequence. Note though that when asked to behaved as a
vector, a vertex sequence will produce the numeric indexes of the nodes in it. The same applies for edge
sequences.
class(diam)
## [1] "igraph.vs"
as.vector(diam)
## [1] 12 6 17 4 3 8 7
plot(net, vertex.size=deg*3)
par(mfrow=c(1,2))
mean_distance(net, directed=F)
## [1] 2.058824
mean_distance(net, directed=T)
## [1] 2.742188
We can also find the length of all shortest paths in the graph:
We can extract the distances to a node or set of nodes we are interested in. Here we will get the distance of
every media from the New York Times.
dist.from.NYT <- distances(net, v=V(net)[media=="NY Times"], to=V(net), weights=NA)
vertex.label.color="white")
We can also find the shortest path between specific nodes. Say here between MSNBC and the New York
Post:
news.path <- shortest_paths(net,
from = V(net)[media=="MSNBC"],
ew[unlist(news.path$epath)] <- 4
edge.width=ew, edge.arrow.mode=0)
Identify the edges going into or out of a vertex, for instance the WSJ. For a single node, use incident() ,
for multiple nodes use incident_edges()
inc.edges <- incident(net, V(net)[media=="Wall Street Journal"], mode="all")
We can also easily identify the immediate neighbors of a vertex, say WSJ. The neighbors function finds all
nodes one step out from the focal actor.To find the neighbors for multiple nodes, use adjacent_vertices()
instead of neighbors() . To find node neighborhoods going more than one step out, use function ego()
with parameter order set to the number of steps out to go from the focal node(s).
plot(net, vertex.color=vcol)
Special operators for the indexing of edge sequences: %–%, %->%, %<-%
* E(network)[X %–% Y] selects edges between vertex sets X and Y, ignoring direction
* E(network)[X %->% Y] selects edges from vertex sets X to vertex set Y
* E(network)[X %->% Y] selects edges from vertex sets Y to vertex set X
For example, select edges from newspapers to online sources:
Co-citation (for a couple of nodes, how many shared nominations they have):
cocitation(net)
## s01 s02 s03 s04 s05 s06 s07 s08 s09 s10 s11 s12 s13 s14 s15 s16 s17
## s01 0 1 1 2 1 1 0 1 2 2 1 1 0 0 1 0 0
## s02 1 0 1 1 0 0 0 0 1 0 0 0 0 0 2 0 0
## s03 1 1 0 1 0 1 1 1 2 2 1 1 0 1 1 0 1
## s04 2 1 1 0 1 1 0 1 0 1 1 1 0 0 1 0 0
## s05 1 0 0 1 0 0 0 1 0 1 1 1 0 0 0 0 0
## s06 1 0 1 1 0 0 0 0 0 0 1 1 1 1 0 0 2
## s07 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0
## s08 1 0 1 1 1 0 0 0 0 2 1 1 0 1 0 0 0
## s09 2 1 2 0 0 0 1 0 0 1 0 0 0 0 1 0 0
## s10 2 0 2 1 1 0 0 2 1 0 1 1 0 1 0 0 0
## s11 1 0 1 1 1 1 0 1 0 1 0 2 1 0 0 0 1
## s12 1 0 1 1 1 1 0 1 0 1 2 0 0 0 0 0 2
## s13 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0
## s14 0 0 1 0 0 1 0 1 0 1 0 0 1 0 0 0 0
## s15 1 2 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0
## s16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
## s17 0 0 1 0 0 2 0 0 0 0 1 2 0 0 0 1 0
We can create an undirected link between any pair of connected nodes ( mode="collapse" )
Create undirected link for each directed one in the network, potentially ending up with a multiplex
graph ( mode="each" )
Create undirected link for each symmetric link in the graph ( mode="mutual" ).
In cases when we may have ties A -> B and B -> A ties collapsed into a single undirected link, we need to
specify what to do with their edge attributes using the parameter ‘edge.attr.comb’ as we did earlier with
simplify() . Here we have said that the ‘weight’ of the links should be summed, and all other edge
attributes ignored and dropped.
edge.attr.comb=list(weight="sum", "ignore"))
8.1 Cliques
Find cliques (complete subgraphs of an undirected graph)
dendPlot(ceb, mode="hclust")
plot(ceb, net)
class(ceb)
## [1] "communities"
## [1] 5
## s01 s02 s03 s04 s05 s06 s07 s08 s09 s10 s11 s12 s13 s14 s15 s16 s17
## 1 2 3 4 1 4 3 3 5 5 4 4 4 4 1 4 4
## [1] 0.292476
High modularity for a partitioning reflects dense connections within communities and sparse connections
across communities.
plot(clp, net)
Community detection based on greedy optimization of modularity
plot(cfg, as.undirected(net))
We can also plot the communities without relying on their built-in plot:
plot(net, vertex.color=colrs[V(net)$community])
8.3 K-core decomposition
The k-core is the maximal subgraph in which every node has degree of at least k. The result here gives the
coreness of each vertex in the network. A node has coreness D if it belongs to a D-core but not to (D+1)-
core.
## [1] 0.1715568
## [1] -0.1102857
assortativity_degree(net, directed=F)
## [1] -0.009551146