Let’s draw some polygons!

2 Toronto Header

KEY QUESTION: Given an Esri Shapefile, how can I visualize the coordinates as polygons?

DESIRED OUTPUT: A map of polygons

CATEGORY OF ANALYSIS: Geospatial analysis, data visualization


ESTIMATED SCRIPT TIME: less than 4 hours

DATA SOURCE: City of Toronto, City Wards, May 2010 (MTM 3 Degree Zone 10, NAD27)

CONTEXT AND DESCRIPTION: You have an Esri shapefile, which usually represents boundaries of some sort. Maybe later you want to see which points lie inside or outside of the polygons. Maybe later you want to make a chloropleth / heat map.

Before doing any of that, we need to first plot the boundaries. In this example, we use the City of Toronto’s open dataset of city wards. We plot the coordinates as polygons to see what the boundaries look like.

VALUE ADDED: This particular script and this particular data set are both very simple, and do not lend themselves to a lot of analysis. This is however the foundational script and building block for more complex analyses to be explored later on. Coupled with other data sets and additional functions, a lot more powerful and insightful analyses can be made later on.

In general, geospatial analysis offers many benefits, including a basic plot of boundaries. Many technical and non-technical people can interpret and digest the information on a map very easily. Using polygons can provide many insights. Maybe you have a boundary polygon and you have a list of addresses, and you need to see which addresses lie inside or outside of the boundary of interest. If you make a chloropleth/heat map, you can see the distribution and the change in your polygon in a very colourful manner.

METHODOLOGY: Using R, I programmed a script using the ggplot2 and rgdal libraries. The key ideas are to transform the shapefile in a useable dataframe and then use ggplot and geom_polygon functions to visualize the boundaries.

Here is the script:

Figure 1: R script of geom_polygon script

R script polygon

And voila! Here is one of the outputted maps:

Figure 2: Map of Toronto’s Ward Boundaries


Again, this is a very basic example of geospatial analysis: it plots boundary lines on a map. This R library package offers many features. You can play around with colours, dimensions, shapes, transparencies, legends, etc. Heat maps or “choropleth maps” are also very interesting to see as they as show gradients of concentrations.

What are your thoughts? Do you find business value in this kind of visualization? Is the R script easy-to-understand? What can be improved? Leave a comment!


SOURCES: Here is a list of sources I used for help and inspiration. They’re great for more detail and additional reading.