A national picture in terms of deaths, cases and reporting rates

Malaria Channels for the different districts

Malaria channels from cities

Regional faceting

knitr::opts_chunk$set(echo = FALSE)
library(readxl)
library(tidyverse)
library(ggplot2)
library(esquisse)
library(extrafont)
library(extrafontdb)
library(magrittr)
library(patchwork)
library("rnaturalearth")
library("rnaturalearthdata")
library(RColorBrewer)
library(here)
library(rgdal)
library(sf)
library("tmap")
library("tmaptools")
library("sp")
library(rgdal)
library(gganimate)

malaria_national <- read_excel("malaria_national.xls")

malaria_national %>%
  separate(period, c("epi_wk","yr"),sep = " ",remove = T) %>% 
  mutate(wk = as.integer(substring(epi_wk,2))) %>% 
  ggplot(aes(wk, cases))+
  geom_col(fill = "forestgreen")+
  labs(title = "Epicurve", y = "No. of cases", x = "Epi-Week",
       caption = "Data from Ministry of Health, Uganda",
       subtitle = "Malaria cases, so far recorded in 2022")+
  scale_y_continuous(labels = scales::comma, 
                     expand = c(0,0), limits = c(0,350000),
                     breaks = seq(from = 0, to = 350000, by = 50000))+
  scale_x_continuous(expand = c(0,0),
                     breaks = seq(from = 1, to = 53, by = 4))+
  theme(plot.title = element_text(size = 25, face = "bold"), 
        plot.subtitle = element_text(size = 15),
        axis.text = element_text(size = 10),
        axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        plot.caption = element_text(size = 10))+
  theme_bw()



malaria_national %>%
  separate(period, c("epi_wk","yr"),sep = " ",remove = T) %>% 
  mutate(wk = as.integer(substring(epi_wk,2))) %>% 
  ggplot(aes(wk, rrts))+
  geom_line(color = "black")+
  geom_area(fill = "lightblue")+
  labs(title = "Reporting rates", y = "Percentage", x = "Epi-Week",
       caption = "Data from Ministry of Health, Uganda",
       subtitle = "National reporting rates (%)")+
  scale_y_continuous(labels = scales::comma, 
                     limits = c(0,100), expand = c(0,0),
                     breaks = seq(from = 0, to = 100, by = 20))+
  scale_x_continuous(expand = c(0,0), 
                     breaks = seq(from = 1, to = 53, by = 4))+
  theme(plot.title = element_text(size = 25, face = "bold.italic"), 
        plot.subtitle = element_text(size = 15),
        axis.text = element_text(size = 10),
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 15),
        plot.caption = element_text(size = 10))+
  theme_bw()

  

malaria_national %>%
  separate(period, c("epi_wk","yr"),sep = " ",remove = T) %>% 
  mutate(wk = as.integer(substring(epi_wk,2))) %>% 
  ggplot(aes(wk, deaths))+
  geom_col(fill = "red")+
  labs(title = "Malaria deaths", y = "No. of deaths", x = "Epi-Week",
       caption = "Data from Ministry of Health, Uganda",
       subtitle = "These are the deaths due to malaria so far recorded, 2022")+
  scale_y_continuous(labels = scales::comma,
                     limits = c(0,800), breaks = seq(from = 0, to = 800, 
                                                     by = 100),
                     expand = c(0,0))+
  scale_x_continuous(expand = c(0,0), 
                     breaks = seq(from = 1, to = 53, by = 4))+
  theme(plot.title = element_text(size = 25, face = "bold.italic"), 
        plot.subtitle = element_text(size = 15),
        axis.text = element_text(size = 10),
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 15),
        plot.caption = element_text(size = 10))+
  theme_light()




#importing the data sets
mal_df <- read_excel("malaria_data.xlsx", guess_max = 100000)

mal_df$Cases[ mal_df$Year == 2017 & mal_df$Cases > 9258] <- 9258
mal_df$Cases[ mal_df$Year == 2018 & mal_df$Cases > 9460] <- 9460
mal_df$Cases[ mal_df$Year == 2019 & mal_df$Cases > 11007] <- 11007
mal_df$Cases[ mal_df$Year == 2020 & mal_df$Cases > 9352] <- 9352
mal_df$Cases[ mal_df$Year == 2021 & mal_df$Cases > 9166] <- 9166
mal_df$Cases[ mal_df$Year == 2022 & mal_df$Cases > 9950] <- 9950

mal_df %<>% select(-c("Period", "DST", "Week_num")) %>% 
  pivot_wider(names_from = Year, values_from = c(Cases, Deaths, rrts)) %>% 
  select(District, Week, starts_with("Cases"), Deaths_2022, rrts_2022) %>%
  arrange()

