Spatial Social Networks (SSN) Visualization and Metrics with R
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 pakcage 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. In fact, many of our codes in this tutorial are inspired by this book.
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 will be downloaded from Friendly Cities Lab GitHub page.
igraph package to calculate network metrics,
sf package to create geospatial objects from dataframe,
tmap package to create both static and interactive maps,
tmaptools package to extract OpenStreetMap basemaps, and
tidyverse package as the syntax to process data. You can download the packages using the following codes:
install.packages("igraph") #for network analysis install.packages("sf") #for spatial objects operation install.packages("tmap") #for mapping install.packages("tmaptools") #for using read.osm to extract basemaps install.packages("stars") #for using Mapbox to extract basemaps install.packages("tidyverse") #for data processing install.packages('tigris') #for loading TIGRR shapefiles install.packages("devtools") #to download R package on GitHub ::install_github("friendlycities-gatech/SSNtools")devtools
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.
ggpplot2 has powerful mapping functions. It is intuitive for people who are used to the underlying logic of ggplot2. One advantage of
ggpplot2 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.
cartography has limited mapping functions, but can come in handy when you only need to draw a few simple types of maps. It has some functions that are very friendly for mapping spatial networks, such as
getLinkLayer that takes in point shapefiles and edge tables and return edge shapefiles. You can also overlay proportional symbols and proportional links layers. However, you may not get to control the details of the maps in the most desirable ways.
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. The interaction component is operationalized through
Leaflet package. To draw basemaps with
tmap, you can either use the
read_osm function in
tmaptools or use package
stars to wrap around the raster downloaded through Mapbox API. For
read_osm to work, you will need to install
rJava for the function to run, which may further require downloading Java and Java Development kit. You can see Chapter X for examples to add basemaps.
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
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. We will show the workflow from
tmap with examples in Chapter X.
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.
SSNtools is an R package that provides metrics for analyzing and visualizing spatial social networks.