Map of Tunisia with R
Import the shp files with different resolutions from the following links:
Delegation polygons: https://github.com/malouche/Maps-of-Tunisia-delagations
Gouvernorat polygons: https://github.com/malouche/Maps-of-Tunisia-Gouvernorat
> library(maptools)
> library(sp)
> library(shapefiles)
> tn_deleg<-"/Users/dhafermalouche/Documents/GitHub/Maps-of-Tunisia-delagations/Tunisie_snuts4.shp"
> m_deleg <- readShapePoly(tn_deleg)
> plot(m_deleg)
> tn_gouv<-"/Users/dhafermalouche/Documents/GitHub/Maps-of-Tunisia-Gouvernorat/Tunisie_snuts3.shp"
> m_gouv <- readShapePoly(tn_gouv)
> plot(m_gouv)
raster
package> library(raster)
> m_deleg2<- getData(name="GADM", country="TUN", level=2)
> plot(m_deleg2)
> m_gouv2<- getData(name="GADM", country="TUN", level=1)
> plot(m_gouv2)
ggplot2
> library(ggplot2)
> tn_deleg_fr <- fortify(m_deleg2,region = "HASC_2")
> colnames(tn_deleg_fr)
[1] "long" "lat" "order" "hole" "piece" "id" "group"
> library(ggplot2)
> p1<-ggplot(tn_deleg_fr, aes(x=long, y=lat, group=group)) +
+ geom_polygon(fill="lightblue",color = "black")+
+ labs(x="",y="")+ theme_bw()+
+ coord_fixed()
> p1<-p1+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())
> p1<-p1+ theme(legend.position="none")
> p1
> tn_gouv_fr <- fortify(m_gouv2,region = "HASC_1")
> colnames(tn_gouv_fr)
[1] "long" "lat" "order" "hole" "piece" "id" "group"
> p2<-ggplot(tn_gouv_fr, aes(x=long, y=lat, group=group)) +
+ geom_polygon(fill="pink",color = "black")+
+ labs(x="",y="")+ theme_bw()+
+ coord_fixed()
> p2<-p2+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())
> p2<-p2+ theme(legend.position="none")
> p2
It’s a thematic map in which areas are shaded or patterned in proportion to the measurement of the statistical variable being displayed on the map.
Let’s make a map of Tunisia at the level of Gouvernorat and represents the Development Regional Index in 2010.
Let’s then import the data into R
. This data is available in https://www.kaggle.com/dhafer/regional-development-index-tunisia. Let’s then import firt into R
> library(readr)
> dt<-read_csv("idr_gouv.csv")
> dt1=as.data.frame(dt)
> head(dt1)
HASC_1 gouvernorat IDR
1 TN.TU TUNIS 0.76
2 TN.AN ARIANA 0.69
3 TN.BA BEN AROUS 0.66
4 TN.MS MONASTIR 0.64
5 TN.SS SOUSSE 0.62
6 TN.NB NABEUL 0.57
Let us notice the presence of a variable HASC_1
. We need then to merge this later data using this column
We download the Tunisian map with polygons at the level of Gouvernorats:
> library(raster)
> m_gouv2<- getData(name="GADM", country="TUN", level=1)
> m_gouv2@data$HASC_1[1:4]
[1] "TN.AN" "TN.BJ" "TN.BA" "TN.BZ"
ggplot2
fortify
from ggplot2
package and we will merge the new fortified data with the data containing the variable of interest and the HASC_1
variable.> library(ggplot2)
> tn_gouv_fr <- fortify(m_gouv2,region = "HASC_1")
> i=match(tn_gouv_fr$id,dt1$HASC_1)
> tn_gouv_fr$IDR=dt1$IDR[i]
> colnames(tn_gouv_fr)
[1] "long" "lat" "order" "hole" "piece" "id" "group" "IDR"
> p2<-ggplot(tn_gouv_fr, aes(x=long, y=lat, group=group)) +
+ geom_polygon(aes(fill=IDR),color = "black")+
+ labs(x="",y="")+ theme_bw()+
+ coord_fixed()
> p2<-p2+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())
> p2<-p2+ theme(legend.position="right")
> p2
> library(readr)
> dt2<-read_csv("data_delegation_tunisia.csv")
> dt2=as.data.frame(dt2)
> head(dt2[,1:3])
Gouvernorat delegation HASC_2
1 Ariana Ariana-Ville TN.AN.AR
2 Ariana Ettadhamen TN.AN.ET
3 Ariana Kalaat-Landlous TN.AN.KA
4 Ariana La-Soukra TN.AN.LS
5 Ariana Mnihla TN.AN.MN
6 Ariana Raoued TN.AN.RA
HASC_2
variable> library(raster)
> m_deleg2<- getData(name="GADM", country="TUN", level=2)
> m_deleg2@data$HASC_2[1:4]
[1] "TN.AN.AR" "TN.AN.ET" "TN.AN.KA" "TN.AN.MN"
> x=(dt2$Nahdha_2011-dt2$Nahdha_2014)/dt2$Nahdha_2011*100
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
-36.59 26.46 38.76 33.47 45.21 65.29 3
fortify
function and bind the new column to the data that will be displayed> library(ggplot2)
> tn_deleg_fr <- fortify(m_deleg2,region = "HASC_2")
> i=match(tn_deleg_fr$id,dt2$HASC_2)
> tn_deleg_fr$DeltaVotes=x[i]
> library(scales)
> p2<-ggplot(tn_deleg_fr, aes(x=long, y=lat, group=group)) +
+ geom_polygon(aes(fill=DeltaVotes),color = "black")+
+ labs(x="",y="")+ theme_bw()+
+ coord_fixed()
> p2<-p2+scale_fill_gradientn(colours=c("blue","white","red"),
+ values=rescale(c(-100,0,100)))
>
> p2<-p2+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())
> p2<-p2+ theme(legend.position="right")
> p2+labs(fill = "% of the Diff, \n of Votes \n 2011 - 2014")
spplot
functionspplot
is a function from the package latice
that can be used to plot choropleth maps. Let us use it on the previous example> library(lattice)
> i=match(m_deleg2@data$HASC_2, dt2$HASC_2)
> m_deleg2@data$DeltaVotes=x[i]
> spplot(m_deleg2,"DeltaVotes",
+ main="% of the Diff, \n of Votes \n 2011 - 2014",sub="",
+ col.regions=colorRampPalette(c('blue', 'white','red'))(30))
Leaflet
packageLeaflet
is one of the most popular open-source JavaScript libraries for interactive maps. To learn more about it visit: https://rstudio.github.io/leaflet/
Let us now see how can we create interactive maps with leaflet
R
package
We will then represent again the variable `Difference of votes'' added above to the delegation map
m_deleg2`. We will also add popups that help the user when clicking on a polygone to know the name of the delgation and the value of the variable at this delegation.
we start then by the popups
> my_popup <- paste0("<strong>",m_deleg2@data$NAME_2,"</strong>"," (",round(m_deleg2@data$DeltaVotes,1),"%)")
> my_popup[1:4]
[1] "<strong>Ariana Médina</strong> (34.6%)"
[2] "<strong>Ettadhamen</strong> (50.6%)"
[3] "<strong>Kalaat El Andalous</strong> (45.4%)"
[4] "<strong>Mnihla</strong> (44.7%)"
> library(leaflet)
>
> MyPaletteColor <- colorBin("RdYlBu", domain=(-40):70,bins=11,na.color = "white")
leaflet
> mm<-leaflet(data = m_deleg2) %>%
+ addProviderTiles("CartoDB.Positron") %>%
+ addPolygons(fillColor = ~MyPaletteColor(m_deleg2@data$DeltaVotes),
+ fillOpacity = 0.8,
+ color = "#BDBDC3",
+ weight = 1,
+ popup = my_popup)
> mm