Polnet 2015 Network Viz Tutorial - Ognyanova
Polnet 2015 Network Viz Tutorial - Ognyanova
Contents
1
Introduction: Network Visualization
The main concern in designing a network visualization is the purpose it has to serve. What are the
structural properties that we want to highlight?
A B
Network maps are far from the only visualization available for graphs - other network representation
formats, and even simple charts of key characteristics, may be more appropriate in some cases.
2
In network maps, as in other visualization formats, we have several key elements that control the
outcome. The major ones are color, size, shape, and position.
Color Position
Size Shape
Modern graph layouts are optimized for speed and aesthetics. In particular, they seek to minimize
overlaps and edge crossing, and ensure similar edge length across the graph.
Layout aesthetics
3
Note: You can download all workshop materials here, or visit kateto.net/polnet2015.
In this tutorial, we will work primarily with two small example data sets. Both contain data about
media organizations. One involves a network of hyperlinks and mentions among news sources. The
second is a network of links between media venues and consumers. While the example data used
here is small, many of the ideas behind the visualizations we will generate apply to medium and
large-scale networks. This is also the reason why we will rarely use certain visual properties such
as the shape of the node symbols: those are impossible to distinguish in larger graph maps. In
fact, when drawing very big networks we may even want to hide the network edges, and focus on
identifying and visualizing communities of nodes. At this point, the size of the networks you can
visualize in R is limited mainly by the RAM of your machine. One thing to emphasize though is
that in many cases, visualizing larger networks as giant hairballs is less helpful than providing charts
that show key characteristics of the graph.
This tutorial uses several key packages that you will need to install in order to follow along. Several
other libraries will be mentioned along the way, but those are not critical and can be skipped. The
main libraries we are going to use are igraph (maintained by Gabor Csardi and Tamas Nepusz),
sna & network (maintained by Carter Butts and the Statnet team), and ndtv (maintained by Skye
Bender-deMoll).
install.packages("igraph")
install.packages("network")
install.packages("sna")
install.packages("ndtv")
DATASET 1: edgelist
The first data set we are going to work with consists of two files, Media-Example-NODES.csv and
Media-Example-EDGES.csv (download here.
4
nodes <- read.csv("Dataset1-Media-Example-NODES.csv", header=T, as.is=T)
links <- read.csv("Dataset1-Media-Example-EDGES.csv", header=T, as.is=T)
head(nodes)
head(links)
nrow(nodes); length(unique(nodes$id))
nrow(links); nrow(unique(links[,c("from", "to")]))
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:
DATASET 2: matrix
head(nodes2)
head(links2)
We start by converting the raw data to an igraph network object. Here we use igraphs
graph.data.frame function, which takes two data frames: d and vertices.
5
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.
library(igraph)
## IGRAPH DNW- 17 49 --
## + attr: name (v/c), media (v/c), media.type (v/n), type.label
## (v/c), audience.size (v/n), type (e/c), weight (e/n)
The two numbers that follow (17 49) refer to the number of nodes and edges in the graph. The
description also lists node & edge attributes, for example:
We also have easy access to nodes, edges, and their attributes with:
Now that we have our igraph network object, lets make a first attempt to plot it.
6
s13
s14
s17
s16
s11 s12
s07
s06
s04
s08 s03
s10
s01 s15
s02
s09 s05
That doesnt look very good. Lets start fixing things by removing the loops in the graph.
You might notice that we could have used simplify to combine multiple edges by summing
their weights with a command like simplify(net, edge.attr.comb = list( Weight = "sum",
"ignore" )). The problem is that this would also combine multiple edge types (in our data:
hyperlinks and mentions).
Lets and reduce the arrow size and remove the labels (we do that by setting them to NA):
plot(net, edge.arrow.size=.4,vertex.label=NA)
7
A brief detour I: Colors in R plots
Colors are pretty, but more importantly they help people differentiate between types of objects, or
levels of an attribute. In most R functions, you can use named colors, hex, or RGB values. In the
simple base R plot chart below, x and y are the point coordinates, pch is the point symbol shape,
cex is the point size, and col is the color. To see the parameters for plotting in base R, check out
?par
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, lets also set the plot background to gray using the par() function for
graphical parameters.
par(bg="gray40")
col.tr <- grDevices::adjustcolor("557799", alpha=0.7)
plot(x=1:5, y=rep(5,5), pch=19, cex=12, col=col.tr, xlim=c(0,6))
8
If you plan on using the built-in color names, heres how to list all of them:
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
plot(x=1:10, y=1:10, pch=19, cex=5, col=pal1)
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.
9
palf <- colorRampPalette(c("gray80", "dark red"))
plot(x=10:1, y=1:10, pch=19, cex=5, col=palf(10))
Finding good color combinations is a tough task - and the built-in R palettes are rather limited.
Thankfully there are other available packages for this:
# If you don't have R ColorBrewer already, you will need to install it:
install.packages("RColorBrewer")
library(RColorBrewer)
display.brewer.all()
This package has one main function, called brewer.pal. To use it, you just need to select the
desired palette and a number of colors. Lets take a look at some of the RColorBrewer palettes:
display.brewer.pal(8, "Set3")
10
display.brewer.pal(8, "Spectral")
display.brewer.pal(8, "Blues")
Using different fonts for R plots may take a little bit of work. This is especially true if you are using
Windows - Mac & Linux users can most likely skip all of this.
In order to import fonts from the OS into R, we can use the extrafont package:
install.packages("extrafont")
library(extrafont)
# Import system fonts - may take a while, so DO NOT run this during the workshop.
font_import()
fonts() # See what font families are available to you now.
loadfonts(device = "win") # use device = "pdf" for pdf plot output.
Now that your fonts are available, you should be able to do something like this:
11
library(extrafont)
plot(net, vertex.size=30)
plot(net, vertex.size=30, vertex.label.family="Arial Black" )
s14 s09
s13
s02 s05
s07
s10
s11 s08 s01
s08 s12 s15
s17 s03
s10 s07
s03 s04
s04 s11
s16 s06
s06 s12
s09 s02 s14
s01 s17 s16
s05 s15 s13
When you save plots as PDF files, you can also embed the fonts:
# First you may have to let R know where to find ghostscript on your machine:
Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.10/bin/gswin64c.exe")
# pdf() will send all the plots we output before dev.off() to a pdf file:
pdf(file="ArialBlack.pdf")
plot(net, vertex.size=30, vertex.label.family="Arial Black" )
dev.off()
embed_fonts("ArialBlack.pdf", outfile="ArialBlack_embed.pdf")
Plotting with igraph: the network plots have a wide set of parameters you can set. Those include
node options (starting with vertex.) and edge options (starting with edge.). A list of selected
options is included below, but you can also check out ?igraph.plotting for more information.
Plotting parameters
12
NODES
vertex.color Node color
vertex.frame.color Node border color
vertex.shape One of none, circle, square, csquare, rectangle
crectangle, vrectangle, pie, raster, or sphere
vertex.size Size of the node (default is 15)
vertex.size2 The second size of the node (e.g. for a rectangle)
vertex.label Character vector used to label the nodes
vertex.label.family Font family of the label (e.g.Times, Helvetica)
vertex.label.font Font: 1 plain, 2 bold, 3, italic, 4 bold italic, 5 symbol
vertex.label.cex Font size (multiplication factor, device-dependent)
vertex.label.dist Distance between the label and the vertex
vertex.label.degree The position of the label in relation to the vertex,
where 0 right, pi is left, pi/2 is below, and -pi/2 is above
EDGES
edge.color Edge color
edge.width Edge width, defaults to 1
edge.arrow.size Arrow size, defaults to 1
edge.arrow.width Arrow width, defaults to 1
edge.lty Line type, could be 0 or blank, 1 or solid, 2 or dashed,
3 or dotted, 4 or dotdash, 5 or longdash, 6 or twodash
edge.label Character vector used to label edges
edge.label.family Font family of the label (e.g.Times, Helvetica)
edge.label.font Font: 1 plain, 2 bold, 3, italic, 4 bold italic, 5 symbol
edge.label.cex Font size for edge labels
edge.curved Edge curvature, range 0-1 (FALSE sets it to 0, TRUE to 0.5)
arrow.mode Vector specifying whether edges should have arrows,
possible values: 0 no arrow, 1 back, 2 forward, 3 both
OTHER
margin Empty space margins around the plot, vector with length 4
frame if TRUE, the plot will be framed
main If set, adds a title to the plot
sub If set, adds a subtitle to the plot
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.
13
s08 s09
s10
s07
s02
s03
s05
s14 s11
s01
s12
s04
s13 s15
s06
s17
s16
# Set edge color to light gray, the node & border color to orange
# Replace the vertex label with the node names stored in "media"
plot(net, edge.arrow.size=.2, edge.color="orange",
vertex.color="orange", vertex.frame.color="#ffffff",
vertex.label=V(net)$media, vertex.label.color="black")
WashingtonPost.com
NYTimes.com
USA Today Google News
Yahoo News
NY Times
LA Times BBC
Wall Street Journal Reuters.com
Washington Post
CNN
ABC
FOX News
MSNBC
The second way to set attributes is to add them to the igraph object. Lets say we want to color
our network nodes based on type of media, and size them based on degree centrality (more links ->
larger node) We will also change the width of the edges based on their weight.
# Compute node degrees (#links) and use that to set node size:
deg <- degree(net, mode="all")
14
V(net)$size <- deg*3
# We could also use the audience size value:
V(net)$size <- V(net)$audience.size*0.6
15
It helps to add a legend explaining the meaning of the colors we used:
plot(net)
legend(x=-1.5, y=-1.1, c("Newspaper","Television", "Online News"), pch=21,
col="#777777", pt.bg=colrs, pt.cex=2, cex=.8, bty="n", ncol=1)
Newspaper
Television
Online News
Sometimes, especially with semantic networks, we may be interested in plotting only the labels of
the nodes:
FOX News
BBC NYTimes.com
Reuters.com USA Today
Yahoo News
New York Post
Google News AOL.com
WashingtonPost.com
16
Lets color the edges of the graph based on their source node color. We can get the starting node
for each edge with the get.edges igraph function.
Network Layouts
Network layouts are simply algorithms that return coordinates for each node in a network.
For the purposes of exploring layouts, we will generate a slightly larger 80-node graph. We use the
barabasi.game function which generates a simple graph starting from one node and adding more
nodes and links based on a preset level of preferential attachment (how much new actors would
prefer to form links to the more popular nodes in the network).
17
You can set the layout in the plot function:
plot(net.bg, layout=layout.random)
l <- layout.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:
18
l <- matrix(c(1:vcount(net.bg), c(1, vcount(net.bg):2)), vcount(net.bg), 2)
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:
# Circle layout
l <- layout.circle(net.bg)
plot(net.bg, layout=l)
19
# 3D sphere layout
l <- layout.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.
Some parameters you can set for this layout include area (the default is the square of # nodes) and
repulserad (cancellation radius for the repulsion - the area multiplied by # nodes). Both
parameters affect the spacing of the plot - play with them until you like the results.
You can also set the weight parameter which increases the attraction forces among nodes connected
by heavier edges.
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
20
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.
The layout fruchterman.reingold.grid is similar to fruchterman.reingold, but faster.
dev.off() # shut off the graphic device to clear the two-figure configuration.
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. Igraph
also has a spring-embedded layout called layout.spring().
l <- layout.kamada.kawai(net.bg)
plot(net.bg, layout=l)
21
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.lgl)
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 layout.norm to normalize the plot with the boundaries you
want.
l <- layout.fruchterman.reingold(net.bg)
l <- layout.norm(l, ymin=-1, ymax=1, xmin=-1, xmax=1)
par(mfrow=c(2,2), mar=c(0,0,0,0))
plot(net.bg, rescale=F, layout=l*0.4)
plot(net.bg, rescale=F, layout=l*0.6)
plot(net.bg, rescale=F, layout=l*0.8)
plot(net.bg, rescale=F, layout=l*1.0)
22
dev.off()
The layout igraph uses by default is called layout.auto. It automatically selects an appropriate
layout algorithm based on the properties (size and connectedness) of the graph.
Lets take a look at all available layouts in igraph:
par(mfrow=c(3,3))
dev.off()
23
layout.kamada.kawai layout.lgl layout.mds
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 were 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
well only keep ones that have weight higher than the mean for the network. In igraph, we can
delete edges using delete.edges(net, edges):
24
cut.off <- mean(links$weight)
net.sp <- delete.edges(net, E(net)[weight<cut.off])
l <- layout.fruchterman.reingold(net.sp, repulserad=vcount(net)^2.1)
plot(net.sp, layout=l)
Another way to think about this is to plot the two tie types (hyperlink & mention) separately.
par(mfrow=c(1,2))
plot(net.h, vertex.color="orange", main="Tie: Hyperlink")
plot(net.m, vertex.color="lightsteelblue2", main="Tie: Mention")
25
Tie: Hyperlink Tie: Mention
l <- layout.fruchterman.reingold(net)
plot(net.h, vertex.color="orange", layout=l, main="Tie: Hyperlink")
plot(net.m, vertex.color="lightsteelblue2", layout=l, main="Tie: Mention")
dev.off()
We can also try to make the network map more useful by showing the communities within it:
26
Highlighting specific nodes or links
Sometimes we want to focus the visualization on a particular node or a group of nodes. In our
example media network, we can examine the spread of information from focal actors. For instance,
lets represent distance from the NYT. The shortest.paths function (as its name indicates) returns
a matrix of shortest paths between nodes in the network.
3
3 2
2 2
2
2
2 1
1
2
3 2 1
0
1
1
Or we can show all the immediate neighbors of the WSJ. Note that the neighbors function finds
all nodes one step out from the focal actor. The corresponding function that finds all edges for a
node is incident.
27
Another way to draw attention to a group of nodes is to mark them:
28
# Generate node color variable:
vcol <- rep("gray40", vcount(net))
vcol[unlist(news.path$vpath)] <- "gold"
R and igraph allow for interactive plotting of networks. This might be a useful option for you if you
want to tweak slightly the layout of a small graph. After adjusting the layout manually, you can get
the coordinates of the nodes and use them for other plots.
tkid <- tkplot(net) #tkid is the id of the tkplot that will open
l <- tkplot.getcoords(tkid) # grab the coordinates from tkplot
plot(net, layout=l)
29
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.
For example, here is a quick heatmap of the network matrix:
AOL.com
WashingtonPost.com
NYTimes.com
Reuters.com
Google News
Yahoo News
BBC
ABC
FOX News
MSNBC
CNN
New York Post
LA Times
USA Today
Wall Street Journal
Washington Post
NY Times
AOL.com
WashingtonPost.com
NYTimes.com
Reuters.com
Google News
Yahoo News
BBC
ABC
FOX News
MSNBC
CNN
New York Post
LA Times
USA Today
Wall Street Journal
Washington Post
NY Times
Depending on what properties of the network or its nodes and edges are most important to you,
simple graphs can often be more informative than network maps.
30
dd <- degree.distribution(net, cumulative=T, mode="all")
plot(dd, pch=19, cex=1, col="orange", xlab="Degree", ylab="Cumulative Frequency")
1.0
Cumulative Frequency
0.8
0.6
0.4
0.2
2 4 6 8 10 12 14
Degree
Two-mode or bipartite graphs have two different types of actors and links that go across, but not
within each type. Our second media example is a network of that kind, examining links between
news sources and their consumers. As you will see below, this time the edges of the network are in a
matrix format. We can read those into a graph object using graph.incidence. In igraph, bipartite
networks have an edge attribute called type that is 0 for one group of nodes and 1 for the other.
head(nodes2)
head(links2)
plot(net2, vertex.label=NA)
31
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.
NYT
BBC
LATimes
USAT CNN
MSNBC
WSJ FOX
ABC
WaPo
Igraph also has a special layout for bipartite networks (though it doesnt always work great, and
you might be better off generating your own two-mode layout).
32
plot(net2, vertex.label=NA, vertex.size=7, layout=layout.bipartite)
Tom
Ted
WaPo
Anna Dave
Ed
WSJ Kate ABC
Lisa
Dan
Nancy Jason
USAT FOX
Brian Jo
Sandra
BBC CNN MSNBC
LATimesSheila
John Jim Jill
Paul
NYT Ronda
Mary
In this example, we will also experiment with the use of images as nodes. In order to do this, you
will need the png library (if missing, install with install.packages("png")
# install.packages("png")
library(png)
33
V(net2)$raster <- list(img.1, img.2)[V(net2)$type+1]
By the way, we can also add any image we want to a plot. For example, many network graphs can
be largely improved by a photo of a puppy carrying a basket full of kittens.
34
# The numbers after the image are its coordinates
# The limits of your plotting area are given in par()$usr
It is a good practice to detach packages when we stop needing them. Try to remember that especially
with igraph and the statnet family packages, as bad things tend to happen if you have them
loaded together.
detach(package:png)
detach(package:igraph)
Plotting with the network package is very similar to that with igraph - although the notation is
slightly different (a whole new set of parameter names!). This package also uses less default controls
obtained by modifying the network object, and more explicit parameters in the plotting function.
Here is a quick example using the (by now familiar) media network. We will begin by converting
the data into the network format used by the Statnet family of packages (including network, sna,
ergm, stergm, and others).
As in igraph, we can generate a network object from an edge list, an adjacency matrix, or an
incidence matrix. You can get the specifics with ?edgeset.constructors. Here as in igraph above,
we will use the edge list and the node attribute data frames to create the network object. One
specific thing to pay attention to here is the ignore.eval parameter. It is set to TRUE by default,
and that setting causes the network object to disregard edge weights.
35
library(network)
Here again we can easily access the edges, vertices, and the network matrix:
net3[,]
net3 %n% "net.name" <- "Media Network" # network attribute
net3 %v% "media" # Node attribute
net3 %e% "type" # Node attribute
Note that - as in igraph - the plot returns the node position coordinates. You can use them in other
plots using the coord parameter.
36
detach(package:network)
For a full list of parameters that you can use in the network package, check out ?plot.network.
Interactive D3 Networks in R
These days it is increasingly easier to export R plots to html/javascript output. There are a number
of libraries like rcharts and htmlwidgets that can help you create interactive web charts right
from R. Well take a quick look at networkD3 which - as its name suggests - generates interactive
network visualizations using the D3 javascript library.
One thing to keep in mind is that the visualizations generated by networkD3 are most useful as a
starting point for further work. If you know a little bit of javascript, you can use them as a first
step and tweak the results to get closer to what you want.
If you dont have the networkD3 library, install it now:
install.packages("networkD3")
The data that this library needs from is is in the standard edge list form, with a few little twists.
In order for things to work, the node IDs have to be numeric, and they also have to start from 0.
An easy was to get there is to transform our character IDs to a factor variable, transform that to
numeric, and make sure it starts from zero by subtracting 1.
library(networkD3)
el <- data.frame(from=as.numeric(factor(links$from))-1,
to=as.numeric(factor(links$to))-1 )
The nodes need to be in the same order as the source column in links:
Now we can generate the interactive chart. The Group parameter in it is used to color the nodes.
Nodesize is not (as one might think) the size of the node, but the number of the column in the node
data that should be used for sizing. The charge parameter controls node repulsion (if negative) or
attraction (if positive).
37
Simple Plot Animations in R
If you have already installed ndtv, you should also have a package used by it called animation.
If not, now is a good time to install it with install.packages('animation'). Note that this
package provides a simple technique to create various (not necessarily network-related) animations
in R. It works by generating multiple plots and combining them in an animated GIF.
The catch here is that in order for this to work, you need not only the R package, but also an
additional software called ImageMagick (imagemagick.org). You probably dont want to install that
during the workshop, but you can try it at home.
library(animation)
library(igraph)
We will now generate 4 network plots (the same way we did before), only this time well do it
within the saveGIF command. The animation interval is set with interval, and the movie.name
parameter controls name of the gif.
l <- layout.fruchterman.reingold(net)
38
step.3 <- unlist(neighborhood(net, 2, step.1, mode="out"))
col[setdiff(step.3, step.2)] <- "#FFDD1F"
plot(net, vertex.color=col, layout=l) },
interval = .8, movie.name="network_animation.gif" )
detach(package:igraph)
detach(package:animation
Here we will create somewhat more sophisticated D3 visualizations using the ndtv package. You
should not need additional software to produce web animations with D3. If you want to save the
animations as video files (check out ?saveVideo), you would have to install a video converter called
FFmpeg (http://ffmpg.org). To find out how to get the right installation for your OS, check out
?install.ffmpeg. To use all available layouts, you would also need to have Java installed on your
machine.
install.packages("ndtv", dependencies=T)
As this package is part of the Statnet family, it will accept objects from the network package such
as the one we created earlier.
library(ndtv)
net3
Most of the parameters below are self-explanatory at this point (bg is the background color of the
plot). Two new parameters we havent used before are vertex.tooltip and edge.tooltip. Those
contain the information that we can see when moving the mouse cursor over network elements. Note
that the tooltip parameters accepts html tags for example we will use the line break tag <br>.
The parameter launchBrowser instructs R to open the resulting visualization file (filename) in
the browser.
39
render.d3movie(net3, usearrows = F, displaylabels = F, bg="#111111",
vertex.border="#ffffff", vertex.col = net3 %v% "col",
vertex.cex = (net3 %v% "audience.size")/8,
edge.lwd = (net3 %e% "weight")/3, edge.col = '#55555599',
vertex.tooltip = paste("<b>Name:</b>", (net3 %v% 'media') , "<br>",
"<b>Type:</b>", (net3 %v% 'type.label')),
edge.tooltip = paste("<b>Edge type:</b>", (net3 %e% 'type'), "<br>",
"<b>Edge weight:</b>", (net3 %e% "weight" ) ),
launchBrowser=F, filename="Media-Network.html" )
If you are going to embed the image in a markdown document, use output.mode='inline' above.
Animated visualizations are a good way to show the evolution of a small or medium size network
over time. At present, ndtv is the best R package for that especially since recently it added D3
visualizations to its capabilities.
In order to work with the network animations in ndtv, we need to understand Statnets dynamic
network format, implemented in the networkDynamic package. Lets look at one of the example
data sets included in the package:
data(short.stergm.sim)
short.stergm.sim
head(as.data.frame(short.stergm.sim))
40
## 4 0 1 3 9 FALSE
## 5 2 25 3 9 FALSE
## 6 0 4 3 11 FALSE
What we see here is an edge list. Each edge goes from the node with ID in the tail column to
node with ID in the head column. The edge exists from time point onset to time point terminus.
The idea of onset and terminus censoring refers to start and end points enforced by the beginning
and end of network observation rather than by actual tie formation/dissolution.
We can simply plot the network disregarding its time component (combining all nodes & edges that
were ever present):
plot(short.stergm.sim)
41
Plot nodes & vertices that were active from time 1 to time 5:
Plot all nodes and vertices that were active between time 1 & 10:
render.d3movie(short.stergm.sim,displaylabels=TRUE)
We are now ready to create and animate our own dynamic network. Dynamic network object can be
generated in a number of ways: from a set of networks/matrices representing different time points;
from data frames/matrices with node lists and edge lists indicating when each is active, or when
they switch state. You can check out ?networkDynamic for more information.
We are going to add a time component to our media network example. The code below takes a
0-to-50 time interval and sets the nodes in the network as active throughout (time 0 to 50). The
edges of the network appear one by one, and each one is active from their first a ctivation u ntil time
point 50. We generate this longitudinal network using networkDynamic with our node times as
node.spells and edge times as edge.spells.
42
vs <- data.frame(onset=0, terminus=50, vertex.id=1:17)
es <- data.frame(onset=1:49, terminus=50,
head=as.matrix(net3, matrix.type="edgelist")[,1],
tail=as.matrix(net3, matrix.type="edgelist")[,2])
If we try to just plot the networkDynamic network, what we get is a combined network for the
entire time period under observation or as it happens, our original media example.
One way to show the network evolution is through static images from different time points. While
we can generate those one by one as we did above, ndtv offers an easier way. The command to do
that is filmstrip. As in the par() function controlling base R plot parameters, here mfrowsets the
number of rows and columns in the multi-plot grid.
We can pre-compute the animation coordinates (otherwise they get calculated when you generate
the animation). Here animation.mode is the layout algorithm - one of kamadakawai, MDSJ,
Graphvizand useAttribute (user-generated coordinates).
render.d3movie(net3.dyn, usearrows = F,
displaylabels = F, label=net3 %v% "media",
bg="#ffffff", vertex.border="#333333",
vertex.cex = degree(net3)/2,
vertex.col = net3.dyn %v% "col",
edge.lwd = (net3.dyn %e% "weight")/3,
edge.col = '#55555599',
43
vertex.tooltip = paste("<b>Name:</b>", (net3.dyn %v% "media") , "<br>",
"<b>Type:</b>", (net3.dyn %v% "type.label")),
edge.tooltip = paste("<b>Edge type:</b>", (net3.dyn %e% "type"), "<br>",
"<b>Edge weight:</b>", (net3.dyn %e% "weight" ) ),
launchBrowser=T, filename="Media-Network-Dynamic.html",
render.par=list(tween.frames = 30, show.time = F),
plot.par=list(mar=c(0,0,0,0)), output.mode='inline' )
render.d3movie(net3.dyn, usearrows = F,
displaylabels = F, label=net3 %v% "media",
bg="#000000", vertex.border="#dddddd",
vertex.cex = function(slice){ degree(slice)/2.5 },
vertex.col = net3.dyn %v% "col",
edge.lwd = (net3.dyn %e% "weight")/3,
edge.col = '#55555599',
vertex.tooltip = paste("<b>Name:</b>", (net3.dyn %v% "media") , "<br>",
"<b>Type:</b>", (net3.dyn %v% "type.label")),
44
edge.tooltip = paste("<b>Edge type:</b>", (net3.dyn %e% "type"), "<br>",
"<b>Edge weight:</b>", (net3.dyn %e% "weight" ) ),
launchBrowser=T, filename="Media-Network-even-more-Dynamic.html",
render.par=list(tween.frames = 15, show.time = F), output.mode='inline',
slice.par=list(start=0, end=50, interval=4, aggregate.dur=4, rule='any'))
45