cases <- select(mal_df, Cases_2017:Cases_2021)

mal_df %<>%
  mutate(Upper_limit = apply(cases,1, quantile, 0.75, na.rm = TRUE),
                   Lower_limit = apply(cases,1, quantile, 0.25, na.rm = TRUE)) %>%
  mutate_if(is.numeric,round, digits = 0)

mal_df %<>% 
  mutate(wk = as.integer(substring(Week,2)))

#identifying districts that reported cases above the upper limit
mal_df$d <- mal_df$Upper_limit - mal_df$Cases_2022

colors <- c("Upper Limit" = "red", "Lower Limit" = "green",
            "Malaria cases in 2022" = "#1D2C93")
c1 <- mal_df %>% 
  filter(District == "Kaliro District") %>%
  ggplot(aes(wk)) +
  geom_line( size= 0.5 ,aes(y= Upper_limit,color="Upper Limit")) +
  geom_line( size = 0.5 , aes(y = Lower_limit, color = "Lower Limit")) +
  geom_line( size=0.5, aes(y=Cases_2022, color="Malaria cases in 2022"))+ 
  theme(plot.title = element_text(face = "bold"),
        axis.text.y = element_text(size = 10, hjust = 1),
        plot.margin = margin(rep(15, 4)),
        axis.text.x = element_text(size = 10, hjust = 1))+
  labs(y="No. of cases",x="Epi Week",
       title = "Kaliro District",
       subtitle = "Normal Malaria Channel",
       color = "Legend")+
  scale_color_manual(values = colors)+
  scale_y_continuous(limits = c(0, 3500),breaks = seq(0, 3500, by = 500),
                     expand = c(0,0))+
  scale_x_continuous(limits = c(1, 53),breaks = seq(1, 53, by = 4), 
                     expand = c(0, 0), # The horizontal axis does not extend to either side
                     position = "bottom")+
  theme_bw ()+
  theme(legend.position = "bottom",
        legend.title = element_blank())+
  transition_reveal(wk)

animate(c1, nframes = 53, fps = 5)


lk <- readOGR( here("shape"), "lk" ) #lakes


ds <- readOGR("C:/Users/User/Desktop/R Work/malaria/Shapes_2020/uganda_districts_2019-wgs84",
              "uganda_districts_2019-wgs84")

region <- readOGR("C:/Users/User/Desktop/R Work/malaria/Shapes_2020/Region", "UDHS Regions 2019")


mal_data <- mal_df%>% 
  select(District, wk, Cases_2022, rrts_2022, Deaths_2022) %>% 
  filter(wk == 27) %>% 
  mutate(district = toupper(District)) %>% 
  separate(district, c("dist","gen"),sep = " ",remove = T) %>% 
  select(dist, Cases_2022, rrts_2022, Deaths_2022) %>% 
  mutate_at(4, ~replace_na(.,0))


mal_shp <- merge(x=ds,y= mal_data, by.x= "DName2019", by.y ="dist", all = T)

tm_shape(mal_shp)+
  tm_borders()+
  tm_fill( col=c("Cases_2022"),
           title=c("No. of cases"), 
           #textNA = "Missing", 
           style="fixed",
           breaks=c(0, 500, 1000, 2000, 5000, 10000, Inf), 
           palette = "YlOrRd")+
  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 = "Malaria cases (week 27)",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))


tm_shape(mal_shp)+
  tm_borders()+
  tm_fill( col=c("rrts_2022"),
           title=c("Reporting rates (%)"), 
           #textNA = "Missing", 
           style="fixed",
           breaks=c(0, 20, 40, 60, 80, 100, Inf), 
           palette = "RdYlGn")+
  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 = "Reporting rates (week 27)",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))

tm_shape(mal_shp)+
  tm_borders()+
  tm_fill( col=c("Deaths_2022"),
           title=c(""), 
           #textNA = "Missing", 
           style="fixed",
           breaks=c(0, 1, 2, 6, 10, 100, Inf), 
           palette = "OrRd")+
  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 = "Deaths due to malaria (week 27)",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))

#-------------------------------------------------------------------------------
mal_regional <- read_excel("malaria_region.xls")

regional <- mal_regional %>%
  mutate_at(4, ~replace_na(.,0)) %>% 
  separate(period, c("epi_wk","yr"),sep = " ",remove = T) %>% 
  mutate(wk = as.integer(substring(epi_wk,2))) %>% 
  filter(wk ==27) %>% 
  select(region, cases, deaths, rrts)

