Spatial Social Networks (SSN) Visualization and Metrics with R
2023-05-01
Chapter 1 Introduction
This tutorial is dedicated to describe the basic concepts, methods, and visualizations involved to map and analyze non-planar networks (social networks & relationships) in geographic space using R. A non-planar network is a network in which the nodes are geolocated, while the edges can cross over each other without creating new nodes. For example, mapping Facebook friendships and flight routes in geographic space are non-planar. A planar network is spatially embedded in a way that the edges intersect only at the endpoints, such as the road, river, and eletrical grid networks. There are already some tutorials for mapping and analyzing planar networks, but none exists for non-planar networks.
We assume our audience has basic knowledge of R programming with tidyverse
syntax and basic GIS concepts (e.g., basic geometry types, coordinate system projection, spatial operation etc.). If not, you can use this book Geocomputation with R to get started with the basic concepts. The book also leverages tmap
at map visualization.
Mapping relationship in geographic space has its own challenges, which is described thoroughly in Andris’s paper Challenges for Social Flows. In our visualization examples, we will try to show the best practices to map social networks in space that can counter some of these problems.
All the data you need to complete the tutorial can be loaded from SSNtools
R package, which is developed together with this tutorial.
SSNtools R package
This tutorial comes with an R package SSNtools
(see R package GitHub), which provides metrics for analyzing and visualizing spatial social networks. It is implemented with base R syntax. Download the R package using the following lines below in your R console.
#to download R package on GitHub
install.packages("devtools")
#install development version of our SSNtools R package for advanced SSN metrics.
::install_github("friendlycities-gatech/SSNtools") devtools
This SSNtools
also include the following sample datasets. You can directly use them by calling their names, or explicitly call them at the beginning of the scripts like the following:
# Mafia spatial social networks; undirected and unweighted
data(MafiaNodes)
data(MafiaEdges)
# Mafia spatial social networks, limited to NYC
data(NYCMafiaNodes)
data(NYCMafiaEdges)
# Mafia spatial social networks, limited to Detroit
data(DTMafiaNodes)
data(DTMafiaEdges)
# Census block groups to restaurant POIs; bipartite and weighted
data(POINodes)
data(POIEdges)
# Emergency responders, emergency locations, and their teammates; ego-centric network
data(EmergencyNodes)
data(EmergencyEdges)
Other required packages
We use the following packages in the tutorial to wrangle data, calculate network metrics, and visualize statistical graphs.
install.packages("igraph") #for network analysis
install.packages("sf") #for spatial objects operation
install.packages("tmap") #for interactive and static maps
install.packages("basemaps") #for retrieving basemaps for tmap
install.packages("tidyverse") #for data processing
install.packages('tigris') #for loading TIGER shapefiles
install.packages('stplanr') #for using od2line function to convert points to lines
install.packages('ggplot2') #for visualizing statistical graphics
There are a few packages for visualizing maps. We chose tmap
in the end for its flexibility to switch between interactive maps and static plots. We also have some codes that use ggplot2
to generate other graphic visualizations.
ggplot2
has powerful mapping functions. It is intuitive for people who are used to the underlying logic of ggplot2. One advantage of ggplot2
is that it can easily add high-resolution Google Maps as basemaps using ggmap
package (only in WGS84 projection). Here are some good tutorials that teach you how to use ggplot2 to map planar networks and produce basic elements of a map. However, you need to convert spatial objects to dataframe for ggpplot2
to draw on.
tmap
is equally powerful as ggpplot2
, and has the extra function to switch between a static plot and interactive plot mode. This feature is useful when you want to zoom in and out to explore your data.
sf
converts data frame into spatial objects that can be used in base R plot function and tmap
. It can also conduct various spatial operation, such as coordinate system projection, spatial joins, and topological relations. A brief introduction of how to streamline sf
and tmap
is given in the book Geocomputation with R.
igraph
is the go-to package for network analysis, such as calculating the centrality measures of a network. These measures may become the attributes for nodes or edges in the maps (see Chapter 3).
tidyverse
provides a set of packages that share the same data representation and use the pip symbol %>%
to connect steps of processing.
tigris
download geographic boundary shapefiles from TIGER website.
stplanr
is an R package that analyze planar spatial network. We mostly use the od2line()
function in the package to convert points to lines.
Download R codes from the tutorial
The example R codes in this tutorial can be downloaded here. To download a specific R script, you can:
- click a specific R script
- go to Raw
- copy and paste the codes in your R studio OR right click -> Save as -> delete .txt in file extension name (so the file name should be name.R)