Network-on-a-map in R

A conversation on the SOCNET mailing list made me think that it might be worth writing up a quick illustration of how to do this in R with the network library. There are a 3 steps to a really basic geographic network plot:

  • Get relational data with appropriate lat and long coordinates for vertices (the hard step!)
  • Tell one of the R mapping libraries to plot a map
  • Tell the network library to plot the network with the lat & long coordinates, without first erasing the map

First we load the libraries

library(network)  # basic 'statnet' network library
library(maps)     # basic R map plotting library

Usually the hardest part of working with geographic data is doing the “geocoding” to assign latitude and longitude to data points (vertices). For this example, we can grab a dataset, courtesy of Katherine Ognyanova, that has already been geocoded from This is a dataset of co-occurrences of country names in statements by U.S. Congressmembers.

The first file contains node-level data about all all of the countries, including the latitude and longitude data we will use

# download and parse CSV files
names(rawnodes) # print the column names
##  [1] "ID"        "Dem_freq"  "Rep_freq"  "Tot_freq"  "lat"      
##  [6] "lon"       "Tot_freqn" "Dem_freqn" "Rep_freqn" "Dif_freq" 
## [11] "Dif_freqn"

The second file contains the pairs of country-country names and information on the number of term co-occurrences. We will use this to create the edgelist.

names(rawedges) # print the column names
##  [1] "Source"    "Target"    "Dem_cooc"  "Rep_cooc"  "Tot_cooc" 
##  [6] "Rep_coocn" "Dem_coocn" "Tot_coocn" ""    ""   
## [11] "lat.from"  "lon.from"
nrow(rawedges) # how many edges are there?
## [1] 11789

Because there are a lot of edges in this network, if we plot the whole thing it will be visually unintelligible. So we can filter on the Tot_cooc variable to only include relationships between terms that have a total coo-occurrence of more than 500 times.

# subset to form a smaller matrix with only strong ties
Source Target Tot_cooc
40 afghanistan china 940
55 afghanistan israel 757
72 afghanistan pakistan 2286
81 afghanistan libya 763
82 afghanistan vietnam 985
115 afghanistan russia 610
## [1] 80

80 relationships seems a lot more workable, so lets convert the edgelist into a network, bringing along the total co-occurrence variable as an edge value attribute.

                 directed=FALSE,  # this will be an undirected network
                 ignore.eval=FALSE,  # confusingly, this tells it to include edge weights
                 names.eval='Tot_cooc'  # names for the edge weights

Because we reduced the set of relationships, we also reduced the set of vertices from the full list of countries. So we now need to match up the coordinates in the rawnodes file with the right vertices. For convince, we attach the lat and lon to the network as vertex attributes.

# attach the appropriate lat and long coordinates
# need to subset to the vertices actually in the network


Now we can actually do the plotting of the world map with the map() command, followed by the command. Note the the new=FALSE argument that tells it not to erase the map before drawing the network. The coordinates are passed in via the coord= argument as a two-column matrix.

# plot the map for the background

# plot the network using the geo coordinates,  # pass in the network
             # don't erase the map before drawing the network
             # get coordiantes from vertices and pass in as 2-col matrix
 # ---- all the rest of these are optional to make it look nice ------
             # set a semi-transparent edge color
             # specifiy an edge width scaled as fraction of total co-occurence
             # set the vertex size
             # set a semi transparent vertex color
             # please don't jitter the points around

plot of chunk unnamed-chunk-7

For this network we see that a lot of the ties involved the US. Not surprising, but may not be the most interesting feature of the data. So might be worthwhile to remove the USA vertex and lower the filtering threshold … but this post is supposed to be just about the mechanics of overlaying a network on a map.

However, it is often the case that geographic positioning of networks doesn't do a great job of revealing the network structure. The map is essentially providing geographic context and implicit labeling, but if the underlying data is not drivien by geographic proximity important details can be obscured. For this example, I think it would be more intepretable as traditional network diagram where vertex positions are determined by network proximity.

# but it is actually far more useful as a regular netowrk,displaylabels=TRUE,boxed.labels=TRUE,

plot of chunk unnamed-chunk-8

Katherine Ognyanova also collected a number of posts on how to draw Great Circle curves on maps in R. These may help a little bit with edge overlap, but to use them you will need to roll-you-own network rendering code. This one is from Flowing Data: There are of course lots of cases where you might want to do more sophisticated things with map projections. But for a quick-and-dirty plot, network seems to do pretty well. Of course I'd love to do the animated version of this with the ntdv package, so please contact me if you know of an appropriate dynamic dataset with geographic coordinates.

P.S. I was able to write this in rmarkdown and have it post directly to the blog using the RWordPress package!

4 thoughts on “Network-on-a-map in R”

  1. When I used the network function, R gave me an error message below. What is the problem??

    + matrix.type='edgelist',
    + directed=FALSE, # this will be an undirected network
    + ignore.eval=FALSE, # confusingly, this tells it to include edge weights
    + names.eval='Tot_cooc' # names for the edge weights
    + )
    Error in .Call(setVertexAttribute_R, x, attrname, value, v) :
    NULL value passed as symbol address

  2. This has been very helpful. I am, however, having some difficulty attempting to follow the same steps with my data. I have a network of 79 edges with 50 vertices. My network is a simple list of 79 rows (the edges) with two columns of one vertex (first column) connected to the second vertex (second column).

    Along with that, I also have an attribute table that contains the longitude and latitude of each vertex.

    I am now trying out this portion of your code:, new=FALSE, coord=cbind(coocNet%v%’lon’,coocNet%v%’lat’))

    I am a little lost with this part: coord=cbind(coocNet%v%’lon’,coocNet%v%’lat’).

    How would I apply this with my data? Again, I have two tables, one containing the edgelist and the other containing the attributes, including the longitude and latitude of each vertex.


  3. Can’t find the network library.
    > library(network)
    Error in library(network) : there is no package called ‘network’

Leave a Reply

Your email address will not be published. Required fields are marked *