UCLA Academic Technology Services HomeServicesClassesContactJobs

R FAQ
How can I manage and plot social network data?

Plotting social network data can be easily done with the igraph package in R. This page will demonstrate some basic data management steps for social network data and provide the commands for creating a social network plot.  To install the igraph package, type "install.packages("igraph")" in your R command line.

We will start with an adjacency table, mat25.txt.

library(igraph)
x<-read.table("http://www.ats.ucla.edu/stat/r/faq/mat25.txt", header=FALSE)

In order for the igraph package to recognize this table as a network, we can first convert it to a matrix.  Then, if we wish to calculate graph-related statistics on it (betweenness, closeness, degree), we can use the matrix to create a graph object.

network = as.matrix(x)
g1 = graph.adjacency(network)

# compute betweenness
b1 = betweenness(g1, directed = FALSE)
b1
 [1] 12.510317  4.108730 10.409127  4.920238
 [5] 11.346032 12.488889  1.834524 14.577381
 [9]  6.052381  6.901190  4.176190 10.283333
[13]  7.496032  9.330556  2.146825  4.065873
[17]  1.069048  4.217460  4.419841  9.076984
[21] 10.155159  9.407143  4.019048 12.067460
[25]  9.920238

# compute closeness
c1 = closeness(g1, mode="out")
c1
 [1] 0.3529412 0.3380282 0.3243243 0.3380282
 [5] 0.3428571 0.3380282 0.3333333 0.3380282
 [9] 0.3333333 0.3333333 0.3380282 0.3333333
[13] 0.3428571 0.3333333 0.3380282 0.3428571
[17] 0.4897959 0.3478261 0.3333333 0.3478261
[21] 0.3428571 0.3428571 0.3478261 0.3478261
[25] 0.3287671

# compute degree
d1 = degree(g1, mode="out")
 [1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
[23] 5 5 5

We have shown above how you can read an adjacency matrix as a table.  The igraph package also allows you to read in a dataset in which you list the edges of a network.  The file elist.txt lists six edges:

1 2
1 3
1 4
3 5
4 6
6 4

We can read in this file as a graph, indicating that the format is an "edgelist".

xlist<-read.graph("http://www.ats.ucla.edu/stat/r/faq/elist1.txt", format="edgelist")
xlist

Vertices: 7 
Edges: 6 
Directed: TRUE 
Edges:
          
[0] 1 -> 2
[1] 1 -> 3
[2] 1 -> 4
[3] 3 -> 5
[4] 4 -> 6
[5] 6 -> 4

Looking at the summary of our graph object, R believes our graph has 7 vertices although we only listed edges ranging from vertices 1 through 6. R makes a few assumptions unless otherwise specified:

We can amend our read.graph command to indicate that our graph contains 8 vertices and that the edges are undirected. We can then graph both and see the differences in nodes and edge representations.  Note that the plotting algorithm used does not generate the same plot every time.  In fact, it sometimes generates plots in which vertices are crowded or overlapping or edges are difficult to see.  When this occurs, rerun the plot command to get a new and likely better representation of your network. 

xlist.8un<-read.graph("http://www.ats.ucla.edu/stat/r/faq/elist1.txt", format="edgelist", n = 8, directed = FALSE)

plot.igraph(xlist)

plot.igraph(xlist.8un)

Our first graph has an unconnected 0 vertex and arrows on the edges. Our second has unconnected 0 and 7 vertices and no arrows on the edges.  We could also enter our data in a single vector of vertex indices where an edge connects the first and second, third and fourth, fifth and sixth entries and so on. 

g2 <- graph(c(0, 1, 1, 2, 1, 3, 1, 4, 3, 5, 4, 6, 6, 4))
g2

Vertices: 7 
Edges: 7 
Directed: TRUE 
Edges:
          
[0] 0 -> 1
[1] 1 -> 2
[2] 1 -> 3
[3] 1 -> 4
[4] 3 -> 5
[5] 4 -> 6
[6] 6 -> 4

plot.igraph(g2)

For more information on the igraph package, see the igraph package R-project page or the igraph library page.


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.