region_shp <- merge(x=region ,y= regional, by.x= "Name", 
                    by.y ="region", all = T)

tm_shape(region_shp)+
  tm_borders()+
  tm_fill( col=c("cases"),
           title=c("No. of cases"), 
           #textNA = "Missing", 
           style="fixed",
           breaks=c(0, 3000, 5000, 10000, 20000, 30000, Inf), 
           palette = "YlOrRd")+
  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 = "Malaria cases, week 27, Regional",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))

tm_shape(region_shp)+
  tm_borders()+
  tm_fill( col=c("rrts"),
           title=c("Reporting rates (%)"), 
           #textNA = "Missing", 
           style="fixed",
           breaks=c(0, 20, 40, 60, 80, 100, Inf), 
           palette = "RdYlGn")+
  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 = "Reporting rates (week 27), Regional",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))

tm_shape(region_shp)+
  tm_borders()+
  tm_fill( col=c("deaths"),
           title=c("deaths"), 
           #textNA = "Missing", 
           style="fixed",
           breaks=c(0, 1, 2, 6, 10, 100, Inf), 
           palette = "OrRd")+
  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 = "Deaths due to malaria week 27, Regional",
            main.title.position = "left")+
  tm_compass(type = 'arrow', position = c(0.05, 0.8))+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))

#-------------------------------------------------------------------------

mydistrict <- unique(mal_df$District)

c_week <- 27

for(dist in mydistrict){
  kk <-  mal_df%>% filter(District == dist)
  #if(kk$d[c_week]<0){ #& kk$d[c_week-17]<0
    c1 <-  kk %>%
      ggplot(aes(wk)) +
      geom_line( size= 0.5 ,aes(y= Upper_limit,color="Upper Limit")) +
      geom_line( size = 0.5 , aes(y = Lower_limit, color = "Lower Limit")) +
      geom_line( size=0.5, aes(y=Cases_2022, color="Malaria cases in 2022"))+
      theme(axis.text.y = element_text(size = 10, hjust = 1),
        plot.margin = margin(rep(15, 4)),
        axis.text.x = element_text(size = 10, hjust = 1))+
      labs(y="No. of cases",x="Epi Week",
       subtitle = "Normal Malaria Channel",
       color = "Legend")+
      ggtitle(paste(dist, sep = " "))+
      scale_color_manual(values = colors)+
      scale_x_continuous(limits = c(1, 53),breaks = seq(1, 53, by = 4), 
                     expand = c(0, 0),
                     position = "bottom")+
      theme_classic()+
      theme(legend.position = "bottom",
        legend.title = element_blank())
    
    c2 <- kk %>% 
      ggplot(aes(wk)) +
      geom_line( size= 0.5 ,aes(y= rrts_2022), color="black") +
      theme(axis.text.y = element_text(size = 10, hjust = 1),
        plot.margin = margin(rep(15, 4)),
        axis.text.x = element_text(size = 10, hjust = 1))+
      labs(y="%age",x="Epi Week",
       subtitle = "Reporting rates")+
      scale_color_manual(values = colors)+
      scale_y_continuous(limits = c(0, 100),breaks = seq(0, 100, by = 20),
                     expand = c(0,0))+
      scale_x_continuous(limits = c(1, 53),breaks = seq(1, 53, by = 4), 
                     expand = c(0, 0),
                     position = "bottom")+
      theme_classic()+
      theme(legend.position = "bottom",
        legend.title = element_blank())

      p <- c1/c2
            
      print(p)

  #}
}

library(readr)
Mal_data <- read_csv("Mal_data.csv", guess_max = 100000)


mal <- Mal_data %>%
  separate(periodname, c("epi_week","yr"),sep = " ",remove = T) %>% 
  pivot_wider(names_from = yr, values_from = c(cases, rrt)) %>% 
  select(epi_week, district, cases_2020, cases_2021, cases_2022, rrt_2022) %>% 
  mutate_if(is.numeric, round, digits = 0)


mal %<>% 
  mutate_at(3, ~replace_na(.,0)) %>% 
  rowwise() %>% 
  mutate(Upper_limit = quantile(c_across(cases_2020:cases_2021), 0.75, na.rm = TRUE)) %>% 
  mutate_if(is.numeric,round, digits = 0)

mal %<>% 
  mutate_at(4, ~replace_na(.,0)) %>% 
  rowwise() %>% 
  mutate(Lower_limit = quantile(c_across(cases_2020:cases_2021), 0.25, na.rm = TRUE)) %>% 
  mutate_if(is.numeric,round, digits = 0) %>% 
  mutate(wk = as.integer(substring(epi_week,2)))


