Every commodity trading and risk professional understands the usefulness of maps during daily work. Whether used to keep track of infrastructure or trade flows, hardly a day goes by without looking at maps.

The one hurdle in most of our analysis is making fit-for-purpose maps. Most of us are reliant on generic maps from the interent or company/industry reports.

Using R, the commodity professional can now bring the map to his/her own desktop. Allowing the data analysis they already conduct to be integrated into maps that may contain trading hubs, producing basins, and pipelines.

This brief introduction will show the reader how to load data from the EIA website and create an infrastrcture map on their desktop.

The first step will be going to the EIA website and downloading the shapefiles that we will need. Shapefiles can be created from scratch, but for our purposes, we likely will not do better than the files provided by the EIA. These files contain latitude, longitude, and other data points that allow us to plot shapes (lines, polygons, points) onto an open source map in R.

Download the EIA shapefiles for “Natural Gas Interstate and Intrastate Pipelines” and “Natural Gas Market Hubs”. Then set your working directory in R to the directory these files are saved to and require leaflet, magrittr, rgdal, and knitr. These four packages are all we need to create maps in R.

require(leaflet)
require(magrittr)
require(rgdal)
require(knitr)

The next step is to use readOGR (from rgdal) to create the data frames that leaflet will use for mapping.

pipes<-readOGR("<your home directory>","NaturalGas_InterIntrastate_Pipelines_US")
hubs<-readOGR("<your home directory>","NaturalGas_MarketHubs_US")

The last thing needed to create this map is the co-ordinates for the natural gas hubs. Use ogrInfo to see the data contained within the hubs data frame.

ogrInfo("<you home directory>","NaturalGas_MarketHubs_US")

Longitude, latitude, and operator are the fields that will map and properly idenitify the gas hubs.

Entering the following command will create and plot a map called NG.map. The “pipes” data frame is referenced in addPolylines, the “hubs” data frame is used in addMarkers, and within both of these commands a “group” is idenitfied to add layers to the map.

NG.map <- leaflet() %>%
  addTiles() %>%
  addPolylines(data = pipes, color ="red",group = "NG Pipelines") %>%
  addMarkers(data = hubs, ~Longitude,~Latitude,popup = ~as.character(Operator), group = "NG Hubs") %>%
  addLayersControl(overlayGroups = c("NG Pipelines","NG Hubs"))
NG.map

You should now be able to successfully plot a map within R (I personally find the maps display better when exported to a browser). What we have created today is very basic and not particularly useful, but it provides the bare bones for a much more valuable tool within your commodity business.