Making Maps of Tunisian Governorate with R

In this page I explain how to make maps of the delegations of Tunisian governorate with R Studio. I used Professor Malouche’s guide on “Map of Tunisia with R” as an initial reference for the project. I start by loading the required packages:

library(readxl)
library(tigris)
library(dplyr)
library(leaflet)
library(sp)
library(ggmap)
library(maptools)
library(broom)
library(httr)
library(scales)
library(rgdal)
library(tidyverse)

From this address I choose Tunisia – R sp- level2 option to download the Database of Global Administrative Areas (GADM) for Tunisia for delegations. I save the file name as “tunisia”, and install it to my workflow. Then I use the “fortify” command to extract the latitude and longitude coordinates.

tunisia<- readRDS("tunisia.rds") 
tun_for <- fortify(tunisia) 
ggplot(tun_for) + geom_polygon(aes(x = long, y = lat,group = group),color = "white",fill = "red") +
theme_void() + coord_fixed()

I match the coordinates [long, lat] in “tun_for” file with the delegations names in “tunisia” file [NAME_2].

tunisia@data$id<-rownames(tunisia@data)
tunisia@data$id<-as.numeric(tunisia@data$id)
tun_for$id<-as.numeric(tun_for$id)
tun_for$id<-tun_for$id-1
match <- left_join(tun_for, tunisia@data, by = "id")
unique(match$NAME_1)

As I conduct the analysis on Sidi Bouzid, I filter the coordinates of delegations within Sidi Bouzid:

sidi_for<-filter(match, grepl("Sidi Bou Zid", NAME_1))
head(sidi_for)

For this project I focus on the distribution of deep weels within Sidi Bouzid. I collected the data from this source (page: 20). A copy of the values can be downloaded below:

I merge the weel data with the “sidi_for” data that includes coordinates of delegations.

sidi_weels <- read_excel("sidi_weels.xlsx")
names(sidi_weels )[names(sidi_weels) == "Delegation"] <- "NAME_2"
final_map <- left_join(sidi_weels, sidi_for, by = "NAME_2")

Then I plot the graph:

p<-ggplot(final_map, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Total),color = "black")+
  labs(x="",y="")+ theme_bw()+
  coord_fixed()
p<-p+scale_fill_gradientn(colours=c("white","yellow","green"), 
                            values=rescale(c(0,15,30)))
p<-p+theme(axis.line=element_blank(),
             axis.text.x=element_blank(),
             axis.text.y=element_blank(),
             axis.ticks=element_blank(),
             axis.title.x=element_blank(),
             axis.title.y=element_blank(),
             panel.grid.major = element_blank(),
             panel.grid.minor = element_blank(),
             panel.border = element_blank(),
             panel.background = element_blank())

p<-p+ theme(legend.position="right")
p<-p+labs(fill = "%",title = "Distribution of Deep Weels in Sidi Bouzid", subtitle = "Source: Ministry of Equipment Housing and Infrastructure", caption = "Salih Yasun")
p<-p+ theme(plot.title = element_text(hjust = 0.5),plot.subtitle = element_text(hjust=0.5))
p  


For a comparison, here are the actual numbers:

If you have any questions or suggestions, please e-mail me at syasun@indiana.edu