I wanted to write about how quick and easy it is to make a simple map in R. However, it dawned on me that the first time I tried to make a map in R it actually took a long time. It’s not like it was a hard map to make, it would have taken a GIS analyst 5 minutes in a desktop GIS such as QGIS, ArcGIS, or MapInfo. What took so long was piecing together all the information from the internet to get the Jupyter environment to install the packages, then actually run the simple code without error. I’ve written this blog to help new users to R and Jupyter create their first map in 4 easy steps.


Why use R?

Before we get started let’s discuss the elephant in the room. What is the point? Why would any GIS Analyst use R to create maps? It’s much quicker to load your shape files, join it to a CSV with some data and start mapping away. Most of the time spent (approx. 80% of project time) is dedicated in collecting, cleaning, processing, cleaning (yes, twice) and loading the data just to visualise it.

Now think about the amount of data being generated a day. Did you know that 90% of the world’s data has been created in the last two years at a rate of 2.5 quintillion bytes of data a day? Quantum Spatial Inc. (QSI) has reported that as of 2018 they manage 12 petabytes of data and are adding 1 petabyte of data each year on average. As technology continues to evolve, businesses would want to ensure repetitive data analysis models can be migrated across platforms. R, being one of the popular choices in programming language for data science is a language that could assist the geospatial world in creating statistic data models, thus allowing for ease of migration and support business geospatial data scaling. The point of R is not to make beautiful cartographic maps, it’s to wrangle, process, explore, create statistical models and visualise the data. The outputs enable businesses to easily gain insights from their data.

What are the advantages of R?

Before you scroll down to the steps, here is a list of the advantages of using R for geospatial purposes:

  1. R is open source
  2. R is reproducible
  3. Code can be shared that leads to faster development and collaborative workflows
  4. Being a programming language, it has great flexibility
  5. The huge ecosystem of statistical libraries can be utilised for spatial statistics

Worked Example: Visualising Local Government Areas (LGAs) in R in 4 Steps

Environment: Jupyter Notebook through Anaconda

Step 1: Install packages

Assumptions

When you install packages, it is assumed that you have administrator rights on your computer and that you have devtools installed. If you don’t have devtools installed you can do the following:

  1. In Anaconda Navigtor, go to your Environments tab
  2. Select the environment you want to install it on
  3. Search for devtools
  4. Click on the box
  5. Hit apply

Run command

You can download all the packages you need by running the command:

The command will install a few packages. Most of the packages you will not need for this exercise. However, the packages will be handy when you want to deep dive into geocomputation with R.

The packages used for this exercise are:

  1. rgdal: assist you in reading shape files
  2. leaflet: provides an interactive map background
  3. IRdisplay: This will allow you to imbed your html into the Jupyter Notebook to preview the map.

Step 2: Load shapefile

Download your shape file and unzip it into a folder. It is best if you are starting to have the shape files in the same folder as your Jupyter Notebook. The data used in this tutorial is from: data.vic.gov.au. We have not supplied you with the data for this exercise.

To open a shape file in your kernel:

  1. Load the rgdal library
  2. Assign the shape file to a variable. In the image below, we have used the variable LGA.

Step 3: Make base map & add shp file

You will need to create a base map using leaflet to place your shape file onto.

  1. Load the leaflet and IRdisplay libraries

IRdisplay allows you to download the maps as a html and display it in the notebook.

  1. Start the basemap by assigning the leaflet to a variable (map) and set the view. You can get the lat long from google maps.
  2. You load the leaflet tiles by using the default command: addtiles() %>%.

In this example, we are adding tiles from cartoDB and Esri.

The group command allows you to assign the tile to a group so you can turn on and off.

  1. You can add controls to change the base map by using the addLayersControl command.
  2. Add your LGA layer to the map by using addpolygons().
  3. color command will change the colour of the polygon and popup command will allow you to create a popup from a field in the shape file. In this example, we are taking the values from the field name LGA_NAME. You will need to have a tilde( ~) before the field name for this to work.

Step 4: Display the map & save as HTML

The last thing to do it visualize your map.

The htmlwidgets command allows you to save your widget into the same folder as the notebook.

display_html will display the map in the kernel for you.


We hope you enjoyed our first Geospatial and R insights blog. You can download the jupyter notebook to get you started.

For more information on how you can manage corporate data, please contact Spatial Vision at info@spatialvision.com.au

Nhung Seidensticker

Latest posts by Nhung Seidensticker (see all)