#filtering to obtain cities only 
c <- mal %>% filter( grepl("City", district ) )

c$diff <- c$Upper_limit - c$cases_2022

mycity <- unique(c$district)

c_week <- 27

for(city in mycity){
  kk <- c %>% filter(district == city)
  #if(kk$diff[c_week]<0) { #& kk$diff[c_week-15]<0)
    d1 <- kk %>%
      ggplot(aes(wk)) +
      geom_line( size= 0.5 ,aes(y= Upper_limit,color="Upper Limit")) +
      geom_line( size = 0.5 , aes(y = Lower_limit, color = "Lower Limit")) +
      geom_line( size=0.5, aes(y= cases_2022, color="Malaria cases in 2022"))+
      theme(axis.text.y = element_text(size = 10, hjust = 1),
                   plot.margin = margin(rep(15, 4)),
                   axis.text.x = element_text(size = 10, hjust = 1))+
      labs(y="No. of cases",x="Epi Week",
                  subtitle = "Normal Malaria Channel",
                  color = "Legend")+
      ggtitle(paste(city, sep = " "))+
      scale_color_manual(values = colors)+
      scale_x_continuous(limits = c(1, 53),breaks = seq(1, 53, by = 4), 
                     expand = c(0, 0),
                     position = "bottom")+
      theme_classic()+
      theme(legend.position = "bottom",
                   legend.title = element_blank())
           
      d2 <- kk %>% 
        ggplot(aes(wk)) +
        geom_line( size= 0.5 ,aes(y= rrt_2022), color="black") +
        theme(axis.text.y = element_text(size = 10, hjust = 1),
                   plot.margin = margin(rep(15, 4)),
                   axis.text.x = element_text(size = 10, hjust = 1))+
        labs(y="%age",x="Epi Week",
                  subtitle = "Reporting rates")+
        scale_color_manual(values = colors)+
        scale_y_continuous(limits = c(0, 100),breaks = seq(0, 100, by = 20),
                     expand = c(0,0))+
        scale_x_continuous(limits = c(1, 53),breaks = seq(1, 53, by = 4), 
                     expand = c(0, 0),
                     position = "bottom")+
        theme_classic()+
        theme(legend.position = "bottom",
              legend.title = element_blank())
      
      d <- d1/d2
      
      print(d)
             
#  } 
}



mal_regional <- read_excel("malaria_region.xls")

mal_regional %<>%
  mutate_at(4, ~replace_na(.,0)) %>%
  separate(period, c("epi_wk","yr"),sep = " ",remove = T) %>% 
  mutate(wk = as.integer(substring(epi_wk,2)))

mal_regional %>% 
  ggplot(aes(wk)) +
  geom_line(aes(y= cases), color="black")+
  geom_area(aes(y= cases), fill = "forestgreen")+
  theme(plot.title = element_text(vjust = 0.5, face = "bold", size = 20),
        strip.background = element_rect(color = "black", 
                                        size=1.5, linetype="solid"),
    axis.text.y = element_text(size = 10, hjust = 1),
        plot.margin = margin(rep(15, 4)),
        axis.text.x = element_text(size = 10, hjust = 1))+
  labs(y="No. of cases",x="Epi Week",
       title ="Malaria cases by region", caption = "Data from Ministry of Health, Uganda")+
  scale_color_manual(values = colors)+
  scale_x_continuous(limits = c(1, 53),breaks = seq(1, 53, by = 4), 
                     expand = c(0, 0),
                     position = "bottom")+
  theme_bw()+
  facet_wrap(~region, scales = "free", ncol = 3, nrow = 5)

mal_regional %>% 
  ggplot(aes(wk)) +
  geom_line(aes(y= deaths), fill="black")+
  geom_area(aes(y= deaths), fill = "red")+
  #geom_smooth(aes(y= deaths), color="red")+
  theme(plot.title = element_text(vjust = 0.5, face = "bold", size = 20),
        strip.background = element_rect(color = "black", 
                                        size=1.5, linetype="solid"),
        axis.text.y = element_text(size = 10, hjust = 1),
        plot.margin = margin(rep(15, 4)),
        axis.text.x = element_text(size = 10, hjust = 1))+
  labs(y="No. of deaths",x="Epi Week",
       title ="Malaria deaths by region", caption = "Data from Ministry of Health, Uganda")+
  scale_color_manual(values = colors)+
  scale_x_continuous(limits = c(1, 53),breaks = seq(1, 53, by = 4), 
                     expand = c(0, 0),
                     position = "bottom")+
  theme_bw()+
  facet_wrap(~region, scales = "free", ncol = 3, nrow = 5)