Publication ready map with ggplot2: Case of pearl millet production in Senegal
0.1 Data source
The data used for this tutorial are available via this link by opening it a new tab.
0.2 Note
For any country, the shapefile can be downloaded here
This tutorial is based on Zoclanclounon et al. (2019) paper.
Make sure you’ve installed all requiered packages.
0.3 Code
0.3.1 Clean the R environment workspace
rm(list = ls())0.3.2 Library loading
library(rgdal)
library(mapdata)
library(mapproj)
library(maps)
library(ggplot2)
library(ggrepel)
library(legendMap)
library(dplyr)
library(scales)
library(ggmap)0.3.3 Set working directory
setwd("C:/Users/ANGE/Documents/R MAP")0.3.4 Set shapefile path
mySHP = "C:/Users/ANGE/Documents/R MAP"0.3.5 Import the shapefile
myFile = readOGR(mySHP, layer = "SEN_adm1", stringsAsFactors = FALSE)OGR data source with driver: ESRI Shapefile
Source: "C:\Users\ANGE\Documents\R MAP", layer: "SEN_adm1"
with 14 features
It has 9 fields
Integer64 fields read as strings: ID_0 ID_1
0.3.6 Change in dataframe format for ggplot2
myDF = fortify(myFile, region = "NAME_1")0.3.7 Change long to Longitude and lat to Latitude
myDF = rename(myDF, Longitude = long, Latitude = lat)0.3.8 Overview of the data myDF
| Longitude | Latitude | order | hole | piece | id | group |
|---|---|---|---|---|---|---|
| -17.16056 | 14.89375 | 1 | FALSE | 1 | Dakar | Dakar.1 |
| -17.16004 | 14.89333 | 2 | FALSE | 1 | Dakar | Dakar.1 |
| -17.16000 | 14.89335 | 3 | FALSE | 1 | Dakar | Dakar.1 |
| -17.15683 | 14.89042 | 4 | FALSE | 1 | Dakar | Dakar.1 |
0.3.9 Import the data we want to plot on the map
mydata = read.csv("dta2.csv", header = TRUE, sep = ";")mydata1 = read.csv("data1.csv", header = TRUE, sep = ";")0.3.10 Overview of the data mydata
Type:head(mydata, 4)
| long | lat | id | Production |
|---|---|---|---|
| -17.33 | 14.75 | Dakar | 0 |
| -16.25 | 14.75 | Diourbel | 46231 |
| -16.53 | 14.36 | Fatick | 80000 |
| -12.18 | 12.80 | Kédougou | 152 |
0.3.11 Overview of the data mydata1
Type: head(mydata1, 4)
| NAME_1 | Production | Unity | Prod | long | lat | Produc | Prodd |
|---|---|---|---|---|---|---|---|
| DAKAR | 0 | - | ( 0 ton) | -17.33 | 14.75 | DAKAR ( 0 ton) | DAKAR ( 0 ton) |
| DIOURBEL | 46231 | tons | 46231 tons | -16.25 | 14.75 | DIOURBEL 46231 tons | DIOURBEL (46231 tons) |
| FATICK | 80000 | tons | 80000 tons | -16.53 | 14.36 | FATICK 80000 tons | FATICK (80000 tons) |
| KAOLACK | 77613 | tons | 77613 tons | -16.00 | 14.00 | KAOLACK 77613 tons | KAOLACK (77613 tons) |
0.3.12 Join the data and the shapefle
plotData <- left_join(myDF, mydata)0.3.13 Overview of plotData
| Longitude | Latitude | order | hole | piece | id | group | long | lat | Production |
|---|---|---|---|---|---|---|---|---|---|
| -17.16056 | 14.89375 | 1 | FALSE | 1 | Dakar | Dakar.1 | -17.33 | 14.75 | 0 |
| -17.16004 | 14.89333 | 2 | FALSE | 1 | Dakar | Dakar.1 | -17.33 | 14.75 | 0 |
| -17.16000 | 14.89335 | 3 | FALSE | 1 | Dakar | Dakar.1 | -17.33 | 14.75 | 0 |
| -17.15683 | 14.89042 | 4 | FALSE | 1 | Dakar | Dakar.1 | -17.33 | 14.75 | 0 |
0.3.14 Make the plot
p <- ggplot() + geom_polygon(data = plotData, aes(x = Longitude, y = Latitude,
group = group, fill = Production), color = "black", size = 0.25) + coord_map() +
scale_fill_distiller(palette = "Greens", direction = 1) + geom_point(data = mydata1,
aes(x = long, y = lat), shape = 21, fill = "white", size = 3, color = "black") +
geom_label_repel(data = mydata1, aes(x = long, y = lat, label = NAME_1),
fontface = "bold", color = "black", box.padding = 0.35, point.padding = 0.5,
segment.color = "grey10") + theme_minimal() + theme(panel.grid.major = element_line(colour = "black",
size = 0.5, linetype = "dotted")) + theme(plot.background = element_rect(colour = "white",
size = 1)) + ggtitle("Map of Pearl Millet Production in Senegal (Rainy season 2017)")0.3.15 Map rendering
Just call the map variable p
That is it!