Beautiful Mapping with R — Part 1

Maggie Wanjiru
3 min readJul 7, 2019

--

I was once sent a zipped folder named shapefiles Geo and I honestly didn’t know what to do with it. As we all do, I first downloaded the folder and saved them on my desktop. I then unzipped the folder and on opening it, there were 3 Microsoft Excel CSV files, a text document, DBF file, PRJ file, SBN file, SBX file, SHP file, XML document, SHX file and PNG file.

The files frightened me. It was my first encounter with shapefiles and I must admit that after merely looking at these file names, I closed the folder and never opened it, well, until one day I realized mapping in R was an essential skill.

So, what are shapefiles?

Definition;

According to Wikipedia, (https://en.wikipedia.org/wiki/Shapefile),

The shapefile format is a popular geospatial vector data format for geographic information system (GIS) software. It is developed and regulated by Esri as a (mostly) open specification for data interoperability among Esri and other GIS software products. The shapefile format can spatially describe vector features: points, lines, and polygons, representing, for example, water wells, rivers, and lakes. Each item usually has attributes that describe it, such as name or temperature.

The file names;

Source: https://en.wikipedia.org/wiki/Shapefile

The three mandatory files have filename extensions .shp, .shx and .dbf. The actual shapefile relates specifically to the .shp file but alone is incomplete for distribution as the other supporting files are required.

Mandatory files;

.shp — shape format; the feature geometry itself
.shx — shape index format; a positional index of the feature geometry to allow seeking forwards and backwards quickly
.dbf — attribute format; columnar attributes for each shape, in dBase IV format

Other files;

.prj — projection description, using a well-known text representation of coordinate reference systems
.sbn and .sbx — a spatial index of the features
.ain and .aih — an attribute index of the active fields in a table
.ixs — a geocoding index for read-write datasets
.mxs — a geocoding index for read-write datasets (ODB format)
.atx — an attribute index for the .dbf file in the form of shapefile.columnname.atx (ArcGIS 8 and later)
.shp.xml — geospatial metadata in XML format, such as ISO 19115 or other XML schemas

In each of the .shp, .shx, and .dbf files, the shapes in each file correspond to each other in sequence (i.e., the first record in the .shp file corresponds to the first record in the .shxand .dbf files, etc.)

Loading the shapefiles in R;

Source: https://mgimond.github.io/Spatial/data-manipulation-in-r.html

There are two different ways one can load a shapefile into R: using the rgdal package and using the maptools package. The difference between both methods is that the rgdal option loads the coordinate system (CS) information (if present in the shapefile) while maptools does not.

The function readOGR() from rgdal is used to load the shapefile.

You’ll also notice the fortify function whose purpose is to convert a curves and points object to a data frame for ggplot2.

A Basic Plot;

# #loading the libraries -------------------------------------------library(rgdal)
library(ggplot2)
library(dplyr)
# #loading the shapefiles from the desktop -------------------------kenya_mapping<- readOGR(dsn="C:/Users/MARGRET/Desktop/shapefilesGEO/shapefilesGEO", stringsAsFactors = FALSE)
kenya_mapping_df <- fortify(kenya_mapping)

map <- ggplot(data = kenya_mapping_df, aes(x = long, y = lat, group = group))
map + geom_path() #this code plots the map

In case you prefer to have the map without the lat (latitude) and long (longitude) labels run the code below;

map + 
geom_path()+ #this code plots the map
xlab("")+ #this code removes the labels and replaces it with an empty space
ylab("")

For a colourful map,

# #Adding some color to the map ------------------------------------map + 
geom_polygon(aes(fill = id)) +
coord_fixed(1.3) +
guides(fill = FALSE)

There’s so much more that can be done with maps. Stay tuned!

--

--

Maggie Wanjiru

Data storyteller | Nature Lover | Aspiring photographer