file<-'https://raw.githubusercontent.com/MervynOLuing/TanzaniaShapefiles/master/TanzaniaRegionCodesGDPPct.csv'
region<-read.csv(url(file))
library("shiny")
library("flexdashboard")
library(sf)
library(ggplot2)
library(rgdal)
library(ggsn)
library(tmap)
library(leaflet)

names(region)<-c("Region_Nam","2007","2010","2011","2012","2013","2014","2015","2016")
# Download data.zip from the web
download.file("http://www.nbs.go.tz/nbs/takwimu/references/GIS_Maps.zip", "maps.zip")
# Unzip file
unzip("maps.zip")
TanzmyDFW <-  st_read("Water_body.shp", quiet = TRUE, stringsAsFactors = FALSE)
TanzmyDF <-  st_read("Regions.shp",quiet = TRUE,  stringsAsFactors = FALSE)

TanzReg <- merge( TanzmyDF, region, by='Region_Nam')

df<-as.data.frame( region )
Tanzania <- ggplot(TanzReg) + suppressMessages(suppressWarnings(geom_sf(aes(fill=TanzReg$'2016')))) +
  scale_fill_gradient2 (low = "white", high = "red", # colours
                        limits = c(0, 20), # limts
                        na.value = "yellow", # colour when there are not values
                        name = "Regional GDP at Current Market Prices") + # legend options
  ggtitle("Regional Shares of GDP at Current Market Prices,Tanzania Mainland, 2016") +
  theme(plot.title = element_text(hjust = 0.1,size=10)) +
  theme(axis.text = element_blank(), # change the theme options
        axis.title = element_blank(), # remove axis titles
        axis.ticks = element_blank() )# remove axis ticks  

Tanzania   + geom_sf(data=TanzmyDFW, fill ="skyblue")  + theme(panel.background = element_rect(fill = "white"),
                                                               panel.border = element_rect(colour = "blue",fill = NA))  

tmap_mode("plot")
#2016
Tanzania2016 <- tm_shape(TanzReg) +
  tm_polygons(col = "2016",
              title="Regional GDP at \nCurrent Market Prices", title.size = 0.8) +
  tm_layout(main.title = "Regional Shares of GDP at \nCurrent Market Prices, Tanzania Mainland, 2016" ,
            main.title.position = c("top", "center") ,
            main.title.size = 0.8)
(TanzaniaB <- tm_shape(TanzmyDFW) + tm_fill(col = "lightblue") )

TanzaniaA2016 <- Tanzania2016 + TanzaniaB
#2015
(Tanzania2015 <- tm_shape(TanzReg) + 
    tm_polygons(col = "2015", 
                title="Regional GDP at Current \nMarket Prices - 2015", title.size = 0.8) +
    tm_layout(main.title = "Regional Shares of GDP at \nCurrent Market Prices, Tanzania Mainland, 2015" ,
              main.title.position = c("top", "center") ,
              main.title.size = 0.8)) 

(TanzaniaA2015 <- Tanzania2015 + TanzaniaB)

## 2014

(Tanzania2014 <- tm_shape(TanzReg) + 
    tm_polygons(col ="2014", 
                title="Regional GDP at Current \nMarket Prices - 2014", title.size = 0.8) +
    tm_layout(main.title = "Regional Shares of GDP at \nCurrent Market Prices, Tanzania Mainland, 2014" ,
              main.title.position = c("top", "center") ,
              main.title.size = 0.8)) 

(TanzaniaA2014 <- Tanzania2014 + TanzaniaB) 

#time series
tmap_arrange(TanzaniaA2014, TanzaniaA2015, TanzaniaA2016 )

tmap_mode("view")
TanzaniaA2014 +
  tm_legend(outside = TRUE) +
  tm_view(view.legend.position = c("left", "bottom", legend.title.size = .5,
                                   legend.text.size = .5))