knitr::opts_chunk$set(echo = TRUE)
library("rnaturalearth")
library("rnaturalearthdata")
library("RColorBrewer")
library("here")
library("rgdal")
library("sf")
library("tmap")
library("tmaptools")
library("sp")
library("rgdal")
library(tidyverse)
library(magrittr)
library(readxl)
library(readr)
library(readxl)
updated_vac <- read_excel("updated_vac.xls")

df_2 <- updated_vac %>% 
  filter(district!="1 Test District") %>% 
  #separate(district, c("district","dst"),sep = " ",remove = T) %>% 
  separate(period, c("epi_wk","yr"),sep = " ",remove = T) %>% 
  rowwise() %>% 
  mutate(booster = as.integer(booster),
         first_dose=as.integer(first_dose),
         second_dose=as.integer(second_dose),
         jj=as.integer(jj),
         p_vax=(first_dose),
         f_vax=sum(second_dose,jj, na.rm=TRUE),
         wk = as.integer(substring(epi_wk,2))) %>% 
  select(district, wk, yr, p_vax, f_vax, booster) %>% 
  group_by(district, wk, yr) %>% 
  summarise(p_vax=sum(p_vax),
            f_vax=sum(f_vax),
            booster=sum(booster))

df_3 <- df_2 %>%
  mutate_at(4, ~replace_na(.,0)) %>%
  mutate_at(5, ~replace_na(.,0)) %>%
  mutate_at(6, ~replace_na(.,0)) %>%
  group_by(district) %>% 
  summarise(p_vax=sum(p_vax),
            f_vax=sum(f_vax),
            booster=sum(booster))

ds <- readOGR("C:/Users/User/Desktop/EPIVAC/District", "District")
lk <- readOGR( "C:/Users/User/Desktop/EPIVAC/shape", "lk")

dist_shp <- merge(x=ds,y= df_3, by.x= "name", by.y ="district", all = T)

tm_shape(dist_shp)+
  tm_borders()+
  tm_fill( col=c("p_vax"),
           title=c("counts"), 
           style="fixed",
           breaks=c(0, 90000, 360000, 500000, 1000000,Inf), 
           palette = "RdYlGn")+
  sf::sf_use_s2(FALSE)+
  tm_shape(lk) + 
  tm_fill("lightblue")+
  tm_polygons("#a6cee3") +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Partial Vaccination (National)",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_credits("Data from 2021 to date",
             position = c("LEFT", "TOP"))
 

tm_shape(dist_shp)+
  tm_borders()+
  tm_fill( col=c("f_vax"),
           title=c("counts"), 
           style="fixed",
           breaks=c(0, 3500, 5000, 15000, 30000,Inf), 
           palette = "RdYlGn")+
  sf::sf_use_s2(FALSE)+
  tm_shape(lk) + 
  tm_fill("lightblue")+
  tm_polygons("#a6cee3") +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Full Vaccination (National)",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_credits("Data from 2021 to date",
             position = c("LEFT", "TOP"))


tm_shape(dist_shp)+
  tm_borders()+
  tm_fill( col=c("booster"),
           title=c("counts"), 
           style="fixed",
           breaks=c(0, 200, 500, 1000, 1500, 2000, Inf), 
           palette = "RdYlGn")+
  sf::sf_use_s2(FALSE)+
  tm_shape(lk) + 
  tm_fill("lightblue")+
  tm_polygons("#a6cee3") +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Booster Vaccination (National)",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_credits("Data from 2021 to date",
             position = c("LEFT", "TOP"))


region_data <- read_excel("region_data.xls")

reg_df <- region_data %>% 
  filter(region!="Armed forces") %>% 
  #separate(district, c("district","dst"),sep = " ",remove = T) %>% 
  separate(period, c("epi_wk","yr"),sep = " ",remove = T) %>% 
  rowwise() %>% 
  mutate(booster = as.integer(booster),
         first_dose=as.integer(first_dose),
         second_dose=as.integer(`2nd_dose`),
         jj=as.integer(jj),
         p_vax=(first_dose),
         f_vax=sum(second_dose,jj, na.rm=TRUE),
         wk = as.integer(substring(epi_wk,2))) %>% 
  select(region, wk, yr, p_vax, f_vax, booster) %>% 
  group_by(region, wk, yr) %>% 
  summarise(p_vax=sum(p_vax),
            f_vax=sum(f_vax),
            booster=sum(booster))

regions <- reg_df %>%
  mutate_at(4, ~replace_na(.,0)) %>%
  mutate_at(5, ~replace_na(.,0)) %>%
  mutate_at(6, ~replace_na(.,0)) %>%
  group_by(region) %>% 
  summarise(p_vax=sum(p_vax),
            f_vax=sum(f_vax),
            booster=sum(booster))

regn_shp <- readOGR("C:/Users/User/Desktop/EPIVAC/Regions/Region", "UDHS Regions 2019")

reg_shp <- merge(x=regn_shp,y= regions, by.x= "Name", by.y ="region", all = T)

tm_shape(reg_shp)+
  tm_borders()+
  tm_fill( col=c("p_vax"),
           title=c("Counts"), 
           style="fixed",
           breaks=c(0, 400000, 500000, 1000000,Inf), 
           palette = "RdYlGn")+
  sf::sf_use_s2(FALSE)+
  tm_shape(lk) + 
  tm_fill("lightblue")+
  tm_polygons("#a6cee3") +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Partial Vaccination (Regional)",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_credits("Data from 2021 to date",
             position = c("LEFT", "TOP"))



tm_shape(reg_shp)+
  tm_borders()+
  tm_fill( col=c("f_vax"),
           title=c("Counts"), 
           style="fixed",
           breaks=c(0, 100000, 400000, 800000, Inf), 
           palette = "RdYlGn")+
  sf::sf_use_s2(FALSE)+
  tm_shape(lk) + 
  tm_fill("lightblue")+
  tm_polygons("#a6cee3") +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Full Vaccination (Regional)",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_credits("Data from 2021 to date",
             position = c("LEFT", "TOP"))

tm_shape(reg_shp)+
  tm_borders()+
  tm_fill( col=c("booster"),
           title=c("Counts"), 
           style="fixed",
           breaks=c(0,50000, 600000, Inf), 
           palette = "RdYlGn")+
  sf::sf_use_s2(FALSE)+
  tm_shape(lk) + 
  tm_fill("lightblue")+
  tm_polygons("#a6cee3") +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Booster Vaccination (Regional)",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tm_credits("Data from 2021 to date",
             position = c("LEFT", "TOP"))

##Faceting
tm_shape(reg_shp)+
  tm_borders()+
  tm_fill( col=c("p_vax"),
           title=c("Counts"),
           textNA = "missing", 
           style="fixed",
           breaks=c(0, 400000,500000,1000000,Inf), 
           palette = "RdYlGn")+
  tm_facets('F15Regions')+
  tm_shape(lk) + 
  tm_polygons("#a6cee3" ) +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Partial Vaccination by Region",
            main.title.position = "left")

tm_shape(reg_shp)+
  tm_borders()+
  tm_fill( col=c("f_vax"),
           title=c("Counts"),
           textNA = "missing", 
           style="fixed",
           breaks=c(0, 100000, 400000, 800000, Inf), 
           palette = "RdYlGn")+
  tm_facets('F15Regions')+
  tm_shape(lk) + 
  tm_polygons("#a6cee3" ) +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Full Vaccination by Region",
            main.title.position = "left")


tm_shape(reg_shp)+
  tm_borders()+
  tm_fill( col=c("booster"),
           title=c("Counts"),
           textNA = "missing", 
           style="fixed",
           breaks=c(0,50000, 600000, Inf), 
           palette = "RdYlGn")+
  tm_facets('F15Regions')+
  tm_shape(lk) + 
  tm_polygons("#a6cee3" ) +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Booster Vaccination by Region",
            main.title.position = "left")


sub_county_data <- read_excel("sub_county_data.xls", guess_max = 100000)

sub_data <- sub_county_data %>% 
  separate(period, c("month","yr"),sep = " ",remove = T) %>% 
  mutate(booster = as.integer(boosetr),
         first_dose=as.integer(first_dose),
         second_dose=as.integer(second_dose),
         jj=as.integer(jj))

sub_data %<>% 
  rowwise() %>% 
  mutate(p_vax=(first_dose),
         f_vax=sum(second_dose,jj, na.rm=TRUE)) %>% 
  select(sub_county, p_vax, f_vax, booster) %>% 
  mutate_at(2, ~replace_na(.,0)) %>%
  mutate_at(3, ~replace_na(.,0)) %>%
  mutate_at(4, ~replace_na(.,0))



#
sub_data %<>% 
  group_by(sub_county) %>%
  summarise(p_vax=sum(p_vax),
            full_vax=sum(f_vax),
            booster_vax=sum(booster))

#fitting in the sub-county shapefile
sub_shp <- readOGR("C:/Users/User/Desktop/EPIVAC/Subcounties", "Subcounties")

sub_mg <- merge(x=sub_shp,y= sub_data, by.x= "name", by.y ="sub_county", all = T)

tm_shape(sub_mg)+
  tm_borders()+
  tm_fill( col=c("p_vax"),
           title=c("Counts"),
           textNA = "missing", 
           style="fixed",
           breaks=c(0, 1000, 3000, 4000, 5000, 10000,Inf), 
           palette = "RdYlGn")+
  tm_shape(lk) + 
  tm_polygons("#a6cee3" ) +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Partial Vaccination at sub-county level",
            main.title.position = "left")

tm_shape(sub_mg)+
  tm_borders()+
  tm_fill( col=c("full_vax"),
           title=c("Counts"),
           textNA = "missing", 
           style="fixed",
           breaks=c(0, 1000, 3000, 4000, 5000, 10000,Inf), 
           palette = "RdYlGn")+
  tm_shape(lk) + 
  tm_polygons("#a6cee3" ) +
  tm_layout(bg.color="white")+
  tm_legend(legend.outside = TRUE)+
  tm_layout(bg.color="white",
            main.title = "Full Vaccination at sub-county level",
            main.title.position = "left")